-
Notifications
You must be signed in to change notification settings - Fork 34
HoloAccent Dialogs
The android theme doesn't allow to (easily) customize the default dialogs, the line that separates the title and the content will have the default blue color. Hopefully, HoloAccent provides the utilities to achieve this. There are three different ways in which you can implement your dialogs so we will tackle them separately:
For simple dialogs, the most common way is to use the AlertDialog.Builder class. You can use the inner AccentAlertDialog.Builder class to create a dialog as you would with AlertDialog.Builder and the accent color will be correctly set:
AccentAlertDialog.Builder builder = new AccentAlertDialog.Builder(this);
builder.setTitle("Title");
builder.setMessage("This is a message.");
builder.show();Alternatively, you can use the default AlertDialog and AlertDialog.Builder classes but you have to make sure to apply the accent color after showing the dialog. This is explained more in detail in the next case.
No matter how you create your instance of Dialog, you can apply the color to the divider using the DividerPainter class. Just call paint() method and provide the Window of the dialog.
IMPORTANT: always call paint() AFTER dialog.show().
Dialog dialog = ...;
dividerPainter = new DividerPainter(context);
dialog.show();
dividerPainter.paint(dialog.getWindow());If you are launching the dialog from your AccentActivity subclass, you should use the AccentHelper instance. It is cleaner and so much cooler this way:
Dialog dialog = ...;
dialog.show();
getAccentHelper().prepareDialog(this, dialog.getWindow());The recommended way to display dialogs is to use DialogFragments. To do so with HoloAccent you just have to make your class extend AccentDialogFragment in stead of DialogFragment, this will take care of setting the accent color correctly.
public class SimpleDialogFragment extends AccentDialogFragment {
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setTitle(R.string.dialog_fragment_title)
.setMessage(R.string.dialog_message)
.setPositiveButton(R.string.dialog_button_positive,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// positive action
}
}
)
.setNegativeButton(R.string.dialog_button_negative,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// negative action
}
}
);
return builder.create();
}
}If you are using the support library, it provides its own DialogFragment implementation. It that case, you should have a look at the Support Library section in the Pro Tips & Tricks wiki page.
The last option is to start an Activity with a "dialog theme". Usually you apply the "Theme.Holo.Dialog" to your Activity and start the it normally. In our case, we will replace the theme by "Theme.HoloAccent.Dialog" or "Theme.HoloAccent.Light.Dialog":
<activity
android:name="com.example.activity.ExampleActivity"
android:theme="@style/Theme.HoloAccent.Dialog" />
And just like normal dialogs, we have to apply the accent color to the divider. We will do that in the onCreate() callback:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getAccentHelper().prepareDialog(this, getWindow());
}