![]() |
VOOZH | about |
In many apps, we have seen the Settings screen which is most common in most of the apps. This settings screen is used to manage the preferences of the users. For creating this settings screen android provides a feature to make a settings preferences screen. In this article, we will take a look at implementing the preferences setting screen in Android.
We will be building a simple application in which we will be displaying a simple button and on clicking on that button we will be opening a settings screen which we will create using settings preferences. This settings screen will look similar to what we can get to see in YouTube settings in the General option. A sample video is given below to get an idea about what we are going to do in this article. Note that we are going to implement this project using the Java language.
Step 1: Create a New Project
To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio. Note that select Java as the programming language.
Step 2: Working with the activity_main.xml file
Navigate to the app > res > layout > activity_main.xml and add the below code to that file. Below is the code for the activity_main.xml file.
Step 3: Creating a new activity for displaying the settings screen
Navigate to the app > java > your app's package name > Right-click on it > New > Activity and select Empty activity and name it as SettingsActivity.
Step 4: Working with the MainActivity.java file
Go to the MainActivity.java file and refer to the following code. Below is the code for the MainActivity.java file. Comments are added inside the code to understand the code in more detail.
Step 5: Creating a preference file for displaying our settings
Navigate to the app > res > Right-click on it > New > Android Resource file and you will get to see the below screen.
Step 6: Add the below code in the strings.xml file
Navigate to the app > res > values > strings.xml file and add the below code to it.
Step 7: Working with preferences.xml file
In preferences there are different types of preferences which are listed below :
Navigate to the app > res > xml > preferences.xml file and add the below code to it. Comments are added in the code to get to know in more detail.
Step 8: Now create a new java class for displaying our preference fragment
Navigate to the app > java > your app's package name > Right-click on it > New > Java class and name it as SettingsFragment and add the below code to it. Comments are added in the code to get to know in more detail.
Step 9: Working with activity_settings.xml file
Navigate to the activity_settings.xml file and add the below code to it. Comments are added in the code to get to know in more detail.
Step 10: Working with SettingsActivity.java file
Navigate to the app > java > your app's package name > SettingsActivity.java file and add the below code to it. Comments are added in the code to get to know in more detail.
Now run your app and see the output of the app.