2

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>
rickymarchiori
  • 103
  • 2
  • 13

2 Answers2

4

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

Community
  • 1
  • 1
Cob013
  • 1,057
  • 9
  • 22
  • The second is solved, but for the first one I tried to change both to wrap_content, but nothing seems to happen :/ Changing them in wrap_contet made the buttons overlap. Now it look like one button wide all the dialog. – rickymarchiori Jun 26 '13 at 21:27
  • It could be a little rough but.. why don't you try `android:paddingBottom="50dp"` in ` – Cob013 Jun 26 '13 at 21:33
  • It doesn't work. Even with paddingBottom, the dialog keeps that height – rickymarchiori Jun 26 '13 at 21:41
  • 1
    Your innermost vertical LinearLayouts are unnecessary. One vertical LinearLayout containing TextViews and a horizontal LinearLayout is all that is necessary to reproduce the original layout. – Bryan Herbst Jun 26 '13 at 23:04
0

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.

Bryan Herbst
  • 66,602
  • 10
  • 133
  • 120
  • I have change layout_width and layout_height like you said, but nothing changed :( – rickymarchiori Jun 26 '13 at 21:21
  • Is this being used in a DialogFragment? – Bryan Herbst Jun 26 '13 at 21:29
  • I have my dialog_show XML, then in my java I use Dialog dialog = new Dialog(this); dialog.setContentView(R.layout.dialog_show); and dialog.show(); – rickymarchiori Jun 26 '13 at 21:33
  • 1
    Is there a particular reason you are using a `RelativeLayout`? It looks like a `LinearLayout` might suffice. If you do switch, do you see the same behavior? – Bryan Herbst Jun 26 '13 at 23:02
  • Can't believe it was really that simple. Using LinearLayout works! I used RelativeLayout for ToRightOf attributes, but I've just figured out it's better using nested LinearLayout with vertical and horizontal. Thx! – rickymarchiori Jun 28 '13 at 11:34