I have an alert dialog in the app as shown below.
I want the title and the line which separates the title - message body to be in orange colour. how can i do this? What i tried is using custom style as shown below. But this did not work.
<style name="AboutDialog" parent="@android:style/Theme.Dialog">
<item name="android:textColor">#E5492A</item>
</style>
my alert dialog code:
AlertDialog.Builder alertDialog = new AlertDialog.Builder( new ContextThemeWrapper(MainActivity.context, R.style.AboutDialog));
alertDialog.setTitle("Sample");
alertDialog.setMessage(R.string.rate_dialog_text);
alertDialog.setPositiveButton(R.string.rate_now_text,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
MainActivity.context.startActivity(new Intent(
Intent.ACTION_VIEW, Uri
.parse("market://details?id="
+ MainActivity.APP_PNAME)));
if (editor != null) {
editor.putBoolean("dontshowagain", true);
editor.commit();
}
dialog.dismiss();
}
});
alertDialog.setNeutralButton(R.string.remind_later_text,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
alertDialog.setNegativeButton(R.string.no_thanks_text,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
if (editor != null) {
editor.putBoolean("dontshowagain", true);
editor.commit();
}
dialog.dismiss();
}
});
return alertDialog.create();
}
Your can use this project AlertDialogPro. Include this project to yours and define your theme like below:
And specify this theme to your app's theme with attribute "alertDialogProTheme":
Use AlertDialogPro.Builder to build dialog:
by the way its a late answer, maybe it will be useful for someone. to create Custom AlertDialog and have the same neat buttons like the Default AlertDialog. follow the simple method below
Try to create a
custom layout
for dialog and provide thatlayout
to your alert dialog by usingYou can see this example for custom dialog.
Instead of:
Try this:
NOTE: This is only available for API 11 (Android 3.0) and above.
If you need to to support Android < 3.0 you probably have to create a custom dialog (instead of using
AlertDialog
EDIT Added really sleazy reflection-based hack
If you are really stuck and don't want to implement a custom dialog, then try the following.
After you've built the dialog and just before you want to return it, instead of this:
do this:
Now add this very nasty reflection-based method:
I tested this on Android 2.2 and Android 4.0 and it works. It may not do exactly what you want, so you'll need to try it. I can't guarantee it will work on all devices or on future versions of Android, since it depends a lot on the way the
AlertDialog
class is implemented (if they change that in the future this probably won't work anymore).Just a few notes for anyone who cares:
The reason that I use
setOnShowListener()
is because you can't actually get to the internalView
objects thatAlertDialog
uses until after they have been inflated. They don't get inflated immediately when you create anAlertDialog
, it happens some time later. Using the listener we can get control after the layout is inflated but before theDialog
is shown.I'm using reflection to access in internal member variables in the
AlertDialog
implementation. Once I get access to theTextView
that contains the title, I have to monkey around to find the horizontal line that is used to separate the title from the message. To do this I get theLayout
that surrounds the title (thisLayout
contains the alert icon and title text). Then I get theLayout
that surrounds that (thisLayout
wraps the icon, title text and the separator). Then I look at all children of the surrounding layout and set the color on allImageView
objects in there. The separator is implemented as anImageView
using a drawable, therefore it isn't possible to just change the color. Note: An alternative to usingsetColorFilter()
would be to replace the drawable in the ImageView with a suitably colored drawable here.Thanks for the challenge, it was kinda fun to figure this out :-D