I'm trying to make a custom theme for my android app. But the problem is that I can't change actionbar theme. When the style is applied, it gave me the following error and shutdown the app:
java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
I can't understand what I'm doing wrong. Here's my code:
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.liderlink.dev.acelink" >
<application
android:allowBackup="true"
android:icon="#drawable/icon"
android:label="#string/app_name"
android:theme="#style/AcelinkTheme">
<activity
android:name=".Dashboard"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
res/values/styles.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="android:Theme.Holo.Light">
<!-- Customize your theme here. -->
</style>
</resources>
res/values/themes.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<!-- the theme applied to the application or activity -->
<style name="AcelinkTheme"
parent="#android:style/Theme.Holo.Light">
<item name="android:actionBarStyle">#style/AcelinkActionBar</item>
</style>
<!-- colors -->
<color name="aceblue">#438eb9</color>
<!-- ActionBar styles -->
<style name="AcelinkActionBar"
parent="android:style/Widget.Holo.Light.ActionBar">
<item name="android:background">#color/aceblue</item>
</style>
</resources>
The exception is pretty clear : you need to use a theme that inherits from Theme.AppCompat, probably because you are using ActionBarActivity, which requires it as specified in the docs.
You can do this :
<style name="AcelinkTheme" parent="#android:style/Theme.AppCompat.Light">
<item name="android:actionBarStyle">#style/AcelinkActionBar</item>
</style>
<!-- ActionBar styles -->
<style name="AcelinkActionBar" parent="#android:style/Widget.AppCompat.ActionBar">
<item name="android:background">#color/aceblue</item>
</style>
For clarity, it is better to have colors defined in their own resource file IMHO.
According to this thread, you should change your parent to a Theme.AppCompat theme derivate:
<style name="AcelinkTheme"
parent="#style/Theme.AppCompat">
<item name="android:actionBarStyle">#style/AcelinkActionBar</item>
</style>
This link should also help you to customize your theme: Styling the Action Bar :)
Related
I'm trying to customize my Android theme, but nothing seems to work.
Activity:
public class MainActivity extends AppCompatActivity {
...
}
themes.xml
<resources xmlns:tools="http://schemas.android.com/tools">
<!-- Base application theme. -->
<style name="Theme.Delete" parent="Theme.MaterialComponents.Light">
<!-- Primary brand color. -->
<item name="colorPrimary">#color/primary</item>
<item name="colorPrimaryVariant">#color/primary</item>
<item name="colorOnPrimary">#color/white</item>
<!-- Secondary brand color. -->
<item name="colorSecondary">#color/primary</item>
<item name="colorSecondaryVariant">#color/primary</item>
<item name="colorOnSecondary">#color/black</item>
<!-- Status bar color. -->
<item name="android:statusBarColor" tools:targetApi="l">?attr/colorPrimaryVariant</item>
<!-- Customize your theme here. -->
<item name="materialButtonStyle">#style/Widget.App.Button</item>
</style>
<style name="Widget.App.Button" parent="Widget.MaterialComponents.Button">
<item name="materialThemeOverlay">#style/ThemeOverlay.Button</item>
<item name="colorPrimary">#color/primary</item>
</style>
<style name="ThemeOverlay.Button" parent="" >
<!-- For filled buttons, your theme's colorPrimary provides the default background color of the component-->
<item name="colorPrimary">#color/primary</item>
</style>
</resources>
In layout I'm using Material Design Components for example
<com.google.android.material.button.MaterialButton
android:id="#+id/button_home_all"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/home_all"
app:layout_constraintStart_toStartOf="parent"/>
but the default purple_200 background color is applied to the button. Can you help me a bit? I read that there I should use Activity instead of AppCompatActivity but I can't change it as my activity is a bottom navigation activity I can't find support for onSupportNavigateUp or other realated stuff
EDIT
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="licenta.allbank">
<application
android:requestLegacyExternalStorage="true"
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/Theme.AllBank"
android:usesCleartextTraffic="true">
<activity
android:name=".activity.MainActivity"
android:configChanges="keyboardHidden|orientation|screenSize"
android:label="#string/app_name"
android:launchMode="singleInstance"
android:windowSoftInputMode="adjustPan">
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data
android:host="callback"
android:scheme="https" />
</intent-filter>
</activity>
</application>
</manifest>
To use a MaterialButton you have to use a Theme.MaterialComponents.* theme in your app.
Then you can define the materialButtonStyle attribute.
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.MaterialComponents.Light">
....
<item name="materialButtonStyle">#style/Widget.App.Button</item>
</style>
In this way you can customize the theme of all the buttons in your app.
To override the default background color you can use the materialThemeOverlay attribute:
<style name="Widget.App.Button" parent="Widget.MaterialComponents.Button">
<item name="materialThemeOverlay">#style/ThemeOverlay.Button</item>
</style>
<style name="ThemeOverlay.Button" parent="" >
<!-- For filled buttons, your theme's colorPrimary provides the default background color of the component-->
<item name="colorPrimary">#color/my_color</item>
</style>
Otherwise you can use the backgroundTint attribute in your custom style:
<style name="Widget.App.Button" parent="Widget.MaterialComponents.Button">
<!-- Background color -->
<item name="backgroundTint">#color/buttonBackgroundSelector</item>
</style>
Figured it out. If you're testing on your phisical device and it is set on dark mode, you should make the changes in your values-night/themes.xml file, otherwise, working on your values/themes.xml should be enough
I 'm trying to develop and android app and I'm stuck with this error for days. I know there are similar questions byt reading them unfortunately didn't help me.
Here is my AndroidManifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.kostas.android.waveradio"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="17"
android:targetSdkVersion="23" />
<application
android:name="com.android.tools.fd.runtime.BootstrapApplication"
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme" >
<activity android:name="com.kostas.android.waveradio.MainActivity" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
My styles.xml file
<resources>
<style name="MyMaterialTheme" parent="MyMaterialTheme.Base">
</style>
<style name="MyMaterialTheme.Base" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="windowNoTitle">true</item>
<item name="windowActionBar">false</item>
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
</style>
<style name="MyMaterialTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />
<style name="MyMaterialTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />
</resources>
Thank you very much in advance.
You don't have any style in your styles.xml with the name "AppTheme".
E.G:
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar"> <!-- This means your Activity will be using the Light theme with no ActionBar -->
<!-- Customize your theme here. -->
<item name="colorPrimary">#color/primary</item> <!-- This is the primary color of your app, it is used for the ActionBar/Toolbar for example. -->
<item name="colorPrimaryDark">#color/primary_dark</item> <!-- Color of Status bar on API 21+ -->
<item name="colorAccent">#color/accent</item> <--This is the accent color, used for FloatingActionBar and other things like the EditText divider -->
</style>
Now, you're probably wondering "What are #color/primary" (otherwise you don't know what the error you're currently getting is).
Go to your colors.xml (res>values>colors.xml) and define primary, primary_dark, and accent with the colors that you want. (I suggest this site to do it for you as it picks good material colors):
<color name="primary"><!-- Your color --></color>
<color name="primary_dark"><!-- Your color --></color>
<color name="acent"><!-- Your color --></color>
Remember to replace <!-- Your color --> with your desired colors (such as #3367d6)
Here's an image explaining what each attribute does.
i define two theme in styles.xml
AppTheme1 and AppTheme2:
<resources>
<!-- Base application theme. -->
<style name="AppTheme1" 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>
<style name="AppTheme2" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">#00ff00</item>
<item name="colorPrimaryDark">#ff00ff</item>
<item name="colorAccent">#0000ff</item>
</style>
</resources>
when i change theme in application tag inside AndroidManifest.xml
my app theme doesn't any change and the app only use colors.xml and not styles.xml.
for example my manifest file:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.maneshtsoft.blackdictionary">
<application
android:allowBackup="true"
android:icon="#mipmap/favicondark"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme2">
<!-- OR AppTheme1 does not matter -->
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
just say that i use toolbar and AppCompactActivity maybe help to answer.
thanks.
The problem may be the cache of InstantRun.
Did you try "Clean & Rerun"? If you use AndroidStudio 2.1 or higher, Select Menu -> Run -> Clean & Rerun.
I'm a beginner in Android development and I trying to build my theme but I have a problem. I tried searching answers on google, d.android but to no avail.
ActionBar is changing color (this is OK), but text and icon of app doesn't show (only if I use my theme - if I use default theme everything is OK). Why?
res/values/styles.xml:
<resources xmlns:android="http://schemas.android.com/apk/res/android">
<style name="AppBaseTheme" parent="android:Theme.Light">
</style>
<!-- Application theme. -->
<style name="AppTheme" parent="AppBaseTheme">
<item name="android:actionBarStyle">#style/MyActionBar</item>
</style>
<style name="MyActionBar" parent="AppTheme">
<item name="android:background">HEX COLOR</item>
<item name="android:titleTextStyle">#style/MyTitleTextStyle</item>
</style>
<style name="MyTitleTextStyle" parent="AppTheme">
<item name="android:textColor">#FFFFFF</item>
</style>
</resources>
Here is my AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.appname"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="11"
android:targetSdkVersion="18" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.example.appname.Intro"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
"You should add following code in onCreate() method.
getSupportActionBar().setDisplayShowHomeEnabled(true);
getSupportActionBar().setIcon(R.drawable.ic_launcher); "
I see the problem that you use AppTheme as parent for MyActionBar style and it causes circular dependency. MyActionBar -> AppTheme -> MyActionBar ... You should use android:Widget.ActionBar or similar as parent of your action bar theme.
Also there should be android:Theme.Holo.Light instead of android:Theme.Light as parent of AppBaseTheme.
Here is how your res/values/styles.xml file should look like:
<resources xmlns:android="http://schemas.android.com/apk/res/android">
<style name="AppBaseTheme" parent="android:Theme.Holo.Light"/>
<!-- Application theme. -->
<style name="AppTheme" parent="AppBaseTheme">
<item name="android:actionBarStyle">#style/MyActionBar</item>
</style>
<style name="MyActionBar" parent="android:Widget.ActionBar">
<item name="android:background">HEX COLOR</item>
<item name="android:titleTextStyle">#style/MyTitleTextStyle</item>
</style>
<style name="MyTitleTextStyle" parent="AppTheme">
<item name="android:textColor">#FFFFFF</item>
</style>
</resources>
I have an application using action bar
styles.xml
<resources xmlns:android="http://schemas.android.com/apk/res/android">
<!--
Base application theme, dependent on API level. This theme is replaced
by AppBaseTheme from res/values-vXX/styles.xml on newer devices.
-->
<style name="AppBaseTheme" parent="android:Theme.Light">
<!--
Theme customizations available in newer API levels can go in
res/values-vXX/styles.xml, while customizations related to
backward-compatibility can go here.
-->
</style>
<!-- Application theme. -->
<style name="AppTheme" parent="AppBaseTheme">
<!-- All customizations that are NOT specific to a particular API-level can go here. -->
</style>
<color name="custom_theme_color">#ffa900</color>
<style name="CustomTheme" parent="android:Theme.Light">
<item name="android:windowBackground">#color/custom_theme_color</item>
<item name="android:colorBackground">#color/custom_theme_color</item>
</style>
</resources>
manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.actionbar"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="11"
android:targetSdkVersion="18" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/CustomTheme" >
<activity
android:name="com.example.actionbar.ActionBarActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
My Question is that initially by default it was using Theme.Light and action bar is displayed but when I have customized a theme using parent as Theme.Light it dosen't work but if I use parent as Theme.Holo then it works for custom theme. Anyone any Idea.
Here is what I have done. It is working perfectly for me. In Res->value->style
<?xml version="1.0" encoding="utf-8"?>
<resources>
<!-- the theme applied to the application or activity -->
<style name="CustomTheme"
parent="#style/Theme.Light">
<item android:name="actionBarStyle">#style/MyActionBar</item>
</style>
<!-- ActionBar styles -->
<style android:name="MyActionBar"
parent="#style/Widget.AppCompat.Light.ActionBar.Solid.Inverse">
<item android:name="background">#drawable/actionbar_background</item>
</style>
</resources>
Then in drawable folder(create one if you don't have it),create a new xml actionbar_background and paste the below code in it.
<?xml version="1.0" encoding="utf-8"?>
<bitmap android:tileMode="repeat"
android:src="#drawable/ab_texture_tile_example"
xmlns:android="http://schemas.android.com/apk/res/android"/>
Then you place your custom colour in the other drawable folders. Here ab_texture_title_example is my background colour used.