Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to inherit from a style specified from a different api version

This is a pretty basic question - I am supporting API versions 11+ and I want to use functionality on phones that have a newer API version. However, it would be nice if I didn't have to re-define my style. So, for example, if, in values/styles.xml I have:

<resources
    xmlns:android="http://schemas.android.com/apk/res/android">

  <style name="Theme.MyApp.Input.Text">
    <item name="android:layout_width">match_parent<item>
    <item name="android:layout_height>wrap_content</item>
  </style>
</resources>

and then in values-v14/styles.xml I have:

<resources
  xmlns:android="http://schemas.android.com/apk/res/android">
  <style name="Theme.MyApp.Input.Text">
    <item name="android:textAllCaps">true</item>
  </style>
</resources>

Then, on devices that have API 14+, I'll get the union of the two styles.

like image 268
jwir3 Avatar asked Apr 25 '15 00:04

jwir3


People also ask

What is theming in Android?

A theme is a collection of attributes that's applied to an entire app, activity, or view hierarchy—not just an individual view. When you apply a theme, every view in the app or activity applies each of the theme's attributes that it supports.

How to Add styles xml in Android studio?

xml in the your application's res/values directory. Add a root <resources> node. For each style or theme, add a <style> element with a unique name and, optionally, a parent attribute. The name is used for referencing these styles later, and the parent indicates what style resource to inherit from.

Where is Styles XML in Android Studio?

Defining Styles 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.


1 Answers

The question is quite old but it is possible: The following snippet is from values/styles.xml

  <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <item name="colorPrimary">@color/green</item>
        <item name="colorPrimaryDark">@color/dark</item>
        <item name="colorAccent">@color/green</item>
        <item name="checkboxStyle">@style/AppTheme.CheckBox</item>
    </style>

If you want to inherit (for eg) the same style and specialize in the API >=23 you must define a values/style-23.xml as below:

<style name="AppTheme.AppTheme23" >
    <item name="android:windowLightStatusBar">true</item>
</style>

Obviously in the AndroidManifest.xml the theme to specify is:

android:theme="@style/AppTheme"

That's all folks!

like image 95
aorlando Avatar answered Oct 21 '22 15:10

aorlando