Noffls
Noffls

Reputation: 5427

Android: Custom Dialog has wrong dimensions

I'm trying to display a dialog with a custom view. The Layout is pretty simple; basicly there are two LinearLayouts; every Layout with a TextView and an EditText.

The Layout i'm using is this:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">
  <LinearLayout
    android:orientation="vertical"
    android:layout_margin="5dip"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/ll_element1">    
    <TextView
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="Element 1"/>
    <EditText
      android:layout_width="250dip"
      android:layout_height="wrap_content"
      android:lines="5"/>
  </LinearLayout>
  <LinearLayout
    android:orientation="vertical"
    android:layout_margin="5dip"
    android:layout_width="wrap_content"
    android:layout_below="@id/ll_element1"
    android:layout_height="wrap_content">    
    <TextView
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="Element 2"/>
    <EditText
      android:layout_width="250dip"
      android:layout_height="wrap_content"
      android:lines="5"/>
  </LinearLayout>
</RelativeLayout>

But if i set it as view of a dialog the looks like this: Wrong sized dialog

My Code to show the Dialog is this:

var view = this.LayoutInflater.Inflate(Resource.Layout.Custom_Dialog, null);

var dialog = new AlertDialog.Builder(this)
            .SetView(view)
            .Create();

dialog.Show();

In my opinion width of the Dialog should be smaller so that the Layout fills the dialog. I've tried several things:

So, what do i have to do to get a correctly sized dialog?

edit Updated the Picture

Upvotes: 0

Views: 4216

Answers (5)

Smitha
Smitha

Reputation: 575

Use builder and add width and height while showing

builder.show().getWindow().setLayout(600, 250);

Upvotes: -1

user4413257
user4413257

Reputation:

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>
    <include layout="@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.

Upvotes: 0

AITAALI_ABDERRAHMANE
AITAALI_ABDERRAHMANE

Reputation: 2519

try this

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns: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" >

    <EditText
        android:layout_width="250dip"
        android:layout_height="wrap_content"
        android:hint="Element 1"
        android:lines="5" />

    <EditText
        android:layout_width="250dip"
        android:layout_height="wrap_content"
        android:hint="Element 2"
        android:lines="5" />

</LinearLayout>

in your activity

final Dialog dialog = new Dialog(YourActivity.this);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.yourCustomdialog);
dialog.setCancelable(true);
dialog.show();

Upvotes: 2

Vineet Shukla
Vineet Shukla

Reputation: 24031

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.

Upvotes: 1

Chirag
Chirag

Reputation: 56925

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.

Upvotes: 1

Related Questions