Show Light AlertDialog using Theme.Light.NoTitleBar

13,220

Solution 1

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);

Solution 2

You can use something like this when creating your AlertDialog :

AlertDialog.Builder builder = null;
if(Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB) {
    builder = new AlertDialog.Builder(BaseActivity.this);
} else {
    builder = new AlertDialog.Builder(BaseActivity.this, AlertDialog.THEME_HOLO_LIGHT);
}
// ... do your other stuff.

This code will create Holo Styled AlertDialog in newer versions and normal device based AlertDialog on devices with older version of Android.

Solution 3

You have to use an AlertDialog Builder. With it you can set a style to your Dialog. Look the following example: http://pastebin.com/07wyX0V3

<style name="popup_theme" parent="@android:style/Theme.Light">
    <item name="android:windowBackground">@color/back_color</item>
    <item name="android:colorBackground">@color/back_color</item>
</style>

Solution 4

If you are using new Material library from google, and want to keep the theme consistent

implementation 'com.google.android.material:material:1.0.0-beta01'

Simply use this for Light AlertBox

AlertDialog.Builder builder = new AlertDialog.Builder(getActivity(),R.style.Theme_MaterialComponents_Light_Dialog_Alert);

for dark AlertBox

AlertDialog.Builder builder = new AlertDialog.Builder(getActivity(),R.style.Theme_MaterialComponents_Dialog_Alert);
Share:
13,220
Si8
Author by

Si8

Updated on July 21, 2022

Comments

  • Si8
    Si8 almost 2 years

    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?

  • Si8
    Si8 almost 11 years
    I am seeing that on my phone running JB 4.2.2
  • Si8
    Si8 almost 11 years
    It covers the entire screen white!