Please help me.
I got struck in solving this error for more than 3 days
I didnot get the proper solution.
I have updated my eclipse to run android 5.0.
I had created a simple android application using minimum version 16 and target version 21 to run android 5.0 material theme.
Have added the theme in styles.xml as like this
<resources>
<!-- inherit from the material theme -->
<style name="AppTheme" parent="android:Theme.Material">
<!-- Main theme colors -->
<!-- your app branding color for the app bar -->
<item name="android:colorPrimary">#color/primary</item>
<!-- darker variant for the status bar and contextual app bars -->
<item name="android:colorPrimaryDark">#color/primary_dark</item>
<!-- theme UI controls like checkboxes and text fields -->
<item name="android:colorAccent">#color/accent</item>
</style>
</resources>
I followed the link to add supporting libraries appcompatv7.
http://developer.android.com/tools/support-library/features.html#v7
But I am getting error stating that R cannot be resolved to variable
whenever I add this appcompat to my project.
please give me a suggestions and also please share your android 5.0 running in lower version project.
for api lower than 21 use Theme.AppCompat instead of android:Theme.Material
please read here http://developer.android.com/training/material/compatibility.html
Your theme.xml should be something like this.
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="AppTheme" parent="AppTheme.Base"/>
<style name="AppTheme.Base" parent="Theme.AppCompat.Light.NoActionBar">
<item name="colorPrimary">#color/grey_main</item>
<item name="colorPrimaryDark">#color/grey_main</item>
<item name="android:windowNoTitle">true</item>
<item name="windowActionBar">false</item>
</style>
</resources>
check your mainactivity.java.
your r.java file could have not generated so that you might get error mentioning R cannot be Resolved
Must add Add appcompat into your project library
sdk\extras\android\compatibility\v7\appcompat
Add AppBaseTheme
<style name="AppBaseTheme" parent="Theme.AppCompat">
read compatibility for material theme and add appcompat into your project as library that you can find in
sdk\extras\android\compatibility\v7\appcompat
than
your style.xml
<style name="AppBaseTheme" parent="Theme.AppCompat">
Related
I have this in my styles.xml:
<style name="UserTheme" parent="ThemeBase">
<item name="android:editTextStyle">#style/EditTextTheme</item>
</style>
Why do I have to repeat the editTextStyle line in v19/styles.xml and v21/styles.xml.
v21/styles.xml:
<style name="UserTheme" parent="ThemeBase">
<item name="android:windowTranslucentStatus">true</item>
<item name="android:windowTranslucentNavigation">true</item>
<item name="android:editTextStyle">#style/EditTextTheme</item>
</style>
Is there a way to just call it in the main styles.xml and have it apply everywhere so I don't have to write it multiple times?
I couldn't find any recommended solution so I i digged into AppCompat source. The way they do it is like this.
In your styles.xml
<style name="Base.V7.Theme.YourThemeName" parent="Theme.AppCompat.Light.NoActionBar">
</style>
<style name="Base.Theme.YourThemeName" parent="Base.V7.Theme.YourThemeName" />
<style name="Theme.YourThemeName" parent="Base.Theme.YourThemeName" >
<item name="colorPrimary">#color/primary</item>
<item name="colorPrimaryDark">#color/primary_dark</item>
<item name="colorAccent">#color/accent</item>
</style>
In your styles-v21.xml
<style name="Base.V21.Theme.YourThemeName" parent="Base.V7.Theme.YourThemeName">
<item name="android:navigationBarColor">#color/white</item>
<item name="android:windowTranslucentStatus">true</item>
</style>
<style name="Base.Theme.YourThemeName" parent="Base.V21.Theme.YourThemeName" />
In your styles-v22.xml
<style name="Base.V22.Theme.YourThemeName" parent="Base.V21.Theme.YourThemeName">
<item name="android:navigationBarColor">#color/black</item>
<item name="android:windowTranslucentStatus">false</item>
</style>
<style name="Base.Theme.YourThemeName" parent="Base.V22.Theme.YourThemeName" />
For every new version you extend the previous base version. If you want to override any attribute for different version just put it inside Base.VXX.Theme.YourThemeName block on your new styles-vXX.xml file.
Why do I have to repeat the editTextStyle line in v19/styles.xml and
v21/styles.xml?
If you've applied some STYLE to some attribute, Android will search styles.xml file for highest api level for which file_api_level<=Android_device_api_level and searches for STYLE in it. If it finds it would apply that STYLE to view otherwise will continue searching for the STYLE in lower api level files.
e.g. - If you have four files styles.xml(default), v19/styles.xml, v21/styles.xml, v25/styles.xml and your devices is running on api level 24. Then it'll search for STYLE in v21/styles.xml first, then v19/styles.xml and finally in styles.xml(default). Only first occurrence of the STYLE will get applied. So you can't just define only extra attributes in version-specific styles.xml file.
If you don't want to repeat common attributes here is an alternate. To declare window transitions for Android 5.0 (API level 21) and higher, you need to use some new attributes. So your base theme in res/values/styles.xml could look like this:
<resources>
<!-- base set of styles that apply to all versions -->
<style name="BaseAppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="colorPrimary">#color/primaryColor</item>
<item name="colorPrimaryDark">#color/primaryTextColor</item>
<item name="colorAccent">#color/secondaryColor</item>
</style>
<!-- declare the theme name that's actually applied in the manifest file -->
<style name="AppTheme" parent="BaseAppTheme" />
</resources>
Then add the version-specific styles in res/values-v21/styles.xml as follows:
<resources>
<!-- extend the base theme to add styles available only with API level 21+ -->
<style name="AppTheme" parent="BaseAppTheme">
<item name="android:windowActivityTransitions">true</item>
<item name="android:windowEnterTransition">#android:transition/slide_right</item>
<item name="android:windowExitTransition">#android:transition/slide_left</item>
</style>
Now you can apply AppTheme in your manifest file and the system selects the styles available for each system version.
Is there a way to just call it in the main styles.xml and have it
apply everywhere so I don't have to write it multiple times?
Yes, there is a way in which you can maintain only one styles.xml file.
First of all, start using AppCompat themes. They provide backward compatibility and will work for older android versions as well.
Now define all of your styles in styles.xml(default) file and if your Android Studio is showing you some warning/error for some attribute which is supported in higher level apis:
You can suppress that warning using: tools:targetApi="SupportedAndroidVersionName"
Now Android will ignore that particular attribute if it's not supported and your whole style will work perfectly for both lower and higher api levels.
Read more about Styles and Themes here.
Hope it helps :)
Newer versions of Android have additional themes available to applications, and you might want to use these while running on those platforms while still being compatible with older versions. You can accomplish this through a custom theme that uses resource selection to switch between different parent themes, based on the platform version.
Why do I have to repeat the editTextStyle line in v19/styles.xml and
v21/styles.xml?
Because if your app is running on v21, v21/styles.xml will be loaded and if running on v19, v19/styles.xml will be loaded. In case you don't have v21/styles.xml or v19/styles.xml the app will automatically use your default values/styles.xml but you wont be able to take advantage of new features provide only for v21 or v19.
For more reference you can read Supporting Different Devices and Select a theme based on platform version.
I've been trying to implement the Material Design theme, following these instructions.
I'm not using ToolBar (must I?)
ALL my Activities extends ActionBarActivity.
Using getSupportActionBar() all across the project.
I'm compiling and targeting to API 21 in gradle (minimun is API 15).
My <application> tag contains android:theme="#style/AppTheme"
Running the application on Lollipop device (with a specific v21 similar theme works).
My styles.xml:
<style name="AppBaseTheme" parent="#style/Theme.AppCompat">
<item name="actionBarStyle">#style/MyActionBar</item>
</style>
<!-- Application theme. -->
<style name="AppTheme" parent="AppBaseTheme">
<!-- All customizations that are NOT specific to a particular API-level can go here. -->
</style>
<style name="MyActionBar" parent="#style/Widget.AppCompat.ActionBar.Solid">
<item name="displayOptions">useLogo|showHome</item>
<item name="logo">#drawable/home_page_logo</item>
<item name="background">#color/actionbar_background_color</item>
<item name="textColor">#color/white</item>
<item name="titleTextStyle">#style/MyActionBarTextStyle</item>
</style>
No matter what I tried, the application crashes the second I launch my main activity on onCreate() with this crash log:
Caused by: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
at android.support.v7.app.ActionBarActivityDelegate.onCreate(ActionBarActivityDelegate.java:151)
at android.support.v7.app.ActionBarActivityDelegateBase.onCreate(ActionBarActivityDelegateBase.java:138)
at android.support.v7.app.ActionBarActivity.onCreate(ActionBarActivity.java:123)
Did anyone experience this issue? any suggestions on what might cause this?
Edit:
It's definitely something in my styles.xml theme. If I force the app to use the default Theme.AppCompat theme, it works.
What might cause a theme to fail? I verified the ActionBar attributes are not using "android:". Anything else?
SOLVED...
2 of my jar libs apparently have generated values.xml that contains styles of both AppTheme and AppBaseTheme.
I verified only our dependencies modules, as jar libraries shouldn't declare application themes, specially not with the name of the default ones.
Before posting the answer, I added to my AndroidManifest.xml <application>
tools:replace="android:theme" and declared the new theme, assuming it'll work and my application will override any other theme.
The solution eventually, stupid as it is, was to rename my own AppTheme and AppBaseTheme to different names and now it works.
Hours spent on such a trivial fix. Hopefully, this will spare some time for others.
parent should be Theme.AppCompat not #style/Theme.AppCompat.
Using #style/ you'll be using the style from the Android API 21, and it must be the style from the AppCompat library.
edit:
probably same thing for the Widget.AppCompat
edit2:
like this
<style name="AppBaseTheme" parent="Theme.AppCompat">
<item name="actionBarStyle">#style/MyActionBar</item>
</style>
<!-- Application theme. -->
<style name="AppTheme" parent="AppBaseTheme">
<!-- All customizations that are NOT specific to a particular API-level can go here. -->
</style>
<style name="MyActionBar" parent="Widget.AppCompat.ActionBar.Solid">
<item name="displayOptions">useLogo|showHome</item>
<item name="logo">#drawable/home_page_logo</item>
<item name="background">#color/actionbar_background_color</item>
<item name="textColor">#color/white</item>
<item name="titleTextStyle">#style/MyActionBarTextStyle</item>
</style>
Try remove the "#style/" from the first line in the styles.xml so you have:
<style name="AppBaseTheme" parent="Theme.AppCompat">
I'm a beginner in android development. And I'm following the training on developer.android.com. But I'm having problem changing actionbar background color right now.
I have the error
Description Resource Path Location Type
error: Error: No resource found that matches the given name (at 'android:background' with value '#colors/orange'). styles.xml /ActionBarColor/res/values line 17 Android AAPT Problem
when I'm refering to res/values/colors.xml from my res/values/styles.xml
colors.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name ="orange">#FF8800</color>
</resources>
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="Theme.AppCompat.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.
-->
<item name="actionBarStyle">#style/myActionBar</item>
</style>
<style name = "myActionBar" parent = "Widget.AppCompat.Light.Base.ActionBar.Solid.Inverse">
<item name ="android:background">#colors/orange</item> <!--Error Here-->
</style>
<!-- Application theme. -->
<style name="AppTheme" parent="AppBaseTheme">
<!-- All customizations that are NOT specific to a particular API-level can go here. -->
</style>
</resources>
I read almost every thread on this but found no clue. My code looks exactly same as the others, as far as I can tell. Please help me. The project target is API19, min ver is API11
added themes.xml as the website says, error stays
<?xml version="1.0" encoding="utf-8"?>
<resources>
<!-- the theme applied to the application or activity -->
<style name="CustomActionBarTheme"
parent="#style/Theme.AppCompat.Light.DarkActionBar">
<item name="android:actionBarStyle">#style/MyActionBar</item>
<!-- Support library compatibility -->
<item name="actionBarStyle">#style/MyActionBar</item>
</style>
<!-- ActionBar styles -->
<style name="MyActionBar"
parent="#style/Widget.AppCompat.Light.ActionBar.Solid.Inverse">
<item name="android:background">#colors/orange</item>
<!-- Support library compatibility -->
<item name="background">#colors/orange</item>
</style>
</resources>
As I can see, you're using the AppCompat library on your Android project, so you should only use AppCompat resources for theming. Right from the Android training pages on how to Style the Action Bar, in the section "Customize the Background":
<?xml version="1.0" encoding="utf-8"?>
<resources>
<!-- the theme applied to the application or activity -->
<style name="CustomActionBarTheme"
parent="#style/Theme.AppCompat.Light.DarkActionBar">
<item name="android:actionBarStyle">#style/MyActionBar</item>
<!-- Support library compatibility -->
<item name="actionBarStyle">#style/MyActionBar</item>
</style>
<!-- ActionBar styles -->
<style name="MyActionBar"
parent="#style/Widget.AppCompat.Light.ActionBar.Solid.Inverse">
<item name="android:background">#drawable/actionbar_background</item>
<!-- Support library compatibility -->
<item name="background">#drawable/actionbar_background</item>
</style>
</resources>
Then apply your theme to your entire app or to individual activities:
<application android:theme="#style/CustomActionBarTheme" ... />
Create an style for the Action Bar based on any existent AppCompat themes and define the background item (for appcompat apps) - MyActionBar in the above example.
Create an style that'll be used by your project that applies the previously created Action bar style, and define the actionBarStyle item (for appcompat apps) - CustomActionBarTheme in the above example.
You would really want to read through the Android Training Pages, they're pretty well explained; and you also have the Android API Guides and the Android API Reference Pages just in case.
I am working with action bar and i want to customize the action bar logo. For that i have written code (below) but its working for Samsung and MicroMax devices but not for Lenovo K900 and Sony Ericssion Xperia z1. What should i do?
Api 16 its working fine but not in 17 (4.2)
Code:
<style name="AppTheme" parent="AppBaseTheme">
<item name="android:actionBarStyle">#style/MyActionBar</item>
</style>
<style name="MyActionBar" parent="#android:style/Widget.Holo.Light.ActionBar">
<item name="android:logo">#drawable/product_category_logo</item>
<item name="android:background">#drawable/actionbar_theme</item>
<item name="android:backgroundStacked">#color/black</item>
<item name="android:backgroundSplit">#color/white</item>
<item name="android:textColor">#000000</item>
<item name="android:textSize">16sp</item>
</style>
Values-v14 Folder:
<resources>
<!--
Base application theme for API 14+. This theme completely replaces
AppBaseTheme from BOTH res/values/styles.xml and
res/values-v11/styles.xml on API 14+ devices.
-->
<style name="AppBaseTheme" parent="android:Theme.Holo.Light.DarkActionBar">
<!-- API 14 theme customizations can go here. -->
</style>
Values-v11 Folder:
<resources>
<!--
Base application theme for API 11+. This theme completely replaces
AppBaseTheme from res/values/styles.xml on API 11+ devices.
-->
<!-- <style name="AppBaseTheme" parent="android:Theme.Holo.Light"> -->
<style name="AppBaseTheme" parent="android:Theme.Light"><!-- Recent changes -->
<!-- API 11 theme customizations can go here. -->
</style>
Guy i don't know what should i do. Please help me out.
I would suggest you to use ActionBarSherlock project in your application. Because this project already handles this kind of things for you.
And just for the sake of verification, Google is also using this ActionBar in its Google I/O app.
You can also download this project from GitHub ActionBarSherlock which also includes demo versions of this open source project.
To add ActionBarSherlock to your project:
Simply git clone the repo from GitHub in your workspace. And then import it as -> create project from existing source in Eclipse.
After this go to Project->Properties->Android and Add it as Library.
Here is your answer for importing it: Importing Actionbarsherlock into eclipse
And after importing it look for the Sample projects in it, how they are making use of Sherlock Action Bar. You will get a lot of info from there.
Most important thing about ActionBarSherlock is that you can also use it for Android OS below 3.0 but official Android API provide Action Bar widget only above OS v3.0. So you can target your app for all kind of users, those who are using OS above 3.0 and below 3.0, both will be targeted.
Please add below theme in all folder - values,vaules-11values-14
<style name="AppTheme" parent="AppBaseTheme">
<item name="android:actionBarStyle">#style/MyActionBar</item>
</style>
<style name="MyActionBar" parent="#android:style/Widget.Holo.Light.ActionBar">
<item name="android:logo">#drawable/product_category_logo</item>
<item name="android:background">#drawable/actionbar_theme</item>
<item name="android:backgroundStacked">#color/black</item>
<item name="android:backgroundSplit">#color/white</item>
<item name="android:textColor">#000000</item>
<item name="android:textSize">16sp</item>
</style>
i think you have to use actionbarsharelock or actionbarcompart for < android 3.0
but first put this theme in values-11,values-14 and test the app.
also use theme MyActionBar in android manifest file.
try and let me k'no
I'm trying to modify the default background theme color, which should be easy but surprisingly I can't get it working. Please note that I want the change to be across the entire app, not just for a single activity. Here is my code:
styles.xml
<resources>
<color name="white_opaque">#FFFFFFFF</color>
<color name="pitch_black">#FF000000</color>
<style name="AppTheme" parent="android:Theme.Light">
<item name="android:background">#color/white_opaque</item>
<item name="android:windowBackground">#color/white_opaque</item>
<item name="android:colorBackground">#color/white_opaque</item>
</style>
</resources>
and of course in the manifest
<application
.
.
.
android:theme="#style/AppTheme" >
</application>
Android doc which I consulted on modifying themes:
http://developer.android.com/guide/topics/ui/themes.html
I've tried switching between white_opaque and pitch_black for all the xml attributes but it doesn't change a thing. Any suggestions?
Okay turned out that I made a really silly mistake. The device I am using for testing is running Android 4.0.4, API level 15.
The styles.xml file that I was editing is in the default values folder. I edited the styles.xml in values-v14 folder and it works all fine now.
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="android:Theme.Holo.NoActionBar">
<item name="android:windowBackground">#android:color/black</item>
</style>
</resources>
Open res -> values -> styles.xml and to your <style> add this line replacing with your image path <item name="android:windowBackground">#drawable/background</item>. Example:
<resources>
<!-- 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:windowBackground">#drawable/background</item>
</style>
</resources>
There is a <item name ="android:colorBackground">#color/black</item> also, that will affect not only your main window background but all the component in your app. Read about customize theme here.
If you want version specific styles:
If a new version of Android adds theme attributes that you want to
use, you can add them to your theme while still being compatible with
old versions. All you need is another styles.xml file saved in a
values directory that includes the resource version qualifier. For
example:
res/values/styles.xml # themes for all versions
res/values-v21/styles.xml # themes for API level 21+ only
Because the styles in the values/styles.xml file are available for all
versions, your themes in values-v21/styles.xml can inherit them. As
such, you can avoid duplicating styles by beginning with a "base"
theme and then extending it in your version-specific styles.
Read more here(doc in theme).