Search code examples
androidlayoutdialog

Dialog is shrinking even with all parent layout_width set as fill_parent


Another weird behavior encountered here. This time my dialog is shrinking width wise inspite of setting all parent layout's width as fill_parent. Here is the image...enter image description here

I have tried making it an activity with theme set as Dialog in manifest. Still it is behaving same. However it becomes normal as soon as I set layout_width for first TextView "User Agreement" as fill_parent. But I don't understand this behavior as it should not depend on TextView's width for its own width. Please tell me if there is any other efficient way to deal with these type of situations. My code for layout is below:

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

    <TextView
        android:id="@+id/textView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        android:text="User Agreement"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textColor="#B80303" />

    <View
        android:layout_width="fill_parent"
        android:layout_height="3dip"
        android:background="?android:attr/listDivider" >
    </View>

    <LinearLayout
        android:id="@+id/linearLayout4"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >

        <CheckBox
            android:id="@+id/checkBoxForTermsId"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:textColor="#7B4302"
            android:text="I Agree The Terms &amp; Conditions" >
        </CheckBox>

This is not whole code for layout because I don't think it is needed...

The code for showing Dialog is as follows:

private void showAgreementBox() {
        // TODO Auto-generated method stub
        final Dialog dialog = new Dialog(Launcher.this);
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        dialog.setContentView(R.layout.useragreement);
        dialog.setTitle("User Agreement");
        dialog.setCancelable(false);
        final TextView userAg = (TextView) dialog.findViewById(R.id.textViewOfUserAg);
        final CheckBox checkUserAg = (CheckBox) dialog.findViewById(R.id.checkBoxForTermsId);
        final Button continueB = (Button) dialog.findViewById(R.id.continueB);
        checkUserAg.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                if (checkUserAg.isChecked() == true) {
                    continueB.setEnabled(true);
                } else {
                    continueB.setEnabled(false);
                }
            }
        });

        continueB.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                dialog.dismiss();
                //checkForTrialPeriod(isUserRegisterd);
            }
        });

        Button cancelB = (Button) dialog.findViewById(R.id.cancelB);
        cancelB.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                finish();
            }
        });
        dialog.show();
    }

Solution

  • Fix the width of your parent layout

     android:layout_width="400dp"
    

    fill_parent works for Activity xml but for dialogs you have to set the width.

    If you are looking for a general solution set an image as background in the parent layout just like we place images in different drawable folders to support multiple screens.

    You can also set width as full screen using

    dialog.getWindow().getAttributes().width = LayoutParams.FILL_PARENT;
    

    Why this behavior is shown by dialog (quoting from this post by Dianne Hackborn)

    The dialog theme, as part of its nature, sets the top-level window layout to be WRAP_CONTENT. You could try manually setting the Window layout width and height to FILL_PARENT [...]

    Also took reference from this answer.