Search code examples
androidandroid-layoutandroid-5.0-lollipopcode-duplication

Alternative Layouts


I am developing an android application that I want to support Material Design for devices running 5.0+ and devices from 4.1 to 4.4.

I am using the appcompat library to get some support for older versions.

Now I am faced with attributes that are only present in v21, like elevation.

I can create a layout-v21 folder and add my activity's layout there, again, which leads to quite a bit of duplication.

Do you do this another way?

Is there a way to use styles for this? How do I subclass a style from values to values-v21


Solution

  • Here is a link that I have used that gives some information about overriding styles/themes for v21 Lollipop: http://antonioleiva.com/material-design-everywhere/

    Essentially you can do in values/themes.xml

    <style name="AppTheme" parent="AppTheme.Base"/>
    
    <style name="AppTheme.Base" parent="Theme.AppCompat">
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimary</item>
        <item name="android:windowNoTitle">true</item>
        <item name="windowActionBar">false</item>
    </style>
    

    And then in values-v21/themes.xml

    <style name="AppTheme" parent="AppTheme.Base">
        <item name="android:windowContentTransitions">true</item>
        <item name="android:windowAllowEnterTransitionOverlap">true</item>
        <item name="android:windowAllowReturnTransitionOverlap">true</item>
        <item name="android:windowSharedElementEnterTransition">@android:transition/move</item>
        <item name="android:windowSharedElementExitTransition">@android:transition/move</item>
    </style>
    

    To override AppTheme for v21 but to retain all the properties for the AppTheme.Base from pre-v21.