Dialog Fragment with wrap_content coming as a full screen. How to make it as a height of layout and not a full screen?
Asked Answered
H

2

19

I have created a custom dialog fragment and its xml as follows:

<?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:layout_margin="5dp"

    android:padding="5dip" >

    <ListView
        android:id="@+id/product_list"
         android:background="@drawable/border_details"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
         />

    <RelativeLayout
        android:id="@+id/AddtoCart"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_marginTop="10dp"

        android:background="#005959" >

        <Button
            android:id="@+id/button_addToCart"
            android:layout_width="250dp"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:background="@drawable/gradient_button"
            android:layout_marginTop="2dp"
            android:layout_marginBottom="2dp"
            android:text="@string/add_to_cart"
            android:textColor="#000000" />
    </RelativeLayout>

</RelativeLayout>

When I call this Dialog, it is taking full screen as provided in the screenshot:

ScreenShot

How do I restrict the size of this Dialog to the height of the list and the button?

How do I remove the excess white space?

Heyde answered 4/2, 2014 at 6:47 Comment(0)
H
26

Solved by changing RelativeLayout to LinearLayout and setting weight of list to 1 as follows

<?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:layout_margin="5dp"
   android:orientation="vertical"
    android:padding="5dip" >

    <ListView
        android:id="@+id/product_list"
         android:background="@drawable/border_details"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
         />
    <RelativeLayout 
    android:id="@+id/AddtoCart"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="bottom"
    android:layout_marginTop="10dp"
    android:background="#005959" >

    <Button
        android:id="@+id/button_addToCart"
        android:layout_width="250dp"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:layout_marginBottom="2dp"
        android:layout_marginTop="2dp"
        android:background="@drawable/gradient_button"
        android:text="@string/add_to_cart"
        android:textColor="#000000" />

</RelativeLayout>
</LinearLayout>
Heyde answered 4/2, 2014 at 11:59 Comment(0)
C
13

You have given android:layout_alignParentBottom="true" for the bottom Relative layout so it will take the screen's bottom since the parent relative layout's height is not static.

solution:

  1. You can change the reference for the bottom relative layout to -> align bottom (below) to the list view
  2. Or you can set the bottom layout as the footer for the list view
  3. Or use linear layout vertical instead of the parent relative layout with height as wrap content
  4. Or give static height for the parent relative layout so the bottom relative layout will get the parent relative layout's height and it will be aligned to the parent relative layout's bottom (don't forget to give align top of bottom relative layout to the list view)
Cestode answered 4/2, 2014 at 6:54 Comment(6)
what is the reason for that u removed the answer tag for my ans?@HeydeCestode
When I change the orientation the button is not visible so it's not worked for me. I want my button to be visible always.Heyde
are u setting static height.?Cestode
No. It's just wrap_content and weightHeyde
Best answer to this problem in this forum, thank you!Nonperishable
Don't forget adding this part to your alert dialog fragment : override fun onResume() { super.onResume() dialog?.window?.setLayout( ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT) dialog?.window?.setGravity(Gravity.CENTER) }Spectral

© 2022 - 2024 — McMap. All rights reserved.