Every Android application has some kind of settings screen. In this course, you’ll learn how to leverage SharedPreferences and the Preferences framework to build excellent settings screens that conform to Androids design guidelines.
At the core of modern mobile applications is a thorough understanding of SharedPreferences and Settings Screens. In this course, Kotlin on Android: Managing Preferences and Settings, you will learn how to leverage SharedPreferences and the Preferences framework. First, you will learn about the Preferences framework, which helps you conform to Android's design guidelines and provide a consistent user experience. Then, you will use SharedPreferences to save, edit, and retrieve key-value pairs saved locally on the Android device. Finally, you will implement a number of different interfaces for detecting when changes occur to Preferences and SharedPreferences. When you’re finished with this course, you will have a foundational knowledge of SharedPreferences and Settings Screens that will help you as you move forward to develop mobile applications.
Course Overview Hi. My name is Mitch Tabian, and welcome to my course, Kotlin on Android: Managing Preferences and Settings. I'm a self-employed Android developer, and I'm here to teach you about SharedPreferences and settings screens using Kotlin. Having a solid understanding of SharedPreferences and building settings screens is absolutely fundamental to every Android application. It's so important that the Android team built a special framework to assist developers in creating a consistent experience. Some of the major topics that we'll cover include the Preferences framework. If you're building a settings screen, you need to use the Preferences framework. It'll help you conform to Android's design guidelines and provide a consistent user experience. SharedPreferences. We'll use SharedPreferences to save, edit, and retrieve key value pairs saved locally on the Android device. Preference listeners. It's not enough to be able to save preferences. You need to know when they've been changed. We'll implement a number of different interfaces for detecting when changes occur to Preferences and SharedPreferences. By the end of the course, you'll have an excellent understanding of how you can use the Preferences framework and SharedPreferences to build settings screens. Before beginning the course, you should be familiar with Android Studio, and I recommend watching the course named Android Apps with Kotlin: Building Your First App by Jim Wilson. I hope you'll join me on this journey to learn about preferences with my Kotlin on Android: Managing Preferences and Settings course at Pluralsight.
Building a Settings Screen Building a settings screen is an important part of every Android application. There's always going to be certain things that users need the ability to customize, and it's usually best to save those properties locally on a device. The Preferences framework was built specifically with settings screens in mind. That is, it's literally made for building settings screens. It contains special UI building blocks that developers can take advantage of and construct a layout. The most beautiful thing about the building blocks is that you don't need to write any code to save changes made by the users. When the user changes a setting, it's automatically saved in a preference file for the application. So literally all you need to do is build the layout using special XML building blocks, and it's good to go. It's a beautiful thing. One question you might have at this point is but Mitch, what if a user has multiple devices, like a tablet or a second phone? The preferences would only be saved locally on each device. If that's your concern, you're right. The preferences are saved locally in a file on a device. To keep the data consistent, you'll need to upload the settings to a server. Then you can have the application query the server and check for the updated settings. The Preferences framework has listeners that are perfect for facilitating that. We can set PreferenceChangeListeners to listen to the keys of the preferences. Then when there's a change, we can upload the changes to a server. We're going to take a close look at PreferenceChangeListeners in the module named Implementing Preference Listeners. Another great thing about the Preferences framework is that your settings screens are guaranteed to meet Android's design guidelines. You're building the layout with special UI building blocks, so there's basically no way you can mess it up. I don't know about you, but not being able to mess something up sounds pretty good to me.
Implementing Preference Listeners Understanding Preferences listeners is fundamental to effectively using the Preference framework and SharedPreferences. With respect to the Preference framework, we can listen for changes made to the Preference objects in a Settings screen. We can also add PreferenceClickListeners to the Preferences objects. We'll be using a PreferenceClickListener to inflate AccountFragment when a user clicks here on the Settings screen. With respect to SharedPreferences, we can listen for changes made to the key-value pairs. In a practical sense, when preferences are changed, you might want to update the values on a server. It's usually best to save settings on a server since that way a user could theoretically log into the app from any device, and the configuration would be the same. Obviously our application isn't real, and there's no server to upload to. So instead, we're going to simulate a server upload using a runnable. That way you get a feel for what it would actually be like.