Android: Custom Dialog Has Wrong Dimensions
Solution 1:
try this
<?xml version="1.0" encoding="utf-8"?><LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:id="@+id/ll_element1"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_margin="5dip"android:orientation="vertical" ><EditTextandroid:layout_width="250dip"android:layout_height="wrap_content"android:hint="Element 1"android:lines="5" /><EditTextandroid:layout_width="250dip"android:layout_height="wrap_content"android:hint="Element 2"android:lines="5" /></LinearLayout>
in your activity
finalDialogdialog=newDialog(YourActivity.this);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.yourCustomdialog);
dialog.setCancelable(true);
dialog.show();
Solution 2:
Remove TextView and use android:hint
on EditText . Put Both EditText in One LinearLayout
with vertical Orientation.
Change android:layout_width of EditText as fill_parent
as well as its parent too.
Solution 3:
You have set the lines=5
in your EditText
, so it is taking the space. Use dp
instead of dip
, check this
Now how do you want it to display? Give some reference so that exact problem can be identified.
Solution 4:
None of these answers helped me, so here is my solutíon (maybe someone find it useful).
I had similar problem with NumberPicker
which was displayed in AlertDialog
. NumberPicker
had own layout which was defined in relative units ("match_parent"
, android:layout_weight
, ...).
Inflating layout without rootView
results in layout with default parameters so layout size was not as expected. Inflating layout with rootView
leads to IllegalStateException
: "The specified child already has a parent" when AlertDialog#create()
is invoked since create()
method attaches view to the internal dialog layout.
As described in How to make an alert dialog fill 90% of screen size? you can override default parameters like this:
AlertDialog dialog = builder.show();
WindowManager.LayoutParams params = new WindowManager.LayoutParams();
params.copyFrom(dialog.getWindow().getAttributes());
params.width = 300; // LayoutParams.MATCH_PARENT will match dialog_layout which can be stretched to fullwidth.params.height = 300;
dialog.getWindow().setAttributes(params);
Instead of using fixed width and height in pixels, you can create dialog_helper.xml
where you define some transparent view in relative units (android:id="@+id/dialog_dimension"
), from which you will later copy width and height. Then include this layout into the activity/fragment layout:
<?xml version="1.0" encoding="utf-8"?><RelativeLayout...><LinearLayout....>
...
</LinearLayout><includelayout="@layout/dialog_helper"/></RelativeLayout>
Since this view is contained in activity layout it has "measured" width and height which can be used as dialog parameters:
View dialogDim = findViewById(R.id.dialog_dimension);
params.width = dialogDim.getMeasuredWidth();
params.height = dialogDim.getMeasuredHeight();
Calculating relative size from dialog.getWindow().getAttributes()
would not work as it contains some negative value constants (MATCH_PARENT or something else). Therefore, if you want to calculate dialog size relative to display size use screen metrics.
Note, after screen rotate the same width and height will be used, therefore I will recommend to wrap your dialog into the DialogFragment
which is automatically recreated when device configuration is changed, so dialog will be created with updated dialogDim
according to current screen orientation. Here is a simple example How to create DialogFragment.
Previous example contains communication between activity and fragment (activity need to receive message about dialog event). This requires to define custom listener interface + properly override Fragment#onAttach()
method. All these steps are explained in following article: Cummunication between Fragment and Activity.
Solution 5:
Use builder and add width and height while showing
builder.show().getWindow().setLayout(600, 250);
Post a Comment for "Android: Custom Dialog Has Wrong Dimensions"