Android dialog width

AndroidDialogWidth

Android Problem Overview


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?

Android Solutions


Solution 1 - Android

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 - Android

I'm using:

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

Solution 3 - Android

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 - Android

As Matthias points at out https://stackoverflow.com/questions/1362723/how-can-i-get-a-dialog-style-activity-window-to-fill-the-screen/1693716#1693716 UMAR's solution works, but only if the window attributes are set AFTER setContentView() is called.

Solution 5 - Android

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);

Solution 6 - Android

Since API 8 FILL_PARENT is deprecated. Use MATCH_PARENT instead.

params.height = LayoutParams.MATCH_PARENT;

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionSolidView Question on Stackoverflow
Solution 1 - AndroidUMAR-MOBITSOLUTIONSView Answer on Stackoverflow
Solution 2 - Androidhector6872View Answer on Stackoverflow
Solution 3 - Androidroy mathewView Answer on Stackoverflow
Solution 4 - AndroidAlex PretzlavView Answer on Stackoverflow
Solution 5 - AndroidPranav MittalView Answer on Stackoverflow
Solution 6 - AndroidMiguel RiveroView Answer on Stackoverflow