Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to specify correct dialog size in XML layout file for Android dialog?

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>
like image 796
Ruchir Baronia Avatar asked May 29 '16 19:05

Ruchir Baronia


People also ask

How do I make alert dialog fill 90% of screen size?

Builder adb = new AlertDialog. Builder(this); Dialog d = adb. setView(new View(this)). create(); // (That new View is just there to have something inside the dialog that can grow big enough to cover the whole screen.)

What is custom dialogue in android?

The custom dialog uses DIALOG to create custom alert in android studio. Dialog display a small window i.e a popup which draws the user attention over the activity before they continue moving forward. The dialog appears over the current window and display the content defined in it.


1 Answers

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.

like image 138
CommonsWare Avatar answered Oct 06 '22 00:10

CommonsWare