7

I'm creating an AlertDialog. If create it like this:

AlertDialog.Builder builder = AlertDialog.Builder((RelationActivity)getContext());
builder.setMessage("No relations found.");
builder.setPositiveButton("Ok", new DialogInterface.OnClickListener() {       
    public void onClick(DialogInterface dialog, int id) {
        ((RelationActivity)getContext()).finish();
    }
});
builder.create();
builder.show();

This is the result: http://www.ozze.com.br/1.png

But, if I try to set a theme, like this:

AlertDialog.Builder builder = new AlertDialog.Builder(((RelationActivity)getContext()), android.R.style.Theme_Holo_Light_Dialog);

This is the result: http://www.ozze.com.br/2.png

Please, can anyone help me with this issue? It looks like when using a theme, the theme "surrounds" the alert dialog.

seaplain
  • 771
  • 7
  • 25
Carlos
  • 170
  • 1
  • 3
  • 11

2 Answers2

10

To set a different Theme for the alert dialog like Theme.Holo.Light try to use ContextThemeWrapper as used in Dialog.java in android source:

builder = new AlertDialog.Builder(new ContextThemeWrapper(this, android.R.style.Theme_Holo_Light_Dialog))
Marcin S.
  • 11,161
  • 6
  • 50
  • 63
  • is it ContextThemeWrapper applicable for API 14 ,I doubt . – Android Stack Oct 14 '12 at 21:34
  • Thanks Marcin! That works! But it works with Theme_Holo, not with Theme_Holo_Light... Is this common? – Carlos Oct 14 '12 at 21:54
  • @AndroidStack It's available from API 1, and is not deprecated. – Cat Oct 14 '12 at 21:58
  • Well, Marcin´s solution worked great! But I guess Theme_Holo_Light works perfect only on API Level 14. As soon as I get done with my current project I´ll run some tests. – Carlos Oct 15 '12 at 03:02
  • Glad that it worked for you. I think you are right and Holo.Light is available since API 11 Check this link http://stackoverflow.com/questions/9681648/how-to-use-holo-light-theme-and-fall-back-to-light-on-pre-honeycomb-devices It explains how to use Holo.Light in older devices. Then in ContextThemeWrapper just replace android.R.style.Theme_Holo_Light_Dialog with your own style that parent is Holo.Light something like this: R.style.myCustomDialog. – Marcin S. Oct 15 '12 at 14:57
9

Here is link of Original answer here

For quick reference I am posting here
Theme with v7 library android.support.v7.app.AlertDialog

android.support.v7.app.AlertDialog.Builder builder = new android.support.v7.app.AlertDialog.Builder(this,R.attr.alertDialogTheme);

Theme with constructer for android.app.AlertDialog

android.app.AlertDialog.Builder builder = new android.app.AlertDialog.Builder(this,AlertDialog.THEME_HOLO_LIGHT );

But as per new documentation
This constant(AlertDialog.THEME_HOLO_LIGHT) was deprecated in API level 23. Use Theme_Material_Light_Dialog_Alert .

 android.app.AlertDialog.Builder builder = new android.app.AlertDialog.Builder(this,android.R.style.Theme_Material_Light_Dialog_Alert );
Community
  • 1
  • 1
Lokesh Tiwari
  • 10,496
  • 3
  • 36
  • 45