10

I have created an android dialog using an XML layout file like so:

  final Dialog dialog = new Dialog(this);
        dialog.setContentView(R.layout.enabledialog);
        dialog.setTitle("Define message keys and setup");

        Switch locationSwitch = (Switch) dialog.findViewById(R.id.locationSwitch);
        Switch blareSwitch = (Switch) dialog.findViewById(R.id.blareSwitch);

        final EditText locationEdit = (EditText) dialog.findViewById(R.id.locationEdit);
        final EditText blareEdit = (EditText) dialog.findViewById(R.id.blareEdit);

        WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
        lp.copyFrom(dialog.getWindow().getAttributes());
        lp.width = WindowManager.LayoutParams.MATCH_PARENT;
        lp.height = WindowManager.LayoutParams.MATCH_PARENT;

        dialog.show();

        dialog.getWindow().setAttributes(lp);

However, my XML file's contents only take up half of the screen, so when I display the dialog, I am showing extra space, which doesn't look nice:

HERE IS A SKETCH OF HOW THE DIALOG LOOKS (R.layout.enabledialog):

enter image description here

How do I trim this extra space? In android studio, the layout editor assumes the XML file is to take up the whole screen, but really, I only want a small popup window.

Thanks,

Ruchir


EDIT: Here is my layout resource file:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#1dd1e9">


    <Switch
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/locationSwitch"
        android:layout_marginTop="46dp"
        android:checked="false"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="Blah"
        android:id="@+id/textView"
        android:textSize="30sp"
        android:layout_alignTop="@+id/locationSwitch"
        android:layout_centerHorizontal="true"
        />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:text="Blah Blah Blah"
        android:id="@+id/textView2"
        android:gravity="center"
        android:textSize="20sp"
        android:layout_below="@+id/locationSwitch"
        android:layout_centerHorizontal="true"
        />

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/locationEdit"
        android:layout_below="@+id/textView2"
        android:layout_alignLeft="@+id/textView"
        android:layout_alignStart="@+id/textView"
        android:layout_alignRight="@+id/textView"
        android:layout_alignEnd="@+id/textView"
        android:singleLine = "true"
        android:imeOptions="actionDone" />

    <Switch
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/blareSwitch"
        android:layout_marginTop="40dp"
        android:checked="false"
        android:layout_below="@+id/locationEdit"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="Blah"
        android:id="@+id/textView3"
        android:textSize="30sp"
        android:layout_alignBottom="@+id/blareSwitch"
        android:layout_centerHorizontal="true" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:text="Blah Blah Blah"
        android:id="@+id/textView4"
        android:textSize="20sp"
        android:layout_centerVertical="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/blareEdit"
        android:layout_below="@+id/textView4"
        android:layout_alignLeft="@+id/locationEdit"
        android:layout_alignStart="@+id/locationEdit"
        android:layout_alignRight="@+id/locationEdit"
        android:layout_alignEnd="@+id/locationEdit"
        android:singleLine = "true"
        android:imeOptions="actionDone" />
</RelativeLayout>
3
  • Please post the layout resource file. You might also wish to change MATCH_PARENT to WRAP_CONTENT in your Java code. May 29, 2016 at 19:36
  • @CommonsWare Edited :) May 29, 2016 at 19:39
  • @CommonsWare I actually had it at MATCH_PARENT because when it was WRAP_CONTENT, the content was overlapping and not fitting properly. May 29, 2016 at 19:40

8 Answers 8

3

Try changing fill_parent to wrap_content in your root RelativeLayout, as well as changing MATCH_PARENT to WRAP_CONTENT in your Java code (or get rid of the whole setAttributes() bit).

I actually had it at MATCH_PARENT because when it was WRAP_CONTENT, the content was overlapping and not fitting properly

Then that's a separate problem that you would need to fix. Start by getting the dialog size to be roughly what you want. Then, use some inspection tool (the new one in AS 2.2, Hierarchy View, uiautomatorviewer, etc.) to start figuring out how to fix your layout to avoid your overlaps.

2
  • Okay, I have changed all the MATCH_PARENT'S to WRAP_CONTENT's, and the overlapping is continuing. How do I get/use those inspection tools you talked about? Thanks May 29, 2016 at 19:53
  • @RuchirBaronia: uiautomatorviewer is a command-line tool in your SDK. Hierarchy View is in the Android Device Monitor (available from Tools > Android in the Android Studio main menu). Both are covered in the docs, at least lightly. Basically, they give you a look at the actual content of your UI, loaded from the device, so you can visualize the rules as they are actually being applied. May 29, 2016 at 20:11
3
+25

Try changing yout layout parameters height to:

lp.height = WindowManager.LayoutParams.WRAP_CONTENT;

2

I usually set the LayoutParams programmatically. I think you can try DialogFragment with below code in onResume:

/**
 * Setup position of dialog 
 */
protected void setupDialogPosition() {
    // setup the dialog
    WindowManager.LayoutParams p = getDialog().getWindow().getAttributes();
    p.width = ViewGroup.LayoutParams.MATCH_PARENT;
    p.y = 0;
    // change height of dailog
    if (mHeight == ViewGroup.LayoutParams.MATCH_PARENT) {
        p.height = mScreenHeight - p.y;
        if (isStatusBarVisible()) {
            p.height -= getStatusBarHeight();
        }
        } else {
        p.height = ViewGroup.LayoutParams.WRAP_CONTENT;
    }
    getDialog().getWindow().setGravity(mGravity);
    getDialog().getWindow().setAttributes(p);
}
2

