rickymarchiori
rickymarchiori

Reputation: 103

Custom dialog height to match content

This is my custom dialog, there are some textboxs and two buttons. I'm using a RelativeLayout on it.

I'd like the dialog size to match the content, not all that blank space under there. I don't want to use explicit pixel heights (it's not good practice).

Another thing, there's a way to have some space between every view?

enter image description here

This is my XML

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

<TextView
    android:id="@+id/txt_desc"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textSize="8pt"
    android:textStyle="italic" />

<TextView
    android:id="@+id/txt_date"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"         
    android:layout_marginLeft="10dp"
    android:layout_below="@+id/txt_desc"
    android:textSize="8pt" />

<TextView
    android:id="@+id/txt_place"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"         
    android:layout_marginLeft="10dp"
    android:layout_below="@+id/txt_date"
    android:textSize="8pt" />

<Button
    android:id="@+id/btn_close"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:layout_below="@+id/txt_place"
    android:layout_toRightOf="@+id/view"
    android:text="@string/btn_close" />

<View
    android:id="@+id/view"
    android:layout_width="0dp"
    android:layout_height="1dp"
    android:layout_centerHorizontal="true" />

<Button
    android:id="@+id/btn_edit"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/txt_place"
    android:layout_toLeftOf="@+id/view"
    android:drawableLeft="@android:drawable/ic_menu_edit"
    android:text="@string/btn_edit" />

</RelativeLayout>

Upvotes: 2

Views: 6265

Answers (2)

Cob013
Cob013

Reputation: 1067

First question

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

Second question

Plus use android:marginTop="5dp" if you want to separate a View from Top for 5 dp. marginLeft|Right|Bottom are also available.

<TextView
    android:id="@+id/txt_place"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"         
    android:layout_marginLeft="10dp"
    android:layout_marginTop="5dp" <-----------------
    android:layout_below="@+id/txt_date"
    android:textSize="8pt" />

By the way, if I were you, I'd nest some layouts to make order. You can have a general Relative Layout, then 2 linear layouts: one horizontal for TextViews and one vertical for Buttons. You can definely play with your editor and try to make the best appearance for your dialog.

EDIT

Try this one:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#0000"
    android:orientation="vertical"
    android:paddingBottom="50dp" >

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

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

            <TextView
                android:id="@+id/txt_desc"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="prova"
                android:textSize="8pt"
                android:textStyle="italic" />

            <TextView
                android:id="@+id/txt_date"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="10dp"
                android:text="prova"
                android:textSize="8pt" />

            <TextView
                android:id="@+id/txt_place"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="10dp"
                android:text="prova"
                android:textSize="8pt" />
        </LinearLayout>

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

            <Button
                android:id="@+id/btn_edit"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:drawableLeft="@android:drawable/ic_menu_edit"
                android:text="btn_edit" />

            <Button
                android:id="@+id/btn_close"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="btn_close" />

            <View
                android:id="@+id/view"
                android:layout_width="0dp"
                android:layout_height="1dp" />
        </LinearLayout>
    </LinearLayout>

</LinearLayout>

I've hard-coded the texts. The main idea is to use a big container layout in background, which is trasparent (background=#0000) and in top of that a layout in wrap_content containing your Views.

If this solution doesn't resolve your problem, I think you could edit height directly in your code. Try to relate to this question

Upvotes: 4

Bryan Herbst
Bryan Herbst

Reputation: 67259

Set your relative layout's height to wrap_content.

For example:

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

    <!-- More XML -->

</RelativeLayout>

Also be aware that fill_parent is deprecated for widths and heights.

You can add either margin (extra space outside a view) or padding (extra space inside a view) with android:layout_margin or android:padding respectively. There are also variants such as layout_marginTop that only apply to a specific side of the view.

Upvotes: 0

Related Questions