Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Show Light AlertDialog using Theme.Light.NoTitleBar

I used the following line in my manifest:

android:theme="@android:style/Theme.Light.NoTitleBar"

to have no title bar and display the light version of AlertDialog in my app, like in example: enter image description here

But it's displaying in dark theme still:

enter image description here

My Dialog Java code:

    new AlertDialog.Builder(FreeDraw.this)
    .setIcon(android.R.drawable.ic_dialog_alert)
    .setTitle("Clear Drawing?")
    .setMessage("Do you want to clear the drawing board?")
    .setPositiveButton("Yes", new DialogInterface.OnClickListener() {

        @Override
        public void onClick(DialogInterface dialog, int which) {
            finish();
            startActivity(getIntent());  
        }
    })
    .setNegativeButton("No", null)
    .show();

How do I keep the theme light for AlertDialog?

like image 1000
Si8 Avatar asked Sep 04 '13 14:09

Si8


1 Answers

The top dialog in your post is a Holo Light themed dialog whereas the bottom one is the older themed dialog. You cannot get a Holo Light themed dialog on versions below Honeycomb. Here is a little snippet I use to select the light theme based on what android version the device is running.

The AlertDialog.Builder will use the theme of the context it's passed. You can use a ContextThemeWrapper to set this.

ContextThemeWrapper themedContext;
if ( Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB ) {
    themedContext = new ContextThemeWrapper( FreeDraw.this, android.R.style.Theme_Holo_Light_Dialog_NoActionBar );
}
else {
    themedContext = new ContextThemeWrapper( FreeDraw.this, android.R.style.Theme_Light_NoTitleBar );
}
AlertDialog.Builder builder = new AlertDialog.Builder(themedContext);
like image 122
James McCracken Avatar answered Sep 19 '22 05:09

James McCracken