Search code examples
javaandroidandroid-layoutandroid-xmlandroid-dialog

Why is Android custom dialog coming fullscreen


I am trying to create custom dialog.

Base Layout (I have also tried various modified layouts):

<RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <TextView
            android:id="@+id/title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignParentRight="true"
            android:layout_alignParentTop="true"
            android:background="@color/dialogHeaderBackground"
            android:gravity="center"
            android:minHeight="@dimen/minHeightDialogTitle"
            android:text=""
            android:textStyle="bold" />

        <TextView
            android:id="@+id/message"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_below="@+id/title"
            android:layout_marginTop="5dp"
            android:text="" />

        <LinearLayout
            android:id="@+id/loutButton"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_below="@+id/message"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="5dp"
            android:orientation="horizontal" >

            <Button
                android:id="@+id/btnNeutral"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="" />

            <Button
                android:id="@+id/btnNegative"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="" />

            <Button
                android:id="@+id/btnPositive"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="" />
        </LinearLayout>
    </RelativeLayout>

Java code:

        Dialog dialog = new Dialog(this);
//      Dialog dialog = new Dialog(this,R.style.Theme_Dialog);
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        
        dialog.setContentView(R.layout.custom_dialog);
        
        Button btnPositive=(Button) dialog.findViewById(R.id.btnPositive);
        Button btnNegative=(Button) dialog.findViewById(R.id.btnNegative);
        Button btnNeutral=(Button) dialog.findViewById(R.id.btnNeutral);
        
        TextView txvTitle=(TextView)dialog.findViewById(R.id.title);
        TextView txvMessage=(TextView)dialog.findViewById(R.id.message);
        ...
        so on and so forth

I get following dialog, which is fullscreen, which I do not want:

fullscreen dialog

Solutions already tried, which give same results as above screenshot:

  1. Styles.xml configuration 1 | Implemented Pastebin code
  2. Styles.xml configuration 2 | Implemented Pastebin code
  3. The example provided in Android Dev Guide using AlertDialog.Builder | Implemented Java Code
  4. None of my underlying layout use MATCH_PARENT in height. As said in this Stack overflow answer
  5. Also try to change window settings to WRAP_CONTENT for dialog based on answer here | Implemented Java Code in Pastebin
  6. Have also tried to keep entire RelativeLayout inside a parent LinearLayout, see the 3rd comment in following answer

The only time I can get decent dialog is when specifying height for the XML layout, like this android:layout_height="150dp"

 <RelativeLayout
        xmlns:android="http://schemas.android.com/apk/res/android"        
        android:layout_width="match_parent"
        android:layout_height="150dp" >

Dialog when fixed height is specified:

  1. The content/layout goes missing when message is bigger more text in dialog message
  2. When message is smaller, dialog is displayed bigger than required, see below OK button less text is dialog message

Don't suggest the above (specify fixed height) as solution as it is not efficient and not looking that good and also not dynamic, it's static.

What to do specify,decent looking custom dialog which is customizable too?


Solution

  • remove android:layout_alignParentBottom="true"line from linear layout