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?
This question is related to
android
android-layout
I was experiencing this problem even though my Theme was an AppCompat
Theme and my Activity was an AppCompatActivity
(or Activity
, as suggested on other's answers). So I cleaned, rebuild and rerun the project.
(Build -> Clean Project ; Build -> Rebuild Project ; Run -> Run)
It may seem dumb, but now it works great!
Just hope it helps!
go to your styles and put the parent
parent="Theme.AppCompat"
instead of
parent="@android:style/Theme.Holo.Light"
This is when you want a AlertDialog in a Fragment
AlertDialog.Builder adb = new AlertDialog.Builder(getActivity());
adb.setTitle("My alert Dialogue \n");
adb.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
//some code
} });
adb.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
} });
adb.show();
I had an activity with theme <android:theme="@android:style/Theme.Dialog">
used for showing dialog in my appWidget and i had same problem
i solved this error by changing activity code like below:
@Override
protected void onCreate(Bundle savedInstanceState) {
setTheme(R.style.Theme_AppCompat_Dialog); //this line i added
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_dialog);
}
This really forced me to post my own answer.
Since i am using Theme.AppCompat.Light.NoActionBar
, and also replaced all AlertDialog
instances with support compatibility imports and still faced problems below v21 (Lollipop).
I didn't like idea of changing theme which was proper.
So after 2 days, I finally gave some thought about the other libraries that are specifying AppTheme
for their AndroidManifest.xml
.
I found out that there is yet another one: Paytm Checkout SDK.
Thus, the following changes fixed the problem.
AppTheme
using for my app to 'XYZAppTheme'using tools
:replace method in AndroidManifest of my project(app).
<uses-permission android:name="android.permission.INTERNET" />
<application
android:name=".XYZ"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher_v2"
android:label="@string/app_name"
android:largeHeap="true"
android:roundIcon="@mipmap/ic_launcher_v2_round"
android:supportsRtl="true"
android:theme="@style/XYZAppTheme"
tools:replace="android:icon,android:theme">
<activity
android:name=".xyz.SplashActivity"
android:launchMode="singleTask"
android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
just make that
getApplicationContext().getTheme().applyStyle(R.style.Theme_Mc, true);
and In your values/styles.xml add this
<style name="Theme.Mc" parent="Theme.AppCompat.Light.NoActionBar">
<!-- ADD Your Styles -->
</style>
For me, the Android SDK didn't seem to be able to find the styles definition. Everything was wired correctly and doing a simple project clean fixed it for me.
Do not forget to clean the project after VCS Local History restore
In case anybody still wondering about this issue.
try this :
new android.support.v7.app.AlertDialog.Builder(this)
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
compile 'com.android.support:appcompat-v7:23.0.0'
compile 'com.android.support:design:23.0.0'
instead of
compile 'com.android.support:appcompat-v7:23.0.0'
compile 'com.android.support:design:23.1.0'
When You're Using Kotlin, nothing solved my problem until I change the Builder parameter from 'applicationContext' to 'this'.
This Does Not Work
val dialogDelete = AlertDialog.Builder(applicationContext)
.setTitle("Confirmation")
.setMessage("Delete this photo?")
.setNegativeButton(android.R.string.no){ it, which ->
it.dismiss()
}
dialogDelete.show()
Following Code Works
val dialogDelete = AlertDialog.Builder(this)
.setTitle("Confirmation")
.setMessage("Delete this photo?")
.setNegativeButton(android.R.string.no){ it, which ->
it.dismiss()
}
dialogDelete.show()
Your Activity is extending ActionBarActivity which requires the AppCompat.theme to be applied. Change from ActionBarActivity to Activity or FragmentActivity, it will solve the problem.
If you use no Action bar then :
android:theme="@android:style/Theme.Light.NoTitleBar.Fullscreen"
min sdk is 10. ActionBar
is available from api level 11. So for 10 you would be using AppCompat
from the support library for which you need to use Theme.AppCompat
or descendant of the same.
Use
android:theme="@style/Theme.AppCompat" >
Or if you dont want action bar at the top
android:theme="@style/Theme.AppCompat.NoActionBar">
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 of ActionBarActivity
.
Also have a look @ Dialog Attributes in the link below
First of all, add this as import => import androidx.appcompat.app.AlertDialog
I am posting this being the smallest ever solution to the problem. I just changed the instantiation of
new AlertDialog.Builder(mContex)
to
new AlertDialog.Builder(mContext, R.style.PreferenceDialogLight)
Where <style name="PreferenceDialogLight" parent="Base.Theme.MaterialComponents.Dialog.Alert">
My Activity with SectionsPagerAdapter and ViewPager & Fragment
public class MyActivity extends AppCompatActivity implements ActionBar.TabListener
...
...
@Override
public void onPostResume(){
super.onPostResume();
try {
getSupportActionBar().setDisplayShowTitleEnabled(false);
}catch (NullPointerException ignored){
}
}
In my case, I had AppTheme
in AndroidManifest
set correctly to a style that inherited from Theme.AppCompat
. However, the individual activities had style settings in AndroidManifest
that were overriding that.
In my case such issue was appear when i tried to show Dialog. The problem was in context, I've use getBaseContext() which theoretically should return Activity context, but appears its not, or it return context before any Theme applied.
So I just replaced getBaseContexts() with "this", and now it work as expected.
Dialog.showAlert(this, title, message,....);
Instead of all of this, about changing a lot of this in your app.. just do it simple as this video does.
https://www.youtube.com/watch?v=Bsm-BlXo2SI
I already use it so... it's a 100 percent effective.
In my case, I was using a style called "FullScreenTheme", and although it seemed to be correctly defined (it was a descendant of Theme.AppCompat
) it was not working.
I finally realized that I was using an AAR library that internally also had defined a style called "FullScreenTheme" and it was not descendant of Theme.AppCompat
. The clashing of the names was causing the problem.
Fix it by renaming my own style name so now Android is using the correct style.
I had the same problem, but it solved when i put this on manifest: android:theme="@style/Theme.AppCompat.
<application
android:allowBackup="true"
android:icon="@drawable/icon"
android:label="@string/app_name_test"
android:supportsRtl="true"
android:theme="@style/Theme.AppCompat">
...
</application>
for me a solution, after trying all solutions from here, was to change
<activity
android:name="com.github.cythara.MainActivity"
android:label="Main">
</activity>
to include a theme:
<activity
android:name="com.github.cythara.MainActivity"
android:theme="@style/Theme.AppCompat.NoActionBar"
android:label="Main">
</activity>
You have many solutions to that error.
You should use Activity or FragmentActivity instead of ActionbarActivity or AppCompatActivity
If you want use ActionbarActivity or AppCompatActivity, you should change in styles.xml Theme.Holo.xxxx to Theme.AppCompat.Light (if necessary add to DarkActionbar)
If you don't need advanced attributes about action bar or AppCompat you don't need to use Actionbar or AppCompat.
This one worked for me:
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name"
android:theme="@style/Theme.AppCompat.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
In my case i have no values-v21 file in my res directory. Then i created it and added in it following codes:
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
NOTE: I had intended this as an answer, but further testing reveals it still fails when built using maven from the command line, so I've had to edit it to be a problem! :-(
In my case when I got this error I was already using a AppCompat Theme and the error didn't make much sense.
I was in the process of mavenizing my android build. I had already dependencies on the apklib and jar versions of app compat, thus:
<!-- See https://github.com/mosabua/maven-android-sdk-deployer -->
<dependency>
<groupId>android.support</groupId>
<artifactId>compatibility-v7-appcompat</artifactId>
<version>${compatibility.version}</version>
<type>apklib</type>
</dependency>
<dependency>
<groupId>android.support</groupId>
<artifactId>compatibility-v7-appcompat</artifactId>
<type>jar</type>
</dependency>
<dependency>
<groupId>android.support</groupId>
<artifactId>compatibility-v7</artifactId>
<type>jar</type>
</dependency>
<dependency>
<groupId>android.support</groupId>
<artifactId>compatibility-v4</artifactId>
</dependency>
Now, when I import the maven project and build and run from IntelliJ it's fine.
But when I build and deploy and run from the command line with maven I still get this exception.
If you need to extend ActionBarActivity you need on your style.xml:
<!-- Base application theme. -->
<style name="AppTheme" parent="AppTheme.Base"/>
<style name="AppTheme.Base" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
If you set as main theme of your application as android:Theme.Material.Light instead of AppTheme.Base then you’ll get an “IllegalStateException:You need to use a Theme.AppCompat theme (or descendant) with this activity” error.
In my case, i was inflating a view
with ApplicationContext
. When you use ApplicationContext
, theme/style is not applied, so although there was Theme.Appcompat
in my style, it was not applied and caused this exception. More details: Theme/Style is not applied when inflater used with ApplicationContext
If you are using the application context, like this:
final AlertDialog.Builder builder = new AlertDialog.Builder(getApplicationContext());
change it to an activity context like this:
final AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
For my Xamarin Android project (in MainActivity.cs), I changed…
public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
to
public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsApplicationActivity
…and the error went away. I realise that isn't a solution for everyone but it might give a clue to the underlying problem.
I had this problem as well and what I did to fix it, AND still use the Holo theme was to take these steps:
first I replaced this import:
import android.support.v7.app.AppCompatActivity;
with this one:
import android.app.Activity;
then changed my extension from:
public class MyClass extends AppCompatActivity {//...
to this:
public class MyClass extends Activity {//...
And also had to change this import:
import android.support.v7.app.AlertDialog;
to this import:
import android.app.AlertDialog;
and then you can use your theme tag in the manifest at the activity level:
android:theme="@android:style/Theme.Holo.Dialog" />
and lastly, (unless you have other classes in your project that has to use v7 appCompat) you can either clean and rebuild your project or delete this entry in the gradle build file at the app level:
compile 'com.android.support:appcompat-v7:23.2.1'
if you have other classes in your project that has to use v7 appCompat then just clean and rebuild the project.
You have came to this because you want to apply Material Design in your theme style in previous sdk versions to 21. ActionBarActivity
requires AppTheme
so if you also want to prevent your own customization about your AppTheme, only you have to change in your styles.xml (previous to sdk 21) so this way, can inherit for an App Compat theme.
<style name="AppTheme" parent="android:Theme.Holo.Light.DarkActionBar">
for this:
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
I had such crash on Samsung devices even though the activity did use Theme.AppCompat. The root cause was related to weird optimizations on Samsung side:
- if one activity of your app has theme not inherited from Theme.AppCompat
- and it has also `android:launchMode="singleTask"`
- then all the activities that are launched from it will share the same Theme
My solution was just removing android:launchMode="singleTask"
Copying answer from @MarkKeen in the comments above as I had the same problem.
I had the error stated at the top of the post and happened after I added an alert dialog. I have all the relevant style information in the manifest. My problem was cured by changing a context reference in the alert builder - I changed:
new android.support.v7.app.AlertDialog.Builder(getApplicationContext())
to:
new android.support.v7.app.AlertDialog.Builder(this)
And no more problems.
This is what fixed it for me: instead of specifying the theme in manifest, I defined it in onCreate
for each activity that extends ActionBarActivity
:
@Override
protected void onCreate(Bundle savedInstanceState) {
setTheme(R.style.MyAppTheme);
super.onCreate(savedInstanceState);
setContentView(R.layout.my_activity_layout);
...
}
Here MyAppTheme
is a descendant of Theme.AppCompat
, and is defined in xml. Note that the theme must be set before super.onCreate
and setContentView
.
In Android Studio:
Add support library to the gradle file and sync.
Edit build.gradle(Module:app)
to have dependencies as follows:
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
implementation 'com.android.support:appcompat-v7:23.1.1'
implementation 'com.android.support:design:23.1.1'
implementation 'com.android.support:support-v4:23.1.1'
}
My support Library version is 23.1.1
; use your support library version as applicable.
All you need to do is add android:theme="@style/Theme.AppCompat.Light"
to your application tag in the AndroidManifest.xml
file.
If using getApplicationContext() as the parameter while inflating the layout, you can try to use getBaseContext() instead. e.g.
View.inflate(getBaseContext(),
getLayoutId() == 0 ? R.layout.page_default : getLayoutId(),
null);
For me none of the above answers worked even after I had Theme.AppCompat
as my base theme for the application. I was using com.android.support:design:24.1.0
So I just changed the version to 24.1.1
. After the gradle sync, Its working again and the exception went away. Seems to me the issue was with some older versions of design support libraries.
Just putting this here in case other answers don't work for some people.
Change your theme style parent to
parent="Theme.AppCompat"
This worked for me ...
for me was solution to use ContextThemeWrapper:
private FloatingActionButton getFAB() {
Context context = new android.support.v7.view.ContextThemeWrapper(getContext(), R.style.AppTheme);
FloatingActionButton fab = new FloatingActionButton(context);
return fab;}
Make sure you are using an activity context while creating a new Alert Dialog and not an application or base context.
Change the theme of the desired Activity. This works for me:
<activity
android:name="HomeActivity"
android:screenOrientation="landscape"
android:theme="@style/Theme.AppCompat.Light"
android:windowSoftInputMode="stateHidden" />
In your app/build.gradle
add this dependency:
implementation "com.google.android.material:material:1.1.0-alpha03"
Update your styles.xml
AppTheme's parent:
<style name="AppTheme" parent="Theme.MaterialComponents.Light.NoActionBar"/>
In Android manifest just change theme of activity to AppTheme as follow code snippet
<activity
android:name=".MainActivity"
android:label="@string/app_name"
android:theme="@style/AppTheme">
</activity>
If you are struggling with the Recyclerview Adapter class then use
view.getRootView().getContext()
instead of
getApplicationContext() or activity.this
I was getting this same problem. Because i was creating custom navigation drawer. But i forget to mention theme in my manifest like this
android:theme="@style/Theme.AppCompat.NoActionBar"
As soon i added the above the theme to my manifest it resolved the problem.
For me issue resolved by changing the inheritance from AppCompatActivity to Activity in my customDialog class. No changes required in manifest for Theme.Dialog.
In my experiences the problem was the context where I showed my dialog. Inside a button click I instantiate an AlertDialog in this way:
builder = new AlertDialog.Builder(getApplicationContext());
But the context was not correct and caused the error. I've changed it using the application context in this way:
In declare section:
Context mContext;
in the onCreate method
mContext = this;
And finally in the code where I need the AlertDialog:
start_stop = (Button) findViewById(R.id.start_stop);
start_stop.setOnClickListener( new View.OnClickListener()
{
@Override
public void onClick(View v)
{
if (!is_running)
{
builder = new AlertDialog.Builder(mContext);
builder.setMessage("MYTEXT")
.setCancelable(false)
.setPositiveButton("SI", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
Task_Started = false;
startTask();
}
})
.setNegativeButton("NO",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
AlertDialog alert = builder.create();
alert.show();
}
}
}
This is the solution for me.
Quick solution.
Change your base theme parent in styles.xml
Replace from
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
to
<style name="AppTheme" parent="Theme.AppCompat">
Just Do
new AlertDialog.Builder(this)
Instead of
new AlertDialog.Builder(getApplicationContext())
Source: Stackoverflow.com