Skip to content Skip to sidebar Skip to footer

Set Background Image Alertdialog

I am trying to create a customized AlertDialog with Buttons and RadioGroup. I want to have a background image saved in my drawable for this AlertDialog. AlertDialog.Builder alert

Solution 1:

you can do that using Layout file. Check Code Below

LayoutInflaterinflater= (LayoutInflater) this
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
Viewdialoglayout= inflater.inflate(R.layout.dialog_layout, null);
AlertDialog.Builderbuilder=newAlertDialog.Builder(this);
builder.setView(dialoglayout);
builder.show(); 

dialog_layout.xml

<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="fill_parent"android:layout_height="fill_parent"android:gravity="center_horizontal"android:orientation="vertical" ><ImageViewandroid:id="@+id/imageView1"android:layout_width="match_parent"android:layout_height="match_parent"android:src="@drawable/ic_launcher" /></LinearLayout>

You can also use diffrent views in xml.

Solution 2:

Yes, there is a relatively simple variant that works with an AlertDialog:

// your code above
AlertDialog.Builderalert=newAlertDialog.Builder(AutoGenerate.this);                    
alert.setTitle("Select color");
// first show the dialog, then access its window and there set the background
alert.show();
alert.getWindow().setBackgroundDrawable(R.drawable.img);

Solution 3:

Custom dialog with custom view.

Dialogd=newDialog(AutoGenerate.this);
    d.requestWindowFeature(Window.FEATURE_NO_TITLE);
    d.setContentView(R.layout.alertdialog);// custom layour for dialog.Buttonthankyou= (Button) d.findViewById(R.id.thankyou);
    d.show();

alertdialog.xml

<?xml version="1.0" encoding="utf-8"?><LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:id="@+id/dialog_layout_root"android:layout_width="fill_parent"android:layout_height="wrap_content"android:background="#FFFFFF"android:orientation="vertical"android:padding="10dp"
 ><Buttonandroid:id="@+id/thankyou"android:layout_width="100dp"android:layout_height="60dp"android:layout_gravity="center_horizontal"android:text="hello"android:padding="5dp"></Button><TextViewandroid:id="@+id/textView1"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="TextView" /></LinearLayout>

Edit :

    rb= (RadioButton) findViewById(R.id.radioButton1);
    rb.setOnClickListener(newOnClickListener()
    {

        @OverridepublicvoidonClick(View v) {
            // TODO Auto-generated method stub
            showAlertDialog();
        }

    });       

     publicvoidshowAlertDialog() {

        finalDialogd=newDialog(MainActivity.this);
        d.requestWindowFeature(Window.FEATURE_NO_TITLE);
        d.setContentView(R.layout.alertdialog);
        // Thank you Button Listener.finalTextViewtv= (TextView) d.findViewById(R.id.textView1);
        finalButtonthankyou= (Button) d.findViewById(R.id.thankyou);
        thankyou.setOnTouchListener(newOnTouchListener() {

            @OverridepublicbooleanonTouch(View arg0, MotionEvent event) {
                // TODO Auto-generated method stub
                      tv.setText("Button Clicked");

                returntrue;
            }

        });

        d.show();
    }       

Solution 4:

You can set Icon to alert dialog like this

alert.setIcon(R.drawable.image);

Solution 5:

Post a Comment for "Set Background Image Alertdialog"