![]() In this case we make use of the PrefManager.java class. In the onCreate() method of this activity we check whether the WelcomeActivity is opened for the first time or not. Now, what happened was when the app showed cards, it was due to the WelcomeActivity.java activity. So, now after swiping all the cards and on reaching the final card instead of clicking GOT IT to move to the login screen and on pressing the home button we put the app in the background. Whenever we install the SUSI.AI Android app, on opening it for the first time we see welcome cards that give a basic overview of the app. The SUSI.AI Android app opens up the login screen and on just viewing the welcome cards for the first time due to the incorrect implementation of the sharedpreferences, using the Preference Manager we can correctly implement the preferences in welcome activity. To store data in sharedpreferences through the PreferenceManager class we just need to declare an instance of the PreferenceManager in the location in which we want to save the data. In SUSI.AI android app PrefManager.java class is made that uses the sharedpreferences in android and provides a custom wrapper that is used to store the data in the key-value pair form in the app. But, sometimes we need to store data at many places in the application such as saving the login email or a particular information that remains the same for the entire use of the app. There are just two things that need to be done here:įirstly, set app:useSimpleSummaryProvider attribute value to true in ListPreference.SharedPreferences is used in android to store data in the form of a key-value pair in an application. ![]() It’s an interface that allows us to store/modify/delete data locally. How do we fix that? Well, that's very simple actually. SharedPreferences is part of the Android API since API level 1. As you can see, the ListPreference doesn't show which option is currently selected in the summary. For example, drawable-night or values-night.Īnd with this, we've successfully got our dark mode toggle working. You do so by adding these resources in resource folders with the extension of -night. It is possible to add separate icons, colors, styles, and other resources to be used in the day and the night mode. Now, a provision has to be made to make navigation to the SettingsActivity possible which I did by adding an action to the options menu. Override fun onSharedPreferenceChanged ( sharedPreferences : SharedPreferences ?, key : String ?) ![]() Implement SharedPreferences.OnSharedPreferenceChangeListener in SettingsActivity and override the onSharedPreferenceChanged() function. Following values can be passed to this method:ĪppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM - This will toggle the night mode based on whether the system-wide dark mode is enabled or not.ĪppCompatDelegate.MODE_NIGHT_NO - Light mode will be enabled.ĪppCompatDelegate.MODE_NIGHT_YES - Dark/Night mode will be enabled.ĪppCompatDelegate.MODE_NIGHT_AUTO_BATTERY - Dark/Night mode will be enabled when the device is on the battery saving mode. The AppCompatDelegate class' setDefaultNightMode() function is used to change the night mode settings. Now that we are done with the layout, let's look at the code. Follow System Dark Mode Light Mode Selected Dark Mode Selected Auto Battery Dark Mode MODE_NIGHT_FOLLOW_SYSTEM MODE_NIGHT_NO MODE_NIGHT_YES MODE_NIGHT_AUTO_BATTERY
0 Comments
Leave a Reply. |