How to set a different theme to a Layout
Asked Answered
K

5

37

I've set a default theme for the whole app. It's defined in styles.xml as follows:

    <style name="DefaultTheme" parent="@android:style/Theme.Holo.Light">
        <!-- Customization here -->
    </style>

I've also defined a dark theme:

    <style name="DarkTheme" parent="@android:style/Theme.Holo">
        <!-- Customization here -->
    </style>

In the manifest it is declared the light theme as the main theme for the app:

    <application
    ...
    android:theme="@style/DefaultTheme" >

Now this is working fine, but in an activity, I need to set a different theme for a single Layout. Something like this:

    +--------------------------------------------------+
    |         Parent Linear layout (default theme)     |
    |                                                  |
    | +------------------------------------+ +-------+ |
    | |                                    | |       | |
    | |     Left linear layout             | |       | |
    | |     (default theme)                | |       | |
    | |                                    | |       | |
    | |                                    | |       | |
    | |                                    | |    ·<----------- Right Linear Layout
    | |                                    | |       | |        (Need it in dark theme)
    | |                                    | |       | |
    | |                                    | |       | |
    | +------------------------------------+ +-------+ |
    +--------------------------------------------------+

In the layout xml file I'm trying to set a theme for the rightmost child LinearLayout:

    <LinearLayout
    style="@style/DarkTheme">
    ...

I'd expect this to work just fine, and to apply the dark theme to the right layout only (and its child views), but it is not working. I've tried replacing the @style with a built-in @android:style to no avail. I've tested this in the layout editor and on real device/simulator.

Is it possible to apply a custom theme or style to a single layout?

Kaltman answered 29/8, 2013 at 9:52 Comment(1)
I know it is old question, were you able to find an answer to it?Trici
W
35

This is now possible by using the android:theme property on the view and setting it to any theme you like. Note that the child views will inherit the theme of their parent.

Wimbush answered 17/7, 2015 at 3:54 Comment(3)
As of Android MarshmallowMcnulty
This is really vague for an accepted answer. I have provided a more thorough answer at https://mcmap.net/q/415944/-how-to-set-a-different-theme-to-a-layoutPredict
How can I do it programmatically?Begun
H
14

You can use ContextThemeWrapper() to apply theme when create layout programmatically.

LinearLayout darkThemeLayout = new LinearLayout(new ContextThemeWrapper(context, R.style.DarkTheme));
Holcman answered 4/7, 2014 at 21:23 Comment(0)
A
6

With support library you can do:

app:theme="R.style.MyTheme"
Apuleius answered 4/3, 2016 at 7:29 Comment(0)
P
3

API 24 adds a per-view theme parameter, but requires Marshmallow or higher.
The alternative is to use the android support library for maximum compatibility.

Set the support library's theme parameter for the view

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    ...
>
    <LinearLayout
        app:theme="R.style.MyTheme"
        ...
    >
        <!-- content-->
    </LinearLayout>
...
</LinearLayout>

which references a theme defined as a style inside a resource xml

<resources>
    <style name="MyTheme">
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
    </style>
</resources>
Predict answered 3/5, 2019 at 16:30 Comment(2)
I was able to apply your approach from API 16 too. I haven't tried any API below it, but is not limited to API 24 anyway. At least using androidx as I do.Archidiaconal
@RubénViguera That's good to hear. I try to avoid third-party libraries in my answers, since they may not always be available alongside the API. Google has a knack for disowning "beta" packages without much consideration for the impact.Predict
H
1

adding this directly to the can solve the problem.

<com.google.android.material.slider.Slider android:stepSize="1"

      //  app:trackColorActive="#ff4500"
      //  app:trackColorInactive="#fbb999"
    
        android:theme="@style/Theme.MaterialComponents.DayNight"

..

Hygroscope answered 3/6, 2021 at 1:56 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.