Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AlertDialog theme problems

Tags:

android

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.

like image 936
Carlos Avatar asked Oct 14 '12 20:10

Carlos


People also ask

Is AlertDialog deprecated?

This method is deprecated.

What is the difference between Dialog and AlertDialog?

AlertDialog is a lightweight version of a Dialog. This is supposed to deal with INFORMATIVE matters only, That's the reason why complex interactions with the user are limited. Dialog on the other hand is able to do even more complex things .

What is AlertDialog message?

Android AlertDialog can be used to display the dialog message with OK and Cancel buttons. It can be used to interrupt and ask the user about his/her choice to continue or discontinue. Android AlertDialog is composed of three regions: title, content area and action buttons.

What is the structure of an AlertDialog?

Android Alert Dialog is built with the use of three fields: Title, Message area, and Action Button.


2 Answers

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))
like image 195
Marcin S. Avatar answered Nov 17 '22 09:11

Marcin S.


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 );
like image 26
Lokesh Tiwari Avatar answered Nov 17 '22 11:11

Lokesh Tiwari