java - How to change the background of a android Dialog Box -
how can alter background color programatically of dialog box?
alertdialog.builder builder = new alertdialog.builder(this); builder.setcancelable(false); builder.setmessage(r.string.tnc) //.settitle(r.string.tnc_title) .setpositivebutton(android.r.string.ok, new dialoginterface.onclicklistener() { public void onclick(dialoginterface dialog, int id) { log.d(tag, "ok button clicked"); dialog.dismiss(); finish(); }//end onclick }); alertdialog dialog = builder.create(); dialog.show();
this best way !
firstly create dialog box programmatically .
public void onclick(view v) { dialog1 = new dialog(page.this,r.style.mybackgroundstyle); window window = dialog1.getwindow(); window.setgravity(gravity.center); window.setlayout(windowmanager.layoutparams.fill_parent, windowmanager.layoutparams.fill_parent); window.setbackgrounddrawable(new colordrawable()); dialog1.settitle(null); dialog1.requestwindowfeature(window.feature_no_title); window.setflags(windowmanager.layoutparams.flag_fullscreen, windowmanager.layoutparams.flag_fullscreen); dialog1.setcontentview(r.layout.custom_dialog); dialog1.getwindow().setbackgrounddrawable( new colordrawable(android.graphics.color.transparent)); dialog1.setcanceledontouchoutside(true); description.settext(description); heading_name.settext(product_name); button abc = (button)dialog1.findviewbyid(r.id.abc); abc.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { } }); button xyz = (button) dialog1.findviewbyid(r.id.xyz); retailer_btn.setonclicklistener(new view.onclicklistener() { dialog1.dismiss(); } }); dialog1.show(); } });
and create new xml file , set code in xml (dialog layout)
<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/description_layout1" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_margintop="45dp" android:layout_gravity="top|right" android:gravity="right|top" android:orientation="vertical" > <linearlayout android:id="@+id/desription_layout2" android:layout_width="160dp" android:layout_height="90dp" android:layout_marginright="2dp" android:padding="2dp" android:background="@drawable/dropdown_white_background" android:orientation="vertical" > <button android:id="@+id/abc" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_gravity="center" android:layout_weight="2" android:background="@drawable/curve_1" android:text="abc" android:textsize="18dp" android:textcolor="#000000"/> <view android:layout_width="fill_parent" android:layout_height="2dp" android:layout_marginbottom="1dp" android:layout_margintop="1dp" android:background="#ffffff" /> <button android:id="@+id/xyz" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_gravity="center" android:layout_weight="2" android:background="@drawable/curve_1" android:text="xyz" android:textsize="18dp" android:textcolor="#000000"/> </linearlayout> </linearlayout>
and if want additional stuff dialog such rounding corners of dialog of button create new xml in drawable , add together background accordingly.
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_pressed="true"> <shape android:shape="rectangle"> <corners android:radius="3dip" /> <stroke android:width="1dip" android:color="#ffffff" /> <gradient android:angle="-90" android:endcolor="#ccffff" android:startcolor="#ccffff" /> </shape> </item> <item android:state_focused="true"><shape android:shape="rectangle"> <corners android:radius="3dip" /> <stroke android:width="1dip" android:color="#ffffff" /> <solid android:color="#66ffff" /> </shape> </item> <item> <shape android:shape="rectangle"> <corners android:radius="3dip" /> <stroke android:width="1dip" android:color="#66ffff" /> <gradient android:angle="-90" android:endcolor="#66ffff" android:startcolor="#66ffff" /> </shape> </item> </selector>
hope helps :)
java android background dialog alertdialog
No comments:
Post a Comment