Unfortunately, this is not a particularly simple task to accomplish. In my answer here, I detail how to adjust the color of a ListSeparator by just checking out the parent style used by Android, creating a new image, and creating a new style based on the original. Unfortunately, unlike with the ListSeparator's style, AlertDialog themes are internal, and therefore cannot be referenced as parent styles. There is no easy way to change that little blue line! Thus you need to resort to making custom dialogs.
If that just isn't your cup of tea... don't give up! I was very disturbed that there was no easy way to do this so I set up a little project on github for making quickly customized holo-style dialogs (assuming that the phone supports the Holo style). You can find the project here: https://github.com/danoz73/QustomDialog
It should easily enable going from boring blue to exciting orange!
The project is basically an example of using a custom dialog builder, and in the example I created a custom view that seemed to cater to the IP Address example you give in your original question.
With QustomDialog, in order to create a basic dialog (title, message) with a desired different color for the title or divider, you use the following code:
private String HALLOWEEN_ORANGE = "#FF7F27";
QustomDialogBuilder qustomDialogBuilder = new QustomDialogBuilder(v.getContext()).
setTitle("Set IP Address").
setTitleColor(HALLOWEEN_ORANGE).
setDividerColor(HALLOWEEN_ORANGE).
setMessage("You are now entering the 10th dimension.");
qustomDialogBuilder.show();
And in order to add a custom layout (say, to add the little IP address EditText), you add
By following the Dialog source code, I found that Title is generated in Class MidWindow by inflating the dialog_title_holo.xml layout. so the Id of mTitleView is title and the Id of divider is titleDivider.
we can access to Id of title simply by android.R.id.title.
and access to Id of titleDivider by Resources.getSystem().getIdentifier("titleDivider","id", "android");
The final code that i used to change the Direction of title and changing color is:
TextView mTitle = (TextView)findViewById(android.R.id.title);
mTitle.setGravity(Gravity.RIGHT|Gravity.CENTER_VERTICAL);
int x = Resources.getSystem().getIdentifier("titleDivider","id", "android");
View titleDivider = findViewById(x);
titleDivider.setBackgroundColor(getContext().getResources().getColor(R.color.some_color));
Here is another solution (based on suggested answers) that handles the styling of the dialogs in one class without needing to worry about the state of the dialog when you change the style - dialog can be already shown or just initialized.
Usage example:
AlertDialog.Builder builder = new AlertDialog.Builder(context);
AlertDialog dialog = builder.create(); //or builder.show()
DialogViewDecorator.decorate(dialog, android.R.color.holo_red_light); //can also set the defaut color in the class
Implementation:
public class DialogViewDecorator {
private static final
@ColorRes int DEFAULT_TITLE_DIVIDER_COLOR = android.R.color.holo_orange_light;
public static void decorate(Dialog dialog) {
decorate(dialog, DEFAULT_TITLE_DIVIDER_COLOR);
}
/**
* Sets the title divider color when the view is shown by setting DialogInterface.OnShowListener on the dialog.
* <p/>
* If you want to do other things onShow be sure to extend OnDecoratedDialogShownListener(call super.show(...)!)
* and call {@link #decorate(Dialog, int, OnDecoratedDialogShownListener)}.
*
* @param dialog
* @param titleDividerColor
*/
public static void decorate(Dialog dialog, final int titleDividerColor) {
decorate(dialog, titleDividerColor, new OnDecoratedDialogShownListener(titleDividerColor));
}
/**
* Method for setting a extended implementation of OnDecoratedDialogShownListener. Don't forget to call super
* or the titleDividerColor wont be applied!
*
* @param dialog
* @param titleDividerColor
* @param OnShowListener
* @param <T>
*/
public static <T extends OnDecoratedDialogShownListener> void decorate(Dialog dialog, int titleDividerColor, T OnShowListener) {
if (dialog == null || titleDividerColor <= 0) { return; }
if (dialog.isShowing()) {
setTitleDividerColor(dialog, titleDividerColor);
} else {
dialog.setOnShowListener(OnShowListener);
}
}
private static void setTitleDividerColor(DialogInterface dialogInterface, int titleDividerColor) {
try {
Dialog dialog = (Dialog) dialogInterface;
int dividerId = dialog.getContext().getResources().getIdentifier("android:id/titleDivider", null, null);
View divider = dialog.findViewById(dividerId);
if (divider != null) {
divider.setBackgroundColor(dialog.getContext().getResources().getColor(titleDividerColor));
}
} catch (Exception e) {
e.printStackTrace();
}
}
public static class OnDecoratedDialogShownListener implements DialogInterface.OnShowListener {
private int titleDividerColor;
public OnDecoratedDialogShownListener() {
this.titleDividerColor = DEFAULT_TITLE_DIVIDER_COLOR;
}
public OnDecoratedDialogShownListener(int titleDividerColor) {
this.titleDividerColor = titleDividerColor;
}
@Override
public void onShow(DialogInterface dialogInterface) {
setTitleDividerColor(dialogInterface, titleDividerColor);
}
}}