jwir3
jwir3

Reputation: 6209

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.

Upvotes: 8

Views: 541

Answers (1)

aorlando
aorlando

Reputation: 668

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!

Upvotes: 3

Related Questions