views:

975

answers:

4

Hello everyone,

I was wondering if someone could help me out. I am trying to create a custom AlertDialog. In order to do this, I added the following line of code in styles.xml

@drawable/color_panel_background

  • color_panel_background.9.png is located in drawable folder. This is also available in Android SDK res folder.

The following is the main activity.

package com.customdialog;

import android.app.Activity; import android.app.AlertDialog; import android.app.Dialog; import android.content.DialogInterface; import android.os.Bundle;

public class CustomDialog extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main);

    this.setTheme(R.style.CustomAlertDialog);
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setMessage("HELLO!");
    builder .setCancelable(false)
      .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
       public void onClick(DialogInterface dialog, int id) {
           //MyActivity.this.finish();
       }
   })
   .setNegativeButton("No", new DialogInterface.OnClickListener() {
       public void onClick(DialogInterface dialog, int id) {
           //dialog.cancel();
       }
   });

    AlertDialog alertdialog = builder.create();
    alertdialog.show();
}

}

In order to apply the theme to an AlertDialog, I had to set the theme to the current context.

However, I just can't seem to get the app to show customized AlertDialog. Can anyone help me out with this, and thank you very much in advance!

A: 

I'm having the exact same problem.

I am amused
+1  A: 

I guess it cannot be done. At least not with the Builder. I'm working with 1.6 and the Implementation in Builder.create() is:

public AlertDialog create() {
    final AlertDialog dialog = new AlertDialog(P.mContext);
    P.apply(dialog.mAlert);
    [...]
}

which calls the "not-theme-aware" constructor of AlertDialog, which looks like this:

protected AlertDialog(Context context) {
    this(context, com.android.internal.R.style.Theme_Dialog_Alert);
}

There is a second constructor in AlertDialog for changing themes:

protected AlertDialog(Context context, int theme) {
    super(context, theme);
    [...]
}

that the Builder just doesn't call.

If the Dialog is pretty generic anyway, I'd try writing a subclass of AlertDialog, calling the second constructor and use that class instead of the Builder-mechanism.

rflexor
+1  A: 

In Dialog.java (Android src) a ContextThemeWrapper is used. So you could copy the idea and do something like:

AlertDialog.Builder builder = new AlertDialog.Builder(new ContextThemeWrapper(this, R.style.AlertDialogCustom));

And then style it like you want:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <style name="AlertDialogCustom" parent="@android:style/AlertDialog">
        <item name="android:textColor">#00FF00</item>
        <item name="android:typeface">monospace</item>
        <item name="android:textSize">10sp</item>
    </style>
</resources>
Arve Waltin
A: 

I was having this AlertDialog theme related issue using sdk 1.6 as described here: http://markmail.org/message/mj5ut56irkrkc4nr

I solved the issue by doing the following: new AlertDialog.Builder(new ContextThemeWrapper(context, android.R.style.Theme_Dialog))...

Hope this helps.

chee