Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating styles-v21.xml

In Android Studio, I imported a project which did not include styles.xml (v21). So I created a styles-v21.xml file in the values directory. Both styles.xml and styles-v21.xml are in the values directory now.

styles.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <style name="AppTheme" parent="AppTheme.Base">
        <!-- Customize your theme here. -->
    </style>

    <style name="AppTheme.Base" parent="Theme.AppCompat.Light">

    </style>

</resources>

styles-v21.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <style name="AppTheme" parent="android:Theme.Material.Light.DarkActionBar">

    </style>

</resources>

When I build the project I get a build error:

Error:Error: Duplicate resources: C:\xxx\main\res\values\styles-v21.xml:style/AppTheme, C:\xxx\main\res\values\styles.xml:style/AppTheme

The error is that both resource files have the same style name: AppTheme. But in other projects and tutorials I've seen, these styles have the same name. One is for Android with version 21 compatibility and the other is for Android without the compatibility.

I guess I want to know if I should simply change the name or if these files are supposed to have the same name - which means there is a bigger problem at hand. How should I proceed?

like image 271
user2456977 Avatar asked Feb 11 '15 21:02

user2456977


People also ask

Where are styles created in XML?

A style is defined in an XML resource that is separate from the XML that specifies the layout. This XML file resides under res/values/ directory of your project and will have <resources> as the root node which is mandatory for the style file. The name of the XML file is arbitrary, but it must use the . xml extension.

How do I create a style folder in Android?

Right click on res folder, choose New --> Android resource file, set the same name for the new file "styles", in Available qualifiers: choose the last item "Version" and finally set "Platform API level" 21. Save this answer. Show activity on this post. By default, Android Studio doesn't create styles.

Is styles XML and themes XML same?

The difference is how you apply it: instead of applying a style with the style attribute on a view, you apply a theme with the android:theme attribute on either the <application> tag or an <activity> tag in the AndroidManifest. xml file.

Where is Styles XML in Android Studio?

xml file in the new Android Studio. Goto your project file view: Res>Values>Themes folder. Open themes. xml file change actionbar.


3 Answers

Right click on res folder, choose New --> Android resource file, set the same name for the new file "styles", in Available qualifiers: choose the last item "Version" and finally set "Platform API level" 21.

like image 96
Marco Aurelio Silva Avatar answered Oct 17 '22 23:10

Marco Aurelio Silva


By default, Android Studio doesn't create styles.xml (v21). What you need to do is create a folder structure like the following:

  • res/values/styles.xml
  • res/values-v21/styles.xml

And this is what you will get in Android Studio:

enter image description here

Then you can defined different AppTheme for different API levels.

like image 26
Yuchen Avatar answered Oct 18 '22 00:10

Yuchen


You should create styles.xml for version 21 in values-v21 folder and android studio automatically add it as styles.xml(v21)

like image 2
Mycoola Avatar answered Oct 18 '22 01:10

Mycoola