Trying to convert this project from ActionBarSherlock 4.2.0 to ActionBarCompat I hit this problem where a couple activities were using Theme.Sherlock.Dialog theme and there's no similar available anymore.
So here is my attempt to port that over into an ActionBarCompat project, inspiring from ActionBarSherlock theme (longer term note, I should be looking at using DialogFragment(s) instead, but here it is now for transition).
So here is my attempt to port that over into an ActionBarCompat project, inspiring from ActionBarSherlock theme (longer term note, I should be looking at using DialogFragment(s) instead, but here it is now for transition).
We're going to define two separate styles, one for older and one for the newer APIs, with the latter one using the real dialog theme:
<style name="AppTheme.Dialog" parent="android:Theme.Light"> <item name="android:windowNoTitle">true</item> <item name="android:windowFrame">@null</item> <item name="android:windowIsFloating">true</item> <item name="android:windowContentOverlay">@null</item> <item name="android:windowBackground">@drawable/dialog_full_holo_light</item> <item name="android:maxWidth">600dp</item> <item name="android:windowAnimationStyle">@android:style/Animation.Dialog</item> </style>
<style name="AppTheme.Dialog" parent="android:Theme.Holo.Light.Dialog"> <item name="android:windowNoTitle">true</item> <item name="android:windowCloseOnTouchOutside">true</item> </style>
The background drawable is coming from the Android project resources or if you have downloaded them already using Android SDK Manager, copy from ANDROID_SDK/platforms/android-xx/data/res/. That makes for the nice rounded corners, shadow and the spacing on each side.
Now we're going to use the new theme with our "dialog" activity:
<activity android:name="DialogActivity" android:theme="@style/AppTheme.Dialog" />
Now to the actual DialogActivity, there's nothing special about it but you might notice that it doesn't extend ActionBarActivity, but instead a plain Activity, and that is for a few reasons. For one, it doesn't need it, it's not like the dialog will have an action bar (nor that it's possible I believe), but more importantly you will hit a few issues with that.
- ActionBarActivity requires a theme that extends from Theme.AppCompat, otherwise your app will crash with this error:
java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
You could build a dialog theme that's based on that, but you won't be able to use the real Holo dialog themes with later APIs, like we did above.
- More importantly, even if you fix point 1, you will hit this error on older APIs, as ActionBarView checks for size and it will crash with:
java.lang.IllegalStateException: ActionBarView can only be used with android:layout_width="MATCH_PARENT" (or fill_parent)
public class DialogActivity extends Activity { [...] }
* * *
Source code (Google Code - SVN):
Prerequisites :
Resources:
nice post mate, but you havent used windowCloseOnTouchOutside in the older values file, does that mean it wont happen there? thanks in advaNCE
ReplyDeleteCorrect, android:windowCloseOnTouchOutside appears in API 11 (Android 3.0).
ReplyDeletehttp://developer.android.com/reference/android/R.attr.html#windowCloseOnTouchOutside