when I set thestyle in this:
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>
the actionbar will go away.
however,when I set it in this:
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
<item name="android:windowActionBar">false</item>
<item name=“android:windowNoTitle">true</item>
</style>
the actionbar is still here.
what's the difference ?
windowActionBar is an attribute provided in AppCompat library, where as android:windowActionBar is provided in Material theme.
The action bar is going away when you set below code, just because you are using AppCompat library and referring the attribute provided in this library itself:
<item name="windowActionBar">false</item>
On another points, it's same as colorPrimary and android:colorPrimary attribute and all other similar attributes.
For example:
We were using Material theme and referring android:colorPrimary attribute as below:
<style name="AppTheme" parent="android:Theme.Material.Light">
<item name="android:colorPrimary">#color/primary</item>
<item name="android:colorPrimaryDark">#color/primary_dark</item>
....
....
</style>
but we are now using AppCompat library with only colorPrimary attribute, for providing compatibility to lower versions.
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
...
...
</style>
android:windowActionBar denotes property for lollipop and above only. Where as windowActionBar denotes for all versions and is retrieved from support library.
Related
I want to use colorAccent from application theme. There are two themes with different accent colors. I am implement it with this code:
Styles:
<style name="BaseAppTheme" parent="Theme.MaterialComponents.Light.DarkActionBar">
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
</style>
<style name="AppGreenTheme" parent="BaseAppTheme">
<item name="colorAccent">#color/green</item>
</style>
<style name="AppRedTheme" parent="BaseAppTheme">
<item name="colorAccent">#color/red</item>
</style>
Layout:
<FrameLayout
android:id="#+id/receives_funds_share"
android:background="?colorAccent"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
But it shows wrong color (#04dac6). When the correct color is darker (#05c0a5).
When I changed it from android:background="?colorAccent" to android:background="#color/green" it works well. What it the difference? How to correctly retrieve accentColor from style?
Tested on Android 11.
The thing is, since you are using Theme.MaterialComponents.Light.DarkActionBar as your parent, colorAccent does not apply. Instead try replacing it with colorSecondary as follows.
The Theme.MaterialComponents uses the colorSecondary and is so described in their docs. All the components defined in the library uses this attribute.
<style name="BaseAppTheme" parent="Theme.MaterialComponents.Light.DarkActionBar">
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
</style>
<style name="AppGreenTheme" parent="BaseAppTheme">
<item name="colorSecondary">#color/green</item>
</style>
<style name="AppRedTheme" parent="BaseAppTheme">
<item name="colorSecondary">#color/red</item>
</style>
You should set the theme to the activity in mainfest.xml:
<activity
android:name=".activities.MainActivity"
android:theme="#style/AppGreenTheme">
</activity>
I'm trying to set a custom colour for my buttons in Android application but value set for accentColor is always used as button background colour. I am trying to set a global colour for all buttons without having to specify styles or theme for each button individually.
Can you explain please how styling works in Android? I understand the basics, but overriding existing themes is never working for me :(
styles.xml
<resources>
<style name="AppTheme" parent="Theme.MaterialComponents.Light.NoActionBar">
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
<item name="android:titleTextColor">#ffffff</item>
<item name="android:buttonStyle">#style/AppTheme.Button</item>
</style>
<style name="AppTheme.NoActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>
<style name="AppTheme.Button" >
<item name="android:backgroundTint">#color/colorPrimaryDark</item>
</style>
<style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />
<style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />
Short answer: Try going to the source code of how the
Theme.MaterialComponents.Light.NoActionBar is defining the style for
the buttons and override those attributes.
Android styling works by merging all the defined attributes in a theme. For example, you have an AppTheme that includes theming for every built-in view. If you create a new theme for your button and your define just a couple of attributes, what android studio does is to merge all the attributes defined in the AppTheme with your new attributes. So if you want to override the buttons, you need to go deep and find out what are exactly the attributes defined in the general AppTheme you set in your application, they may not be the same.
For example, this is how I changed the color of all the buttons in my app since they were using a global attribute for it:
In the general styles.xml file, I put:
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
...
<item name="colorButtonNormal">?myCustomColor</item>
...
</style>
But if I wanted to change the backgroundTint attribute of my buttons, I had to do this:
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
...
<item name="colorButtonNormal">?myCustomColor</item>
<item name="android:buttonStyle">#style/AppButtonStyle</item>
...
</style>
<style name="AppButtonStyle" parent="Widget.AppCompat.Button">
<item name="android:backgroundTint">?attr/colorAccent</item>
</style>
Also, check out this cool Medium post for another developer experience in this topic.
I managed to solve the issue. The main point to notice here is that I am using Theme.MaterialComponents and not AppCompact.
I have defined a new style and I set it to buttonStyle, which refers to style for AppCompact button. In order to set a style for the material button, I had to use "materialButtonStyle" instead of "buttonStyle".
All relevant properties that can be overridden for MaterialComponents can be found in material design documentation: https://www.material.io/develop/android/components/material-button/
So the final code looks like this:
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.MaterialComponents.Light.NoActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
<item name="colorSecondary">#color/colorAccent</item>
<item name="floatingActionButtonStyle">#style/AppTheme.Test</item>
<item name="materialButtonStyle">#style/AppTheme.Button</item>
<!--<item name="buttonStyle">#style/Widget.MaterialComponents.Button.Icon</item>-->
</style>
<style name="AppTheme.Button" parent="#style/Widget.MaterialComponents.Button">
<item name="android:backgroundTint">#D400EB</item>
</style>
<style name="AppTheme.Test" parent="#style/Widget.MaterialComponents.FloatingActionButton">
<item name="backgroundTint">#A305B1</item>
</style>
<style name="AppTheme.NoActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>
<style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar"/>
<style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light"/>
</resources>
I cannot see my layouts in the layout editor. The error message is whats in the title.
Not sure how many of these topics need to exist and why Android is such a black hole when it comes to issues, but I'm still having problem with this:
This project uses:
implementation 'com.android.support:appcompat-v7:28.0.0'
implementation 'com.android.support:support-v13:28.0.0'
What I have in my styles file:
<style name="AppTheme" parent="Theme.AppCompat.Light">
<!-- Customize your theme here. -->
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
<item name="android:windowContentOverlay">#null</item>
<item name="drawerArrowStyle">#style/DrawerArrowStyle</item>
<item name="colorControlNormal">#color/menu_yellow</item>
<item name="colorControlActivated">#color/menu_yellow</item>
<item name="colorControlHighlight">#color/menu_yellow</item>
</style>
So the idea was to replace it to Base.Theme.AppComat.Light based on other answers:
<!-- Base application theme. -->
<style name="AppTheme" parent="Base.Theme.AppCompat.Light">
<!-- Customize your theme here. -->
...
</style>
Now this just gives me a "resource not found (Base.Theme.AppCompat.Light)"
Either way I have tried a lot of suggested solutions back from 28 alpha but none of them work now with released 28.
try this way
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
<item name="android:windowContentOverlay">#null</item>
<item name="drawerArrowStyle">#style/DrawerArrowStyle</item>
<item name="colorControlNormal">#color/menu_yellow</item>
<item name="colorControlActivated">#color/menu_yellow</item>
<item name="colorControlHighlight">#color/menu_yellow</item>
</style>
maybe this help you
in the styles.xml replace
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
with
<style name="AppTheme" parent="Base.Theme.AppCompat.Light.DarkActionBar">
I tried to change the default icon of HomeAsUpEnabled by editing my styles.xml file. But my icon did't changed. I am working on min sdk version:23. Below is my styles.xml file.
My styles.xml file
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
<item name="android:homeAsUpIndicator">#drawable/back</item>
<item name="homeAsUpIndicator">#drawable/back</item>
</style>
Use the Toolbar and call toolbar.setNavigationIcon(). See more on toolbar here http://developer.android.com/reference/android/widget/Toolbar.html
LOG_CAT :
Caused by: java.lang.IllegalArgumentException: AppCompat does not support the current theme features: { windowActionBar: false, windowActionBarOverlay: false, android:windowIsFloating: false, windowActionModeOverlay: false, windowNoTitle: false }
at android.support.v7.app.AppCompatDelegateImplV7.ensureSubDecor(AppCompatDelegateImplV7.java:371)
at android.support.v7.app.AppCompatDelegateImplV7.initWindowDecorActionBar(AppCompatDelegateImplV7.java:173)
at android.support.v7.app.AppCompatDelegateImplBase.getSupportActionBar(AppCompatDelegateImplBase.java:87)
at android.support.v7.app.AppCompatDelegateImplV7.setSupportActionBar(AppCompatDelegateImplV7.java:197)
at android.support.v7.app.AppCompatActivity.setSupportActionBar(AppCompatActivity.java:99)
at de.memorian.playpal.MainActivity.afterInject(MainActivity.java:72)
if your using AppCompatActivity
try extending Activity instead of AppCompatActivity
public class ActivityName extends Activity
then change the theme in values/styles.xml file:
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
to
<style name="AppTheme" parent="android:Theme.Material">
these should be compatible together.
but it requires min API level 21.
I hope this helps.
check your styles.xml like below
<style name="MyMaterialTheme" parent="MyMaterialTheme.Base">
</style>
<style name="MyMaterialTheme.Base" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="windowActionBar">false</item>
<item name="android:windowNoTitle">true</item>
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
</style>
I got the same problem as well and after a couple of hour of study i came up with this sollution
Before i had this this
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
<item name="android:textColorPrimary">#color/colorTextPrimary</item>
<item name="android:textColorSecondary">#color/colorTextSecondary</item>
</style>
<style name="AppTheme.FullScreen" parent="#style/Theme.AppCompat.Light">
<item name="windowNoTitle">true</item>
<item name="windowActionBar">false</item>
<item name="android:windowFullscreen">true</item>
<item name="android:windowContentOverlay">#null</item>
</style>
According to the documentation they are doing many changes in the new release hence they made strict on later version in appcompat. I change the the parent theme and it works fine , use the below code.
<!-- Base application theme. -->
<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>
<item name="android:textColorPrimary">#color/colorTextPrimary</item>
<item name="android:textColorSecondary">#color/colorTextSecondary</item>
</style>
<style name="AppTheme.FullScreen">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>
thanks