Coder Social home page Coder Social logo

noor1yasser9 / customizing-ui-settings-using-the-androidx-preference-library Goto Github PK

View Code? Open in Web Editor NEW
0.0 1.0 0.0 156 KB

Android settings allow users to change the functionality and behavior of an application. This course will teach you to leverage the power of the AndroidX Preference library to build consistent settings UIs that conform to Android design guidelines.

Kotlin 100.00%

customizing-ui-settings-using-the-androidx-preference-library's Introduction

What Is the AndroidX Preference Library?

Welcome to the Getting Started module of this course. In this module, we are going to get an overview of the AndroidX Preference library. So the first question that comes to mind is what exactly is meant by a settings screen and what is its purpose? Well, settings in Android basically allow you to change the functionality or the behavior of an application. Using settings, you can allow users to customize your application as per their personal choice. And I am sure if you are an Android user, then you must be aware of what exactly is a settings in Android. So a typical settings screen usually looks like this or this. And based on the type of your application, your settings screen should have preferences accordingly. For example, this is a settings screen of Google Play Store, which contains settings options related to app downloads or updates. This is a Gmail setting that contains preferences related to handling emails. Then we have an example from Google Maps, which mainly contains settings related to location updates. Similarly, these two are examples from the Google Drive app and everyone's favorite, WhatsApp application. Now, so far, whatever settings screen that I just showed you, their user interfaces look almost the same. I mean, the way they look appears to be very consistent. Well, this is because such a simple and elegant UI you can be easily built using a special library known as the AndroidX Preference library, and this is what we are going to explore throughout this course. Also note that this library basically belongs to the Android Jetpack, which further ensures that if we use it, our application will automatically use the modern standards of application development. So let's get started.

SharedPreferences and PreferenceDataStore

Let us now take a look at how the Preference library internally saves a value when a user updates his preferences in a settings screen. So for a reference, let us consider this SwitchPreferenceCompat. So a user can either switch it on or switch it off based on what he wants. So suppose the user switches it on. Then what happens is that the Preference library saves the changes in the form of key‑value pair. So what exactly is this key? Well, to identify a Preference object, each Preference must have a unique key. So using this key we can access the value of this Preference in our application. So this key is like an ID to identify a Preference object. Now, what about this value? Well, for this SwitchPreference, the user can either turn it on or off. So it could possibly contain two values, either true or false. So here our value is in the Boolean format, which is true at present. Now, please note that based on the type of reference object you are using in your settings screen, the value could be of either string, Boolean, integer, reference or even a float value. For example, for this status Preference, the value format would be of a string type. But for this SwitchPreference, we have stored the value in the form of a Boolean. So please make a note of this. But also note that the key is always defined in the string format, irrespective of what Preference you are using in your screen. So now moving on, where is this key‑value pair actually persisted or stored? Well, the Preference library by default stores all the key‑value pairs within SharedPreferences, and this SharedPreferences is nothing but an XML file which is private to your own application. This means that this file is highly secured, and no outside application can access this file. And the best part of the SharedPreferences is that it is automatically managed by the Preference library, which means this file is created automatically, and all the data within this file is saved, updated, and deleted automatically by our Preference library, so we don't have to worry about anything. How cool is that? Now, another property of this SharedPreferences is that even if you close the application or update the application from the Play Store, the data within this file is not lost. It is persisted throughout the lifespan of the application. That is, the data is deleted only in case if the user uninstalls the application or manually deletes the application data. So I hope you have got the basic idea of this SharedPreferences file. Now moving on apart from this SharedPreferences, the Preference library can use a second way to store this key‑value pair. Well, the second way is by using a special class of PreferenceDataStore. But this time it is not a default way to store data; it is basically a custom way to save data. This means unlike the SharedPreferences where data is saved and updated automatically; in the case of PreferenceDataStore, it is us who needs to take care of how we are going to save the data. And using this PreferenceDataStore, we can actually store data in different storage options, such as, we can store data in cloud or Firebase, you can use your own back‑end server to save data, you can even store data in a local database such as a Room database, or you are free to store data if you have developed your own storage library. So basically this PreferenceDataStore allows you to store data in any possible way you want to. But in the case of SharedPreferences, the data is stored only within a private XML file, so please make a note of this.

customizing-ui-settings-using-the-androidx-preference-library's People

Contributors

noor1yasser9 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.