samedi 11 juin 2016

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>

Aucun commentaire:

Enregistrer un commentaire