Home > OS >  App theme doesn't load correctly if the system theme is light and the app theme is dark
App theme doesn't load correctly if the system theme is light and the app theme is dark

Time:10-25

If the system theme is light and the app theme is dark, the actionbar items are dark when the app startup. (left picture) When I reload the dark theme again from the in-app settings, the actionbar items are light (as I want) (right picture). This problem only happens in MainActivity. How can fix this problem?

enter image description here

MainActivity.java

protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                PreferenceManager.setDefaultValues(this, R.xml.preferences, true);
                PreferenceUtils.setTheme(this);
                setContentView(R.layout.activity_main);
                if (getSupportActionBar() != null) {
                    this.getSupportActionBar().setElevation(0);
                }
                setTitle("Sample App");
    
            drawerLayout = findViewById(R.id.drawer_layout);
            drawerToggle = new ActionBarDrawerToggle(this, drawerLayout, R.string.nav_drawer_open, R.string.nav_drawer_close);
            drawerLayout.addDrawerListener(drawerToggle);
            getSupportActionBar().setDisplayHomeAsUpEnabled(true);
            getSupportActionBar().setHomeButtonEnabled(true);
            drawerToggle.syncState();
    
            navigationView = findViewById(R.id.nav_view);
            navigationView.setNavigationItemSelectedListener(this);
                .
                .
                .

PreferenceUtils.java

public class PreferenceUtils {

    public static final String LIGHT_MODE = "light";
    public static final String DARK_MODE = "dark";

    public static void setTheme(Activity activity) {
        switch (PreferenceManager.getDefaultSharedPreferences(activity).getString("pref_key_theme"), "")) {
            case LIGHT_MODE: {
                AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
                break;
            } case DARK_MODE: {
                AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
                break;
            } default: {
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
                    AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM);
                } else {
                    AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_AUTO_BATTERY);
                }
                break;
            }
        }
    }
}

activity_main.xml

<androidx.drawerlayout.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@ id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="false"
    tools:context=".MainActivity"
    tools:openDrawer="start">

    <RelativeLayout
        android:id="@ id/main_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <com.google.android.material.tabs.TabLayout
            android:id="@ id/tab_layout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="?attr/colorPrimary"
            android:minHeight="?attr/actionBarSize"
            android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
            app:tabIndicatorColor="?attr/colorAccent"
            app:tabSelectedTextColor="?android:textColorPrimary"
            app:tabTextColor="?android:textColorSecondary"
            app:tabMode="fixed" />     
    </RelativeLayout>

    <com.google.android.material.navigation.NavigationView
        android:id="@ id/nav_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:fitsSystemWindows="true"
        app:headerLayout="@layout/nav_header_main"
        app:menu="@menu/main_drawer" />
</androidx.drawerlayout.widget.DrawerLayout>

themes.xml

<resources xmlns:tools="http://schemas.android.com/tools">
    <!-- Base application theme. -->
    <style name="Theme.SampleApp" parent="Theme.MaterialComponents.DayNight.DarkActionBar">
        <!-- Primary brand color. -->
        <item name="colorPrimary">@color/red_800</item>
        <item name="colorPrimaryVariant">@color/red_900</item>
        <item name="colorOnPrimary">@color/white</item>
        <!-- Secondary brand color. -->
        <item name="colorSecondary">@color/red_700</item>
        <item name="colorSecondaryVariant">@color/red_700</item>
        <item name="colorOnSecondary">@color/black</item>
        <!-- Status bar color. -->
        <item name="android:statusBarColor" tools:targetApi="l">?attr/colorPrimaryVariant</item>
    </style>
</resources>

themes-night.xml

<resources xmlns:tools="http://schemas.android.com/tools">
    <!-- Base application theme. -->
    <style name="Theme.SampleApp" parent="Theme.MaterialComponents.DayNight.DarkActionBar">
        <!-- Primary brand color. -->
        <item name="colorPrimary">@color/dark_grey_900</item>
        <item name="colorPrimaryVariant">@color/black</item>
        <item name="colorOnPrimary">@color/black</item>
        <!-- Secondary brand color. -->
        <item name="colorSecondary">@color/blue_200</item>
        <item name="colorSecondaryVariant">@color/blue_200</item>
        <item name="colorOnSecondary">@color/black</item>
        <!-- Status bar color. -->
        <item name="android:statusBarColor" tools:targetApi="l">?attr/colorPrimaryVariant</item>
    </style>
</resources>

manifest

<application
        android:allowBackup="true"
        android:fullBackupContent="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:theme="@style/Theme.SampleApp">         
        <activity android:name=".MainActivity" android:configChanges="locale|orientation|screenSize|keyboardHidden|uiMode" android:exported="true">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

CodePudding user response:

I am not sure but i guess you can try to set it onViewCreated to be sure view created when you apply theme.

CodePudding user response:

PreferenceManager.setDefaultValues(this, R.xml.preferences, true); 

what does this do? If it used to set default values then remove this line and you should use preference.getBoolean(key, defaultValue) even if u have not set preference u can get Default values

  • Related