Change RelativeLayout to LinearLayout:

<LinearLayout
    android:layout_height="match_parent"
    android:layout_width="wrap_content"
</LinearLayout>
2

In your Relative Layout specify the layout_height and layout_width , and do not set it to fill_parent . Example:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="250dp"
android:layout_height="wrap_content">

<TextView
    android:id="@+id/customdia_text"
    android:layout_marginTop="20dp"
    android:layout_width="wrap_content"
    android:layout_height="40dp"
    android:layout_centerHorizontal="true"
    android:text="hello"
    />
<Button
    android:id="@+id/custdia_btn"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Ok"
    android:layout_marginBottom="30dp"
    android:layout_marginTop="20dp"
    android:layout_below="@+id/customdia_text"
    android:layout_centerHorizontal="true"/>

</RelativeLayout>
2

Convert directly from resource value:

int width = getResources().getDimensionPixelSize(R.dimen.popup_width);
int height = getResources().getDimensionPixelSize(R.dimen.popup_height);        
getDialog().getWindow().setLayout(width, height);

Now match_parent in your layout for the dialog:

android:layout_width="match_parent"
android:layout_height="match_parent"

android:layout_width="match_parent"
android:layout_height="match_parent"

Hope this will help you.

2

Create custom Dialog as follows

public class MyDialog extends Dialog {

    @Override
    public void onAttachedToWindow() {
        super.onAttachedToWindow();
        WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
        lp.width = WindowManager.LayoutParams.MATCH_PARENT;
        lp.height = WindowManager.LayoutParams.WRAP_CONTENT;

        getWindow().setAttributes(lp);
    }

    public MyDialog(Context context) {
        super(context);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog);
    }
}

look at the override method onAttachedToWindow(), here is your trick.this method gets called before onCreate() method. so you can override windows property(width and height) before your dialog gets created.

call this Dialog from your Activity as follows

new MyDialog(ActivityContext).show();

also do some modification in dialog.xml file as well

<?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="wrap_content"
    android:gravity="center"
    android:orientation="vertical">

    <Switch
        android:id="@+id/locationSwitch"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:layout_marginTop="46dp"
        android:checked="false" />

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignTop="@+id/locationSwitch"
        android:layout_centerHorizontal="true"
        android:text="Blah"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:textSize="30sp" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/locationSwitch"
        android:layout_centerHorizontal="true"
        android:gravity="center"
        android:text="Blah Blah Blah"
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:textSize="20sp" />

    <EditText
        android:id="@+id/locationEdit"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignEnd="@+id/textView"
        android:layout_alignLeft="@+id/textView"
        android:layout_alignRight="@+id/textView"
        android:layout_alignStart="@+id/textView"
        android:layout_below="@+id/textView2"
        android:layout_marginBottom="50dp"
        android:imeOptions="actionDone"
        android:singleLine="true" />

</LinearLayout>

edit layout file if you want any extra customization

1

change the Main RelativeLayout size as per requirement in xml file. Specify the Height what you need like bellow

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="100dp"
    android:background="#1dd1e9">

<Switch
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/locationSwitch"
    android:layout_marginTop="46dp"
    android:checked="false"
    android:layout_alignParentTop="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:text="Blah"
    android:id="@+id/textView"
    android:textSize="30sp"
    android:layout_alignTop="@+id/locationSwitch"
    android:layout_centerHorizontal="true"
    />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceSmall"
    android:text="Blah Blah Blah"
    android:id="@+id/textView2"
    android:gravity="center"
    android:textSize="20sp"
    android:layout_below="@+id/locationSwitch"
    android:layout_centerHorizontal="true"
    />

<EditText
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/locationEdit"
    android:layout_below="@+id/textView2"
    android:layout_alignLeft="@+id/textView"
    android:layout_alignStart="@+id/textView"
    android:layout_alignRight="@+id/textView"
    android:layout_alignEnd="@+id/textView"
    android:singleLine = "true"
    android:imeOptions="actionDone" />

<Switch
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/blareSwitch"
    android:layout_marginTop="40dp"
    android:checked="false"
    android:layout_below="@+id/locationEdit"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:text="Blah"
    android:id="@+id/textView3"
    android:textSize="30sp"
    android:layout_alignBottom="@+id/blareSwitch"
    android:layout_centerHorizontal="true" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceSmall"
    android:text="Blah Blah Blah"
    android:id="@+id/textView4"
    android:textSize="20sp"
    android:layout_centerVertical="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true" />

<EditText
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/blareEdit"
    android:layout_below="@+id/textView4"
    android:layout_alignLeft="@+id/locationEdit"
    android:layout_alignStart="@+id/locationEdit"
    android:layout_alignRight="@+id/locationEdit"
    android:layout_alignEnd="@+id/locationEdit"
    android:singleLine = "true"
    android:imeOptions="actionDone" />
</RelativeLayout>

change the Height and width as per Requirements and its not required to setAttributes(lp); in Java . if you are setting make sure that you consider the xml RelativeLayout Height and Width

3
  • How do I know which height? Jun 10, 2016 at 4:25
  • plz check the code in parent RelativeLaout i have given Height as 100dp use that and change as per requirement. Jun 10, 2016 at 4:49
  • change the parent layout height check the code and do trail and error.. Jun 16, 2016 at 13:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.