Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android custom dialog height

Tags:

android

dialog

I'm learning about Android dialogs and I'm confused about what determines their height. If I use this XML for my dialog layout . . .

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content">
  <Button
     android:id="@+id/AButton"
     android:layout_height="wrap_content"
     android:layout_width="wrap_content"
     android:layout_alignParentRight="true"
     android:layout_marginLeft="10px"
     android:text="Click Me to Dismiss"
  /> 
  <TextView android:id="@+id/text"
    android:layout_width="wrap_content"
    android:layout_height="fill_parent"
    android:layout_toLeftOf="@id/AButton"
    android:text="Click this button: " 
    />
   <ImageView
    android:id="@+id/an_image"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="42px"
    android:src="@drawable/screw50">
  </ImageView>      
  />

I get this dialog . . .

alt text

But Developer.Android.com says that...

"WRAP_CONTENT, which means that the view wants to be just big enough to enclose its content (plus padding)"

... so why is the dialog so much higher than it needs to be?

If I replace the layout height with

  android:layout_height="200px"

it makes a properly short dialog but I don't want to use explicit pixel heights (it's not good practice). How do I make the dialog be just big enough for its content?

like image 343
Peter Nelson Avatar asked Dec 01 '10 14:12

Peter Nelson


3 Answers

You have the height of your TextView to fill_parent. Is it possible that this, combined with the height of the RelativeLayout to wrap_content is pushing its height to the maximum size? What happens if you change the TextView height to wrap_content?

To add, the Android SDK says that children of a RelativeLayout can not have a dependency on the Layout, such as align_parent_bottom. I assume that fill_parent is a dependency and thus is breaking the layout.

like image 84
AndrewKS Avatar answered Oct 13 '22 11:10

AndrewKS


Hmm that's strange. I use <LinearLayout/>

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
                  android:layout_width="fill_parent"
                  android:layout_height="fill_parent"
                  android:orientation="vertical"
                  android:background="@android:color/white">
<TextView android:id="@+id/message"
              android:padding="5dp"
              android:layout_width="fill_parent"
              android:layout_height="wrap_content"
              android:layout_marginBottom="10dp"/>
    ...
    </LinearLayout>

And it wraps my content just fine.

like image 36
Amir Raminfar Avatar answered Oct 13 '22 11:10

Amir Raminfar


I've been working on the same problem the last 2 days. Finaly it work properly using some attributes in the style of my dialog.

here's the style i'm using:

<style name="SearchFieldSetterDialog" parent="@android:style/Theme.Dialog">
    <item name="android:windowIsFloating">true</item>
    <item name="android:windowNoTitle">true</item>
    <item name="android:gravity">bottom</item>
</style>

the key attriubutes are:

    <item name="android:windowIsFloating">true</item>
    <item name="android:windowNoTitle">true</item>

the first one, i guess, needs to say to the windowManager to put Frames around the dialog and the second one to take out the space reserved for the dialog title.

with this style and the following layout i finally had it working:

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content">

...

</LinearLayout>

my problem with ths solution is that with the android:windowIsFloating attriubute setted as true and earlyer platform then 3.0 i can't find a way to make the dialog witdh to fill the screen even if the layout_width is setted as fill_parent

hope this will help someone... and sry for my bad english

like image 20
Mario Lenci Avatar answered Oct 13 '22 10:10

Mario Lenci