1

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 having my message only but i want it to be fully custom alert box as i said earlier

please help me, and please share a sample code snippet if its possible thnks :)

currently the code is like that :-

AlertDialog.Builder alertDialogBuilder3 = new AlertDialog.Builder(context);
                alertDialogBuilder3.setTitle("Location Check");
                alertDialogBuilder3
                .setMessage("Do you want to cancel loading?")
                .setCancelable(false)
                .setPositiveButton("Ok",new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog,int id) {

                        LAtestTab.this.finish();
                    }
                })
                .setNegativeButton("No",new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog,int id) {
                        // if this button is clicked, just close
                        // the dialog box and do nothing
                        dialog.cancel();
                    }
                });
                ;

                AlertDialog alertDialog3 = alertDialogBuilder3.create();

                alertDialog3.show();
Mehul Ranpara
  • 4,245
  • 2
  • 26
  • 39
Salman Khan
  • 2,822
  • 5
  • 32
  • 53
  • see this tutorial http://www.mkyong.com/android/android-custom-dialog-example/ and make a google search for "alertdialog custom" you will get lots of good example and code snippet . – ρяσѕρєя K Dec 11 '12 at 06:16
  • can u tell me plz how to call that layout in center it is working properly but dialog's position is not in the center – Salman Khan Dec 11 '12 at 07:26

2 Answers2

6

Here is Java Code....

exit.setOnClickListener(new OnClickListener() 
{   
     @Override
     public void onClick(View arg0) 
    {
            final Dialog dialog1 = new Dialog(CatchTheCatActivity.this);
            dialog1.requestWindowFeature(Window.FEATURE_NO_TITLE);
            dialog1.setContentView(R.layout.custom_alert);

            Button yes = (Button) dialog1.findViewById(R.id.button1);
            Button no = (Button) dialog1.findViewById(R.id.button2);

            yes.setOnClickListener(new OnClickListener()
            {
                @Override
                public void onClick(View v) 
                {
                    finish();   
                }
            });
            no.setOnClickListener(new OnClickListener()
            {
                @Override
                public void onClick(View v) 
                {
                        dialog1.dismiss();  

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

here is the XML file... (custom_alert)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="110dp"
    android:background="@drawable/bg"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" android:gravity="center">


        <ImageView
            android:id="@+id/imageView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="5dp"
            android:src="@drawable/textmessage" />

    </LinearLayout>


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:gravity="center|bottom" >

        <Button
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/yes_button"/>


        <Button
            android:id="@+id/button2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="20dp"
            android:background="@drawable/no_button" />

    </LinearLayout>

</LinearLayout>
Mehul Ranpara
  • 4,245
  • 2
  • 26
  • 39
  • can u tell me plz how to call that layout in center it is working properly but dialog's position is not in the center – Salman Khan Dec 11 '12 at 07:25
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"?>
<LinearLayout xmlns: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:

LayoutInflater inflater = getLayoutInflater();
View dialoglayout = inflater.inflate(R.layout.dialog_layout, (ViewGroup) getCurrentFocus());
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setView(dialoglayout);
Nirali
  • 13,571
  • 6
  • 40
  • 53