Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Alert Dialog background theme/Color

I want to set AlertDialogue theme or change background color.

Though I know it has a default theme but in different version I am getting different theme so I want to fix it for all version.

Or simply change background color as white

 @NonNull
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        final SimpleAdapter adapter = new SimpleAdapter(getContext(), imagesWithNames, R.layout.lib_dialog_image,
                new String[]{"name", "imageID","Spacing"}, new int[]{R.id.text1, R.id.image1,R.id.spacing});
        return new AlertDialog.Builder(getContext()).setAdapter(adapter,
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        ((PlaceCallActivity) getContext()).OnSelected(WithNamesFragment.this.getClass(), (int) ((HashMap<String, Object>) adapter.getItem(i)).get("imageID"));
                    }
                }).setCancelable(true).setTitle("PICK YOUR AVATAR").setNegativeButton("Cancel",new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
            }
        }).create();
    }

Don't post your code please tell where I should make a change here.

AlertDialog.Builder builder = new AlertDialog.Builder(new ContextThemeWrapper(this, R.style.AlertDialogCustom));

Note : The Above line would do it but I want to know where I should give style for my AlertDialogue

enter image description here

like image 675
SamH67 Avatar asked Dec 26 '16 12:12

SamH67


2 Answers

Create your style in your styles.xml file as follows.

<style name="AlertDialogCustom" parent="@android:style/Theme.Dialog">
        <item name="android:textColor">@color/White</item>
        <item name="android:textStyle">bold</item>
        <item name="android:headerDividersEnabled">true</item>
        <item name="android:typeface">normal</item>
        <item name="android:background">@color/colorPrimaryDark</item>
    </style>

Then Create Alert Dialog using Builder as follows

AlertDialog.Builder builder = new AlertDialog.Builder(new ContextThemeWrapper(this/getActvity(), R.style.AlertDialogCustom));

Here passing the current class Context and style to the ContextThemeWrapper class constructor.

like image 98
Saurabh Bhandari Avatar answered Nov 19 '22 04:11

Saurabh Bhandari


You should add dialog style inside res/values/styles.xml. Like as below.

<style name="MyDialog" parent="@android:style/Theme.Holo.Light.Dialog">
        <item name="android:background">@color/white</item>
    </style>

Or you change background color as below also:

EDIT:

 getWindow().setBackgroundDrawableResource(R.color.white);
like image 43
Ankita Shah Avatar answered Nov 19 '22 06:11

Ankita Shah