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
<resources>
<style name="CustomAlertDialog" parent="android:Theme.Dialog.Alert">
<item name="android:windowBackground">@drawable/color_panel_background</item>
</style>
</resources>
- 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?
I was having this
AlertDialog
theme related issue using sdk 1.6 as described here: http://markmail.org/message/mj5ut56irkrkc4nrI solved the issue by doing the following:
Hope this helps.
Arve Waltin's solution looks good, although I haven't tested it yet. There is another solution in case you have trouble getting that to work.... Extend
AlertDialog.Builder
and override all the methods (eg.setText
,setTitle
,setView
, etc) to not set the actual Dialog's text/title/view, but to create a new view within the Dialog's View do everything in there. Then you are free to style everything as you please.To clarify, as far as the parent class is concerned, the View is set, and nothing else.
As far as your custom extended class is concerned, everything is done within that view.
You can directly assign a theme when you initiate the Builder:
Then customize your theme in your
values/styles.xml
I was struggling with this - you can style the background of the dialog using
android:alertDialogStyle="@style/AlertDialog"
in your theme, but it ignores any text settings you have. As @rflexor said above it cannot be done with the SDK prior to Honeycomb (well you could useReflection
).My solution, in a nutshell, was to style the background of the dialog using the above, then set a custom title and content view (using layouts that are the same as those in the SDK).
My wrapper:
alert_dialog_title.xml (taken from the SDK)
alert_dialog_message.xml
Then just use
CustomAlertDialogBuilder
instead ofAlertDialog.Builder
to create your dialogs, and just callsetTitle
andsetMessage
as usual.