3

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();
    }
Vikram Singh
  • 1,420
  • 14
  • 19

2 Answers2

4

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.

blackgreen
  • 34,072
  • 23
  • 111
  • 129
Jitender Dev
  • 6,907
  • 2
  • 24
  • 35
  • 2
    I think he is looking for a more generic solution, that works on different devices.. – bofredo Oct 17 '13 at 15:34
  • I have already tried placing an image in background and it is solving the problem but I don't want to do that as it increase the size of my application. But the question is why it is depending on the width of my title textview and not any other layout or view... – Vikram Singh Oct 17 '13 at 15:39
  • Updated answer again. – Jitender Dev Oct 17 '13 at 16:59
  • Second half of your answer worked...thanks...+1 for information of dialog theme...:) – Vikram Singh Oct 18 '13 at 06:56
4
setContentView(R.layout.layout_name);
    getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.MATCH_PARENT);

this worked for me .

Saneesh
  • 1,796
  • 16
  • 23