5

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, its static

Question

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

I am willing to give bounty as a token of appreciation if you can help me solve this issue

EDIT: Provided bounty to correct solution.

Community
  • 1
  • 1
Akhil Jain
  • 13,872
  • 15
  • 57
  • 93

5 Answers5

6

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

Prachi
  • 2,559
  • 4
  • 25
  • 37
  • @Prachi...Thanks a lot.It works GREAT!. Why is this happening, any idea? – Akhil Jain Aug 28 '15 at 07:07
  • @AkhilJain no idea.... just come to know when i was stuck with similar issue.. better to user layout_gravity or relative layout...if you come across the real reason let me know :) – Prachi Aug 28 '15 at 07:26
  • @Prachi I like your answer. Only 1 line. +1 – M D Aug 28 '15 at 07:34
3
<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" >

remove android:layout_alignParentBottom="true" from above code and check.

Pavya
  • 6,015
  • 4
  • 29
  • 42
2

Ya, it happens with relativelayout. in its google docs clearly mention.

Note that you cannot have a circular dependency between the size of the RelativeLayout and the position of its children. For example, you cannot have a RelativeLayout whose height is set to WRAP_CONTENT and a child set to ALIGN_PARENT_BOTTOM

As you are working with dialog you have to use linearlayout as per your requirement. It will defiantly work for you.

further ideas may be not applicable for you, but I just mention it for reference

you can use java code for make fix size dialog, or you can set layout margin from layout params so that you can maintain responsiveness in different screen size. You also can one thing, get size of screen

DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
int height = metrics.heightPixels;
int width = metrics.widthPixels; 

than you can calculate your new dialog size and set it as @guobao's Answer

DisplayMetrics displayMetrics = new DisplayMetrics();
dialog.getWindow().getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
WindowManager.LayoutParams params = dialog.getWindow().getAttributes();
params.width = (new width);
params.height = (new height);

ask me if any further specification needed.

RBK
  • 2,481
  • 2
  • 30
  • 52
  • The same behaviour occurs with ConstraintLayout. Even, if ConstraintLayout is the root element of a ListViewItemLayout O.o – goemic Aug 27 '19 at 08:54
0

you can set this code in your dialog

DisplayMetrics displayMetrics = new DisplayMetrics();
dialog.getWindow().getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
WindowManager.LayoutParams params = dialog.getWindow().getAttributes();
params.width = (the width you want to set);
params.height = (the height you want to set);
leo
  • 204
  • 3
  • 15
  • thanks for your answer, think this through. How can possibly specify the exact height and width for content, it will vary on screen size and dimension. Also see Answer 5 in solutions already tried I have already implemented your solution with `WRAP_CONTENT` and got nowhere. – Akhil Jain Aug 28 '15 at 07:18
0

This was my approach when I faced the same problem.

I extended DialogFragment (from support.v4 lib)

And then inflate:

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());

    LayoutInflater inflater = getActivity().getLayoutInflater();
    final View view = inflater.inflate(R.layout.dialog_whatever, null);

    builder.setView(view);
    return builder.create();
}

This is the layout:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/rounded_corners"
    android:padding="20dp">

    <TextView
        android:id="@+id/confirm_dialog_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="16sp"
        android:textColor="@color/black"
        android:text="@string/are_you_sure_declining_whatever"/>

    <LinearLayout
        android:layout_marginTop="20dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="right|bottom"
        android:orientation="horizontal">

        <Button
            android:id="@+id/cancel_action"
            android:layout_marginRight="10dp"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:background="@color/transparent"
            android:text="@string/cancel"
            android:textColor="@color/accent_color"/>

        <Button
            android:id="@+id/confirm_action"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:background="@color/transparent"
            android:textColor="@color/accent_color"
            android:text="@string/reject"/>

    </LinearLayout>
</LinearLayout>

This approach also follows Material design guidelines!!

Hope this is of help to you

Best regards,

pedrovic90

pedrovic90
  • 13
  • 3