Search code examples
androidandroid-studioandroid-theme

Android studio layout preview does not get my current theme


I have a test multi-module project and some modules does not see my application theme. In more detail, the modules are the following: app, common, home & details.

The app module contains an activity and the navigation, the common contains strings & the style and home/detail contain a dummy fragment. All fragments implement the common module which contains my style.

// Modules
implementation project(path: ':navigation')
implementation project(path: ':common')

So when I go to the activity's layout editor and try to change the theme from the preview, I can see and select my theme. activity_main.xml

But I go to HomeFragment's layout no such option exists. fragment_home.xml

Of course the style works as expected when I run the application, the only problem is that the preview does not work. Here is the theme which is on the common module:

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

    <style name="AppTheme" parent="Theme.Leanback.Browse">
        <!-- Theme.AppCompat compatibility: -->
        <item name="windowActionBar">false</item>
        <item name="windowNoTitle">true</item>

        <!-- Theme.MaterialComponents compatibility: -->
        <item name="colorPrimary">#171717</item>
        <item name="colorPrimaryVariant">#212121</item>
        <item name="colorAccent">#DD1C1A</item>
        <item name="colorOnPrimary">#eeeeee</item>
        <item name="colorSecondary">#FFEB3B</item>
        <item name="colorSecondaryVariant">#FFC107</item>
        <item name="colorOnSecondary">#eeeeee</item>
        <item name="colorSurface">#2196F3</item>
        <item name="colorOnSurface">#eeeeee</item>

    </style>

</resources>

One thing that I observed during my trial and error is that if I change my theme parent from Leanback to Theme.AppCompat the preview works but I can not do that since I need Leanback. Finally, all the modules except the app one are library modules apply plugin: 'com.android.library'

My setup is a windows 10 PC and I am using Android studio 4 canary 8. I have tested this also on Android studio 4 canary 2 with the same results.

Edit: Solution Set default theme on module manifest, thanks @Martin Zeitler

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.home">

    <application android:theme="@style/AppTheme" />
</manifest>

Solution

  • You could assign a default theme in the AndroidManifest.xml, by adding android:theme to the application node. This should also work for library modules, since they have an empty application node, where one can define the default theme for the XML preview.