Android's Dialog APIs typical usage picks up the theme of the context (Activity) that is used to build the Dialog.
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(MainActivity.this);
The dialog is now going to be based on the Activity's Theme. However, there are cases when the Dialog needs to based on a different theme and AlertDialog has an overloaded Builder API to specify this custom theme.
public AlertDialog.Builder (Context context, int theme)
Note that the theme attribute isn't an explicit style (DialogTheme) instead is a reference attribute in the current theme that points to Dialog theme.
<style name="AppTheme" parent="android:Theme.Holo.Light.DarkActionBar">
<item name="android:alertDialogTheme">@style/DialogTheme</item>
</style>
<style name="DialogTheme" parent="android:Theme.Holo.Dialog">
...
</style>
Now, if MainActivity is based on the AppTheme, the builder could be initialized using the reference attribute. Note that the reference attribute could be a custom application specific attribute too and doesn't have to be based on the android's one.
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(MainActivity.this, android.R.attr.alertDialogTheme);
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(MainActivity.this);
The dialog is now going to be based on the Activity's Theme. However, there are cases when the Dialog needs to based on a different theme and AlertDialog has an overloaded Builder API to specify this custom theme.
public AlertDialog.Builder (Context context, int theme)
Note that the theme attribute isn't an explicit style (DialogTheme) instead is a reference attribute in the current theme that points to Dialog theme.
<style name="AppTheme" parent="android:Theme.Holo.Light.DarkActionBar">
<item name="android:alertDialogTheme">@style/DialogTheme</item>
</style>
<style name="DialogTheme" parent="android:Theme.Holo.Dialog">
...
</style>
Now, if MainActivity is based on the AppTheme, the builder could be initialized using the reference attribute. Note that the reference attribute could be a custom application specific attribute too and doesn't have to be based on the android's one.
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(MainActivity.this, android.R.attr.alertDialogTheme);
No comments:
Post a Comment