Style Dialog in Android
Published on 14.06.2014
The one of the easiest ways to create custom styled Dialog in Android is to hide title and set xml layout in content. For example:
final Dialog dialog = new Dialog(MainActivity.this); dialog.requestWindowFeature(Window.FEATURE_NO_TITLE); dialog.setContentView(R.layout.dialog_select_action_new); dialog.show();
requestWindowFeature
method is need to be set before setContentView
And in dialog_select_action_new.xmlfile we have freedom to make any modifications, just like this:
<?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="match_parent" android:background="#f0f7fa" android:minWidth="350dp" android:orientation="vertical" > <LinearLayout android:layout_width="match_parent" android:layout_height="40dp" android:background="#97c7dc" android:orientation="vertical" > <TextView android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" android:text="@string/add_event_title" android:textColor="#ffffff" android:textSize="20sp" android:textStyle="bold" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:background="#f0f7fa" android:orientation="vertical" android:padding="10dp" > <Button android:id="@+id/dialog_select_fun_btn" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@drawable/btn_add_fun" android:drawableLeft="@drawable/fun_icon" android:drawablePadding="10dp" android:paddingLeft="10dp" android:paddingRight="10dp" android:text="@string/add_event_fun_btn" android:textColor="#ffffff" /> <Button android:id="@+id/dialog_select_ill_btn" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:background="@drawable/btn_add_fun" android:drawableLeft="@drawable/baby_health_icon" android:drawablePadding="10dp" android:paddingLeft="10dp" android:paddingRight="10dp" android:text="@string/add_event_fun_ill" android:textColor="#ffffff" /> </LinearLayout> </LinearLayout>
Final result is:
ps: The big plus from that is on android 2.x devices dialog looks like android 4.x version.