Android dialog width

80,801

Solution 1

I had the same problem.

I used following code to make dialog fill_parent and it worked fine.

public class SharePost extends Dialog
{
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.adaptor_contentsharepost);

        LayoutParams params = getWindow().getAttributes();
        params.height = LayoutParams.FILL_PARENT;
        getWindow().setAttributes((android.view.WindowManager.LayoutParams) params);
    }
}

layout

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:id="@+id/dialogWidth"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    contents here

</LinearLayout>

Solution 2

I'm using:

getWindow().setLayout(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);

Solution 3

Set a minimum width at the top most layout.

android:minWidth="300dp"

For example:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"
  xmlns:android="http://schemas.android.com/apk/res/android" android:minWidth="300dp">

<!-- Put remaining contents here -->

</LinearLayout>

Solution 4

As Matthias points at out How can I get a Dialog style activity window to fill the screen? UMAR's solution works, but only if the window attributes are set AFTER setContentView() is called.

Solution 5

Try this. It works for me.

    dialog = new Dialog(Activity.this);
    dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
    dialog.setContentView(R.layout.feedback_popup);
    dialog.setCancelable(false);
    dialog.setCanceledOnTouchOutside(false);

    WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
    lp.copyFrom(dialog.getWindow().getAttributes());
    lp.width = WindowManager.LayoutParams.MATCH_PARENT;
    lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
    lp.gravity = Gravity.CENTER;

    dialog.getWindow().setAttributes(lp);
Share:
80,801
Admin
Author by

Admin

Updated on March 31, 2020

Comments

  • Admin
    Admin about 4 years

    I can't seem to control the dialog width. I have a simple layout like so`

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout 
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:theme="@android:style/Theme.Dialog"> 
    
        <ScrollView 
            android:orientation="vertical"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent">
            <LinearLayout     android:orientation="vertical"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent">
    
            <TextView 
                android:id="@+id/name_prompt_view"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:text="@string/name_prompt"
                android:padding="10dip"/>
    
            <EditText 
                android:id="@+id/name_inp" 
                android:layout_width="fill_parent" 
                android:layout_height="wrap_content" 
                android:lines="1"
                android:maxLines="1"
                android:maxLength="48"
                android:inputType="text" />
    
            <TextView 
                android:id="@+id/t1_prompt_view"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:text="@string/t1_prompt"
                android:padding="10dip"/>
    
            <Spinner 
                android:id="@+id/t1_inp" 
                android:layout_width="fill_parent" 
                android:layout_height="wrap_content" 
                android:lines="1"
                android:maxLines="1"
                android:maxLength="48"
                android:inputType="text"
                android:singleLine="true"
                android:layout_weight="1"
                android:entries= "@array/t1_allowed_values" />
    
             </LinearLayout>
    
        </ScrollView>
    
    </LinearLayout>
    

    for some reason the dialog is only wide enough for the text input field about 11 chars wide. How do I make the dialog width fill the screen?

  • Matt K
    Matt K almost 13 years
    for some reason the xml layout parameters I set for width/height were not working on my dialog but this worked great! +1
  • Luis A. Florit
    Luis A. Florit about 11 years
    Actually, as it is stated here [stackoverflow.com/questions/4406804/…, it works only after .show() (at least for me).
  • Leo supports Monica Cellio
    Leo supports Monica Cellio almost 11 years
    this is the answer to another question... deprecated doesn't mean disabled (yet)
  • Miguel Rivero
    Miguel Rivero almost 11 years
    I agree, deprecated doesn't mean disabled, but it means that even if you can keep using it you might not do it, as is likely to be removed in a next version.
  • Leo supports Monica Cellio
    Leo supports Monica Cellio almost 11 years
    still not the answer to this question in particular. Since FILL_PARENT still works, you can't expect to solve this question by replacing by a preferred synonym.
  • Brian White
    Brian White over 10 years
    This worked for me but only if I reset the params after calling show().
  • R Earle Harris
    R Earle Harris over 10 years
    If it doesn't work in the outermost layout, as it didn't for me, it works by setting it in one of the layout elements, like a title bar.
  • William T. Mallard
    William T. Mallard about 9 years
    Better solution for me as I wanted to keep the definition entirely in XML and not have to fix it up in the code. Thanks for pointing this attribute out!
  • Machado
    Machado almost 9 years
    FILL_PARENT is now deprecated.
  • Fred
    Fred over 8 years
    fill_parent is now deprecated in favor of match_parent.
  • Dennis van Dalen
    Dennis van Dalen over 8 years
    Should use MATCH_PARENT instead
  • maruti060385
    maruti060385 over 7 years
    If you do this in onActivityCreated if you are using DialogFragment will work.<br> getDialog().getWindow().getAttributes().width = (int) getResources().getDimension(R.dimen.uid_alert_dialog_width); should also work
  • BMU
    BMU over 4 years
    This worked for me with Kotlin and ConstraintLayout
  • Black4Guy
    Black4Guy over 2 years
    So it will be like: dialog.getWindow().setLayout(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);