Skip to content Skip to sidebar Skip to footer

Custom Alert Box

I want an alert box which is fully customized in short i want my background image on that, my custom buttons and my custom message on it, currently i m using a default alert box ha

Solution 1:

Here is Java Code....

exit.setOnClickListener(newOnClickListener() 
{   
     @OverridepublicvoidonClick(View arg0) 
    {
            finalDialogdialog1=newDialog(CatchTheCatActivity.this);
            dialog1.requestWindowFeature(Window.FEATURE_NO_TITLE);
            dialog1.setContentView(R.layout.custom_alert);

            Buttonyes= (Button) dialog1.findViewById(R.id.button1);
            Buttonno= (Button) dialog1.findViewById(R.id.button2);

            yes.setOnClickListener(newOnClickListener()
            {
                @OverridepublicvoidonClick(View v) 
                {
                    finish();   
                }
            });
            no.setOnClickListener(newOnClickListener()
            {
                @OverridepublicvoidonClick(View v) 
                {
                        dialog1.dismiss();  

                }
            });
            dialog1.show();
   }
});

here is the XML file... (custom_alert)

<?xml version="1.0" encoding="utf-8"?><LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="wrap_content"android:layout_height="110dp"android:background="@drawable/bg"android:orientation="vertical" ><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="vertical"android:gravity="center"><ImageViewandroid:id="@+id/imageView1"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginTop="5dp"android:src="@drawable/textmessage" /></LinearLayout><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:layout_marginTop="10dp"android:gravity="center|bottom" ><Buttonandroid:id="@+id/button1"android:layout_width="wrap_content"android:layout_height="wrap_content"android:background="@drawable/yes_button"/><Buttonandroid:id="@+id/button2"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginLeft="20dp"android:background="@drawable/no_button" /></LinearLayout></LinearLayout>

Solution 2:

Refer to this links

http://www.mkyong.com/android/android-custom-dialog-example/

http://android-er.blogspot.in/2011/06/custom-alertdialog.html

And You can create your view directly from the Layout Inflater, you only need to use the name of your layout XML file and the ID of the layout in file.

<?xml version="1.0" encoding="utf-8"?><LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:id="@+id/dialog_layout_root"android:orientation="vertical"android:layout_width="fill_parent"android:layout_height="wrap_content"android:padding="10dp"
 >

And then you can set your layout on the builder with the following:

LayoutInflaterinflater= getLayoutInflater();
Viewdialoglayout= inflater.inflate(R.layout.dialog_layout, (ViewGroup) getCurrentFocus());
AlertDialog.Builderbuilder=newAlertDialog.Builder(this);
builder.setView(dialoglayout);

Post a Comment for "Custom Alert Box"