Question

I have followed some of the questions and examples in this website and implemented the following but the dialog is still showing at top left corner instead of in the middle of the screen.

I would like to have the background dimmed and locate the dialog in the center of the screen...but how could the following be modified? Thanks in advance!!

    @Override  
    public void onBackPressed()   
    {  
        final Dialog dialog1 = new Dialog(First.this, android.R.style.Theme_Translucent_NoTitleBar);
        WindowManager.LayoutParams lp = dialog1.getWindow().getAttributes();
        lp.dimAmount = 0.5f;
        dialog1.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);           

        Window window = dialog1.getWindow();
        window.setGravity(Gravity.CENTER);

        dialog1.setContentView(R.layout.alert_dialog);
        dialog1.setCancelable(true);                        

        TextView thankyou = (TextView) dialog1.findViewById(R.id.thankyou);
        TextView Title_V1 = (TextView) dialog1.findViewById(R.id.Title_V1);
        Button alert_cancel = (Button) dialog1.findViewById(R.id.button_cancel);
        Button alert_quit = (Button) dialog1.findViewById(R.id.button_ok);

        thankyou.setText("Quit?");
        Title_V1.setText("");
        alert_cancel.setText("Cancel");
        alert_quit.setText("Quit!");

        dialog1.show(); 

alert_dialog.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginBottom="30dp"
    android:layout_marginLeft="5dp"
    android:layout_marginRight="5dp"
    android:layout_marginTop="30dp"
    android:background="@drawable/layout_round_corners"
    android:gravity="center" >

    <TextView
        android:id="@+id/thankyou"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="8dp"
        android:layout_marginTop="5dp"
        android:paddingBottom="10dp"
        android:paddingTop="15dp"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:textColor="@color/dark_green"
        android:textSize="25dp"
        android:textStyle="bold" />

    <View
        android:id="@+id/view1"
        android:layout_width="fill_parent"
        android:layout_height="2dp"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/thankyou"
        android:background="@color/green"
        android:paddingTop="20dp" />

    <ScrollView
        android:id="@+id/ScrollView01"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/view1" >

        <TableLayout
            android:id="@+id/Table1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:paddingLeft="8dp"
            android:paddingRight="8dp" >

            <TextView
                android:id="@+id/Title_V1"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="8dp"
                android:textColor="@color/green"
                android:textSize="25dp"
                android:textStyle="bold" />

        </TableLayout>
    </ScrollView>

    <LinearLayout
        android:id="@+id/LinearLayout_Row_1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_below="@+id/ScrollView01"
        android:gravity="center"
        android:orientation="horizontal"
        android:padding="15dp" >

        <Button
            android:id="@+id/button_cancel"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_margin="5dp"
            android:layout_weight="0.5"
            android:background="@drawable/green_btn"
            android:textColor="@color/white"
            android:textSize="@dimen/dp25_heading"
            android:textStyle="bold" />

        <Button
            android:id="@+id/button_ok"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="5dp"
            android:layout_weight="0.5"
            android:background="@drawable/green_btn"
            android:textColor="@color/white"
            android:textSize="@dimen/dp25_heading"
            android:textStyle="bold" />
    </LinearLayout>

</RelativeLayout>
Was it helpful?

Solution

put one parent layout for your Relative Layout and give it fill_parent for android:layout_width and android:layout_height

<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:gravity="center"
    >
// your entire layout and no need to set gravity for your child relative layout.
</RelativeLayout>

and now no need to set gravity for your child RelativeLayout even for window.
and yes make your parent view background to transparent:

android:background="@android:color/transparent"
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top