Home > Enterprise >  How can i implement dark mode on the first activity while the application launched for the first tim
How can i implement dark mode on the first activity while the application launched for the first tim

Time:01-31

I implement dark mode with SwitchPreference in the second activity and in the first activity just a button take me to the second activity (Setting) the problem is when the app destroyed and i launched it again the dark mode not implemented until the second activity launched.

    //First Activity
    
    public class MainActivity extends AppCompatActivity {
    
        Button button;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
 SharedPreferences sharedPreferences =
                PreferenceManager.getDefaultSharedPreferences( this/* Activity context */);
        Boolean theme = sharedPreferences.getBoolean("darkMode", false);
                
 button=findViewById(R.id.button);
            button.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent2=new Intent(MainActivity.this,Settings.class);
                    startActivity(intent2);
                }
            });
    
    
    
        }
    
    }

i have some idea that i should use sharedpreferences but i don't know how can i set it

//Second Activity

    public class MySettingsFragment extends PreferenceFragmentCompat {
    
        @Override
        public void onCreatePreferences(@Nullable Bundle savedInstanceState, @Nullable String rootKey) {
            setPreferencesFromResource(R.xml.preferences,rootKey);
            SwitchPreference switchPreference=findPreference("darkMode");
    
            if (switchPreference.isChecked()){
    
                AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
    
            }else {
                AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
    
            }
    
    
        }
    }

enter image description here

CodePudding user response:

Well as per your code , you are just one step away from achieving result

you just need to use below code in every activity's onCreate method

if (isDarkMode){ // isDarkMode is boolean which we can get from preference
    
                AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
    
            }else {
                AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
    
            }
    ```

CodePudding user response:

Overview:-

You can change dark mode or light mode via this function.

AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.HERE_YOUR_MODE);

You can access the mode like this:

AppCompatDelegate.MODE_NIGHT_YES // for dark mode
AppCompatDelegate.MODE_NIGHT_NO // for light mode
AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM // for set as your devices theme
AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM // when you are in battery saver

Implementation:-

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
    Boolean theme = sharedPreferences.getBoolean("darkMode", false);
    if (theme){
        AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES); // implement here.
    }else{
        AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO); // implement here.
    }
}
  • Related