8

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

SamH67
  • 319
  • 1
  • 4
  • 14

2 Answers2

18

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.

Saurabh Bhandari
  • 2,438
  • 4
  • 26
  • 33
  • First look and tell me what I should do there I cant add this to my return dialog not able to Ankit Shah is doing it right but there is some problem – SamH67 Dec 26 '16 at 13:43
  • @SamH67 tell me exactly what do you want. Then I can help you. – Saurabh Bhandari Dec 26 '16 at 16:16
  • I want to do it my way because I am not able to add your code. Look at my code and tell me how to add but please try before doing it because I have already tried this thing before I have posted this question so I already know this thing but I am able to change theme inside that return statement – SamH67 Dec 27 '16 at 05:06
  • See my **note** Saurabh you have posted same thing I already told you I know this thing still you have posted same line in answer. – SamH67 Dec 27 '16 at 05:11
  • No it couldn't shouldn't work it says cannot resolve constructor context theme wrapper – SamH67 Dec 27 '16 at 05:33
  • And now dont say I have not created style for this, Please if you have a solution then post else dont vote me down for no reason OK? – SamH67 Dec 27 '16 at 05:46
  • OK but here it isn't working you can see the image added by me. Maybe something else I have to try. If you have another way of doing it let me know ok? – SamH67 Dec 27 '16 at 05:51
  • I have a class public class WithNamesFragment extends DialogFragment – SamH67 Dec 27 '16 at 05:53
  • in the place of 'this' use `getActivity().getApplicationContext();` or `getContext()` to pass the context. – Saurabh Bhandari Dec 27 '16 at 05:56
  • Now setAdapter is getting red underline it says cannot resolve method setAdapter – SamH67 Dec 27 '16 at 06:03
  • Got it sorry I was missing bracket this getContext() this worked now it look like this (new ContextThemeWrapper(getContext(), R.style.AlertDialogCustom)) last bracket was missing now It's ok let me run and then I'll verify your answer ok – SamH67 Dec 27 '16 at 06:07
  • Can I reduce the header size of a dialog? – SamH67 Dec 27 '16 at 06:22
  • Tell me in short how to do it. – SamH67 Dec 27 '16 at 06:29
  • @MML that is your activity context – Saurabh Bhandari Apr 08 '19 at 11:26
5

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);
Ankita Shah
  • 1,866
  • 16
  • 31