1. Are you ready for the Galaxy S20? Here is everything we know so far!

Dark Mode in Shared preferences

Discussion in 'Android Development' started by Rubinia, May 3, 2020.

  1. Rubinia

    Rubinia Lurker
    Thread Starter

    I made a dark mode in the settings of my apps and it worked out perfectly BUT I can't save it in SharedPreferences so it will stay how I choose it. I can do a background color in my SharedPreferences but this setting always causes Errors. What do I wrong? I hope someone can help me make it work. Thank you so much. When I run the code like I have it, the App won't even run. When I remove the SharedPreferences Part it works but it doesn't save. Hope someone can help me. I work on it since so many hours and cant solve it, because it is my first app.

    here is the code:

    Code (Text):
    1. public class Settings extends AppCompatActivity {
    2.  
    3.     private static final String TAG = "SettingsActivity";
    4.     private RelativeLayout layout;
    5.     private SharedPreferences preferences;
    6.     private Bundle savedInstanceState;
    7.  
    8.     @Override
    9.     protected void onCreate(Bundle savedInstanceState) {
    10.         super.onCreate(savedInstanceState);
    11.         setContentView(R.layout.activity_settings);
    12.  
    13.         assert getSupportActionBar() != null;
    14.         getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    15.  
    16.         Spinner spinner = findViewById(DarkMode);
    17.  
    18.         spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
    19.             @Override
    20.             public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
    21.                 Log.e(TAG, "onItemSelected: " + position);
    22.                 handleNightMode(position);
    23.             }
    24.  
    25.             @Override
    26.             public void onNothingSelected(AdapterView<?> parent) {
    27.                 Log.e(TAG, "onNothingSelected: ");
    28.             }
    29.         });
    30.  
    31.     }
    32.  
    33.     private void handleNightMode(int position) {
    34.  
    35.         switch (position) {
    36.             case 0:
    37.                 Log.e(TAG, "Nothing Selected");
    38.                 break;
    39.             case 1:
    40.                 Log.e(TAG, "FOLLLOW_SYSTEM");
    41.                 AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM);
    42.                 getDelegate().applyDayNight();
    43.                 break;
    44.             case 2:
    45.                 Log.e(TAG, "YES");
    46.                 AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
    47.                 getDelegate().applyDayNight();
    48.                 break;
    49.             case 3:
    50.                 Log.e(TAG, "NO");
    51.                 AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
    52.                 getDelegate().applyDayNight();
    53.                 break;
    54.             case 4:
    55.                 Log.e(TAG, "AUTO");
    56.                 AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_AUTO_BATTERY);
    57.                 getDelegate().applyDayNight();
    58.                 break;
    59.             default:
    60.                 Log.e(TAG, "FOLLLOW_SYSTEM");
    61.                 break;
    62.         }
    63.  
    64.         SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
    65.         String NightMode = preferences.getString("prefTheme", "NO");
    66.         if (NightMode.equals("YES"))
    67.             AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
    68.         if (NightMode.equals("NO"))
    69.             AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_AUTO_BATTERY);
    70.         else if (NightMode.equals("AUTO"))
    71.             AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_AUTO_BATTERY);
    72.         super.onCreate(savedInstanceState);
    73.         setContentView(R.layout.activity_main);
    74.     }
    75. }
     



    1. Download the Forums for Android™ app!


      Download

       
Loading...

Share This Page

Loading...