Android Studio 0.4.5
Android documentation for creating custom dialog boxes: http://developer.android.com/guide/topics/ui/dialogs.html
If you want a custom dialog, you can instead display an Activity as a dialog instead of using the Dialog APIs. Simply create an activity and set its theme to Theme.Holo.Dialog in
the <activity>
manifest element:
<activity android:theme="@android:style/Theme.Holo.Dialog" >
However, when I tried this I get the following exception:
java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity
I am supporting the following, and I can't using something greater than 10 for the min:
minSdkVersion 10
targetSdkVersion 19
In my styles I have the following:
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
And in my manifest I have this for the activity:
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:theme="@android:style/Theme.Holo.Light.Dialog"
android:name="com.ssd.register.Dialog_update"
android:label="@string/title_activity_dialog_update" >
</activity>
Creating the dialog box like this was something I was hopping to do, as I have already completed the layout.
Can anyone tell me how I can get around this problem?
The reason you are having this problem is because the activity you are trying to apply the dialog theme to is extending
ActionBarActivity
which requires theAppCompat
theme to be applied.Update: Extending
AppCompatActivity
would also have this problemIn this case, change the Java inheritance from
ActionBarActivity
toActivity
and leave the dialog theme in the manifest as it is, a nonTheme.AppCompat
valueThe general rule is that if you want your code to support older versions of Android, it should have the
AppCompat
theme and the java code should extendAppCompatActivity
. If you have *an activity that doesn't need this support, such as you only care about the latest versions and features of Android, you can apply any theme to it but the java code must extend plain oldActivity
.NOTE: When change from
AppCompatActivity
(or a subclass,ActionBarActivity
), toActivity
, must also change the various calls with "support" to the corresponding call without "support". So, instead ofgetSupportFragmentManager
, callgetFragmentManager
.This is when you want a AlertDialog in a Fragment
This solution worked for me.
Design library depends on the Support v4 and AppCompat Support Libraries, so don't use different version for appcompat and design library in gradle.
use
instead of
min sdk is 10.
ActionBar
is available from api level 11. So for 10 you would be usingAppCompat
from the support library for which you need to useTheme.AppCompat
or descendant of the same.Use
Or if you dont want action bar at the top
More info @
http://developer.android.com/guide/topics/ui/actionbar.html
Edit:
I might have misread op post.
Seems op wants a Dialog with a Activity Theme. So as already suggested by Bobbake4 extend
Activity
instead ofActionBarActivity
.Also have a look @ Dialog Attributes in the link below
http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/4.4_r1/frameworks/base/core/res/res/values/themes.xml/
This one worked for me:
In my experiences the problem was the context where I showed my dialog. Inside a button click I instantiate an AlertDialog in this way:
But the context was not correct and caused the error. I've changed it using the application context in this way:
In declare section:
in the onCreate method
And finally in the code where I need the AlertDialog:
This is the solution for me.