Android custom dialog height

13,512

Solution 1

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.

Solution 2

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.

Solution 3

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

Share:
13,512
Peter Nelson
Author by

Peter Nelson

Updated on July 19, 2022

Comments

  • Peter Nelson
    Peter Nelson almost 2 years

    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?

  • Peter Nelson
    Peter Nelson over 13 years
    Yes, I agree LinearLayout doesn't have this problem. But I wanted to use RelativeLayout because of the control it gives me over placement. Is there some restriction or gotcha on RelativeLayout? (I should mention that I'm using Android 2.2 and this problem is happening in emulation and on the actual phone.)
  • Amir Raminfar
    Amir Raminfar over 13 years
    Why not use a RelativeLayout inside a LinearLayout? And no I am not aware of anything :(
  • Peter Nelson
    Peter Nelson over 13 years
    "Why not use a RelativeLayout inside a LinearLayout?" I could try that but it seems like a hack, no offense. There must be a way to get this to work correctly.
  • Seraphim's
    Seraphim's over 10 years
    where do you set the style attributes for your dialog?
  • Mario Lenci
    Mario Lenci over 10 years
  • Naveed
    Naveed over 10 years
    Hi, My question was ops problem. I was browsing around and landed on this post. I actually want the dialog to take up as much space as possible. Is this the correct way of doing it i.e. setting a child of relative layout to fill parent?
  • AndrewKS
    AndrewKS over 10 years
    I believe you are supposed to set the RelativeLayout to fill_parent, in that example.