Android Tips & Traps

Using Your Theme Colours in AlertDialog

Since revision 22.1, the AppCompat library supports theming in AlertDialog by using android.support.v7.app.AlertDialog instead of the platform AlertDialog. However, you need to tell the library about your theme colours. First, define the AlertDialog theme colours:


	<style name="AppDlgTheme" parent="style/Theme.AppCompat.Light.Dialog.Alert" >
		<item name="colorPrimary">@color/primary</item>
		<item name="colorPrimaryDark">@color/primaryDark</item>
		<item name="colorAccent">@color/accent</item>
	</style>
						 

Then, add the style AppDlgTheme to your main theme:


	<style parent="@style/Theme.AppCompat.Light" name="AppTheme">
		<item name="colorPrimary">@color/primary</item>
		<item name="colorPrimaryDark">@color/primaryDark</item>
		<item name="colorAccent">@color/accent</item>
		// AlertDialog style
		<item name="alertDialogTheme">@style/AppDlgTheme</item>
	</style>
						 

NumberPicker getValue() method does not return last edited value

When the user edited the value of the NumberPicker using the soft keyboard just before closing the activity, the getValue() method does not return the updated value. To solve this problem, add a call to clearFocus() before calling getValue().