Skip to content Skip to sidebar Skip to footer

Android Custom Dialog Linearlayout Size Same As Dialogs Bg Image

I am creating a custom dialog as: Dialog myDialog = new Dialog(context, R.style.Theme_Levels); myDialog.setContentView(R.layout.levelselector); myDialog.show(); Style

Solution 1:

Here is how you can do this:

<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:id="@+id/LevelSelector"android:layout_height="fill_parent"android:layout_width="fill_parent"android:orientation="vertical"android:paddingBottom="50dip" ><Viewandroid:layout_width="0dp"android:layout_height="0dp"android:layout_weight="1" /><Buttonandroid:text="Easy"android:id="@+id/btn_Easy"android:layout_gravity="center_horizontal"android:layout_width="wrap_content"android:layout_height="wrap_content" /></LinearLayout>

Solution 2:

Change android:layout_gravity="bottom|center_horizontal" to android:gravity="bottom|center_horizontal". android:layout_gravity is gravity of the layout itself w.r.t. its parent. android:gravity applies to its contents.

Solution 3:

i had the same problem in my case i ended up using a RelativeLayout

here it is.

<?xml version="1.0" encoding="utf-8"?><RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:id="@+id/body"android:layout_margin="20dip"android:layout_width="wrap_content"android:layout_height="wrap_content"android:background="@drawable/dialog_bg" 
><TextViewandroid:id="@+id/message"android:layout_alignParentTop="true"android:layout_width="fill_parent"android:layout_height="wrap_content"android:layout_margin="10dip"android:inputType="textMultiLine"android:textColor="@color/table_text"android:textScaleX=".95"android:textStyle="bold"       
/><TextViewandroid:id="@+id/dummy"android:layout_below="@id/message"android:layout_width="fill_parent"android:layout_height="60dip"android:visibility="invisible" 
/><LinearLayoutandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_margin="10dip"android:layout_below="@id/message"android:layout_centerHorizontal="true"android:orientation="horizontal"
><ImageButtonandroid:id="@+id/ok"android:layout_width="wrap_content"android:layout_height="wrap_content"android:background="@drawable/button_alert"android:src="@drawable/btn_yes"android:visibility="gone"android:layout_weight=".5"
    /><ImageButtonandroid:id="@+id/cancel"android:layout_width="wrap_content"android:layout_height="wrap_content"android:background="@drawable/button_alert"android:src="@drawable/btn_no"android:visibility="gone"android:layout_weight=".5"
    /></LinearLayout></RelativeLayout>

Solution 4:

Ok, I have found a way, not a clean way but works.

You need to add background in style.xml file

   <item name="android:windowBackground">@drawable/levelselectbg</item> 

AND also in you linearlayout:

android:background="@drawable/levelselectbg"

The first background is required to keep the dialog borderless (which I have specified in my style.xml in my question) and the linearlayout background is required to give size to the layout, now it knows its dimensions and places the button at bottom center with padding.

Hope this helps someone, if a better solution is available please share.

Post a Comment for "Android Custom Dialog Linearlayout Size Same As Dialogs Bg Image"