Asym
Asym

Reputation: 1938

Android - Set custom dialog layout height to wrap_content

I am using a custom layout to show a dialog. Here's the layout:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    >
    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            >
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textSize="20sp"
                android:textStyle="bold"
                android:id="@+id/dialog_title_tv"
                android:text="Dialog Title"
                />
        <android.support.design.widget.TextInputLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:padding="16dp"
            >
            <android.support.design.widget.TextInputEditText
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:hint="Enter Phone Number"
                />
        </android.support.design.widget.TextInputLayout>
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:padding="8dp"
            >
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Select Country: "
                android:padding="8dp"
                />
            <Spinner
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:padding="8dp"
                android:id="@+id/country_spinner"
                />
        </LinearLayout>
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:padding="8dp"
            >
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Select City: "
                android:padding="8dp"
                />
            <Spinner
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:padding="8dp"
                android:id="@+id/city_spinner"
                />
        </LinearLayout>
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:padding="8dp"
            >
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Select Area: "
                android:padding="8dp"
                />
            <Spinner
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:padding="8dp"
                android:id="@+id/area_spinner"
                />
        </LinearLayout>
        </LinearLayout>
    </ScrollView>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:gravity="right"
        >
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Done"
            />
    </LinearLayout>
</LinearLayout> 

It shows like this:

enter image description here

However, I want to set its height as WRAP_CONTENT. Currently, it's taking up the whole screen. How can I do this? What am I doing wrong? Thanks in advance

Upvotes: 1

Views: 2370

Answers (3)

Baahubali
Baahubali

Reputation: 163

You may want to try these:

  • Set android:layout_weight="1" inside your ScrollView
  • Set android:layout_height to some static height (in dp).
  • Change parent from LinearLayout to RelativeLayout.

Upvotes: 0

Mohammed Atif
Mohammed Atif

Reputation: 4513

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="#1a000000"
    android:layout_margin="16dp"
    android:padding="16dp">
    <TextView
        android:id="@+id/dialog_header"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="20sp"
        android:text="My Dialog"
        android:fontFamily="sans-serif-condensed"
        android:textStyle="bold"/>
    <android.support.design.widget.TextInputLayout
        android:id="@+id/text_input_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/dialog_header"
        android:layout_marginTop="8dp">
        <android.support.design.widget.TextInputEditText
            android:id="@+id/text_input_edit_text"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textSize="16sp"
            android:hint="Enter phone number"/>
    </android.support.design.widget.TextInputLayout>
    <TextView
        android:id="@+id/select_country_label"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Select Country:"
        android:layout_toStartOf="@+id/country_spinner"
        android:layout_alignBottom="@+id/country_spinner"
        android:textSize="14sp"
        android:fontFamily="sans-serif-condensed"
        android:textStyle="bold"/>
    <Spinner
        android:id="@+id/country_spinner"
        android:layout_width="150dp"
        android:layout_height="wrap_content"
        android:layout_below="@+id/text_input_layout"
        android:layout_alignParentEnd="true"
        android:layout_marginTop="8dp"/>
    <TextView
        android:id="@+id/select_city_label"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Select City:"
        android:layout_toStartOf="@+id/city_spinner"
        android:layout_alignBottom="@+id/city_spinner"
        android:textSize="14sp"
        android:fontFamily="sans-serif-condensed"
        android:textStyle="bold"/>
    <Spinner
        android:id="@+id/city_spinner"
        android:layout_width="150dp"
        android:layout_height="wrap_content"
        android:prompt="Select One"
        android:layout_below="@+id/country_spinner"
        android:layout_alignParentEnd="true"
        android:layout_marginTop="8dp"/>
    <TextView
        android:id="@+id/select_area_label"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Select Area:"
        android:layout_toStartOf="@+id/area_spinner"
        android:layout_alignBottom="@+id/area_spinner"
        android:textSize="14sp"
        android:fontFamily="sans-serif-condensed"
        android:textStyle="bold"/>
    <Spinner
        android:id="@+id/area_spinner"
        android:layout_width="150dp"
        android:layout_height="wrap_content"
        android:prompt="Select One"
        android:layout_below="@+id/city_spinner"
        android:layout_alignParentEnd="true"
        android:layout_marginTop="8dp"/>

    <TextView
        android:id="@+id/done"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="done"
        android:textAllCaps="true"
        android:layout_below="@+id/area_spinner"
        android:layout_alignParentEnd="true"
        android:layout_marginTop="24dp"
        android:textSize="14sp"
        android:paddingTop="16dp"
        android:paddingBottom="16dp"
        android:textColor="#00f"/>
    <TextView
        android:id="@+id/cancel"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="cancel"
        android:textAllCaps="true"
        android:layout_below="@+id/area_spinner"
        android:layout_toStartOf="@+id/done"
        android:layout_marginTop="24dp"
        android:layout_marginEnd="24dp"
        android:textSize="14sp"
        android:paddingTop="16dp"
        android:paddingBottom="16dp"
        android:textColor="#00f"/>

</RelativeLayout>

Just copy paste the above XML code into your custom dialog layout.

Though you have already marked an answer as correct, I am posting this because of the following reasons

  1. It has no nesting of views, which will increase the efficiency of your code.
  2. It follows the Android Design Guidelines of building a Dialog Box
  3. I feel, in stack overflow, providing an optimal solution instead of correcting bugs in existing solution is much more important.

P.S: This is a non scroll view version of the dialog box. Since the content is less. Please comment if you need scrollable version too. It will have slightly different implementation which in turn will have slightly different output.

Upvotes: 2

rafsanahmad007
rafsanahmad007

Reputation: 23881

try this layout code:

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            <TextView
                android:id="@+id/dialog_title_tv"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Dialog Title"
                android:textSize="20sp"
                android:textStyle="bold" />

            <android.support.design.widget.TextInputLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:padding="16dp">

                <android.support.design.widget.TextInputEditText
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:hint="Enter Phone Number" />
            </android.support.design.widget.TextInputLayout>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal"
                android:padding="8dp">

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:padding="8dp"
                    android:text="Select Country: " />

                <Spinner
                    android:id="@+id/country_spinner"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:padding="8dp" />
            </LinearLayout>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal"
                android:padding="8dp">

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:padding="8dp"
                    android:text="Select City: " />

                <Spinner
                    android:id="@+id/city_spinner"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:padding="8dp" />
            </LinearLayout>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal"
                android:padding="8dp">

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:padding="8dp"
                    android:text="Select Area: " />

                <Spinner
                    android:id="@+id/area_spinner"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:padding="8dp" />
            </LinearLayout>
        </LinearLayout>
    </ScrollView>

    <RelativeLayout
        android:id="@+id/AddtoCart"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom">

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:text="Done" />

    </RelativeLayout>
</LinearLayout>

Upvotes: 4

Related Questions