3

I have a Dialog that is constructed with AlertDialog.Builder, and I would like to change the backgroud color of the dialog.

I've been reading in Internet that it's possible to do using ContextThemeWrapper (working with API 10), but it doesn't work.

What I'm doing is:

ContextThemeWrapper wrapper = new ContextThemeWrapper(this, R.style.MyDialogTheme);
AlertDialog alertDialog = new AlertDialog.Builder(wrapper)).create();  

<style name="MyDialogTheme" parent="@android:style/Theme.Dialog">
    <item name="android:background">#FFFFFF</item>
</style>

Why does this not work?

Thanks in advance!

1
  • Is the dialog actually shown or not? You never call alertDialog.show() Commented Oct 18, 2013 at 8:28

3 Answers 3

2

The alertDialog xml file, is here:

https://android.googlesource.com/platform/frameworks/base/+/master/core/res/res/layout/alert_dialog_micro.xml

Just change their values:

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {

    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
    builder.setTitle(_title);
    builder.setMessage(_message);
    //....
    AlertDialog dialog = builder.show();

    int objeto = getResources().getIdentifier("buttonPanel","id","android");
    View vistaObjeto=dialog.findViewById(objeto);
    if (vistaObjeto!=null){
        vistaObjeto.setBackgroundColor(Color.RED);
    }
    objeto = getResources().getIdentifier("topPanel", "id", "android");
    vistaObjeto = dialog.findViewById(objeto);
    if (vistaObjeto != null){
        vistaObjeto.setBackgroundColor(Color.YELLOW);
    }
    objeto = getResources().getIdentifier("alertTitle","id","android");
    vistaObjeto=dialog.findViewById(objeto);
    if (vistaObjeto!=null){
        ((TextView)vistaObjeto).setTextColor(Color.BLACK);
    }
    objeto = getResources().getIdentifier("titleDivider","id","android");
    vistaObjeto=dialog.findViewById(objeto);
    if (vistaObjeto!=null){
        vistaObjeto.setBackgroundColor(Color.GREEN);
    }
    objeto = getResources().getIdentifier("contentPanel","id","android");
    vistaObjeto=dialog.findViewById(objeto);
    if (vistaObjeto!=null){
        vistaObjeto.setBackgroundColor(Color.BLUE);
    }
    objeto = getResources().getIdentifier("buttonPanel","id","android");
    vistaObjeto=dialog.findViewById(objeto);
    if (vistaObjeto!=null){
        vistaObjeto.setBackgroundColor(Color.MAGENTA);
    }
    objeto = getResources().getIdentifier("parentPanel","id","android");
    vistaObjeto=dialog.findViewById(objeto);
    if (vistaObjeto!=null){
        vistaObjeto.setBackgroundColor(Color.CYAN);
    }
    return dialog;
}
Sign up to request clarification or add additional context in comments.

1 Comment

Good find. Link is now outdated, however; currently, the following link works: android.googlesource.com/platform/frameworks/base/+/master/core/…
0

Use the following code for a customized dialog box:

protected Dialog onCreateDialog(int dialogId) {
    LayoutInflater inflater = 
        (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    final AlertDialog.Builder builder = new AlertDialog.Builder(this);
    final View viewMessEdit = inflater.inflate(
        R.layout.example,
        (ViewGroup) findViewById(R.id.dialog_mess_edit_root));
    builder.setView(viewMessEdit);
    viewMessEdit.setBackgroundResource(R.color.pink_dark);
}

And follow this link:

Comments

0

I've modified my AlertDialog with AlertDialog.THEME_DEVICE_DEFAULT_LIGHT and so I wrote my builder like:

AlertDialog.Builder dialog = new AlertDialog.Builder(ApplicationCertificatesListFragment.this.getActivity(), AlertDialog.THEME_DEVICE_DEFAULT_LIGHT);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.