How To Create A Settings Page In Android Studio
Step by step guide to create App Settings using Preferences in Android (Part -1)
Many a times nosotros need certain settings in our app, like setting default language, theme(lite or dark), customizing notifications, etc. In this tutorial we will be creating a simple app with settings to alter text visibility, text size and color. For this we volition be using Shared Preferences.
Final Output:
In this tutorial nosotros will be creating the first setting that is making the text visible or invisible by CheckBoxPreference and in side by side article nosotros will dive into ListPreference and the EditTextPreference.
Step 1:
Layout Code for our Main Activeness
Footstep i:
Create a Settings Activity in which will add our settings menu. To create a menu and add together items to it, starting time create a card folder and add a menu resource file it.
android:orderInCategory: The gild of "importance" of the item, within a grouping.
Step 2:
After creating the menu, we need to override two methods,
In onCreateOptionsMenu method nosotros inflate our settings_menu file in our Main Activity and in the 2nd method we setup an explicit intent to open up Settings Activity.
Step 3:
At present we will set the Settings Activity as the child activity of the Main Activeness past making following changes in the Android Manifest file.
What is Parent Activity and Child Activity?
Up navigation
Upwards navigation, sometimes referred to as ancestral or logical navigation, is used to navigate within an app based on the explicit hierarchical relationships between screens. With Upward navigation, your activities are bundled in a hierarchy, and "kid" activities show a left-facing arrow in the action bar that returns the user to the "parent" activity.
Providing Upwardly behavior for your app is optional, but a good pattern practice, to provide consistent navigation for the activities in your app.
android:launchMode="singleTop": When returning from Settings Activity, it does non recreates the case of the activity instead calls the already existing.
Footstep 4:
For navigating back from the Settings Activity to Master Activity on pressing the back button on the Action bar, nosotros need to add the following code to the SettingsActivity.java
Step four:
Add together following dependencies to your gradle file and sync information technology.
implementation 'com.android.support:preference-v7:26.1.0'
Create a Settings Fragment and extend it with PreferenceFragmentCompat and override the onCreatePreferences method.
Pace v:
Side by side we will create a xml folder in res and add together a settings_pref.xml to it. Here we volition create our settings bill of fare.
For this tutorial we volition be adding just a CheckBoxPreference to the settings menu.
Here,
In Shared Preferences we shop data in fundamental: value pair and while retrieving the information we volition be needing the key.
All the Preferences volition be having a key, championship and a default value.
Now attach settings_pref.xml to the SettingsFragment.java and add this fragment to the SettingsActivity.java
Along with this nosotros will need to add the following theme into styles.xml or else the app volition crash.
<item proper name="preferenceTheme">@style/PreferenceThemeOverlay</particular>
Step 6:
At present to read from the Shared Preferences and making changes to our text, nosotros will make following changes to the MainActivity.java file.
First nosotros got the instance of the Shared Preferences in the method setupSharedPreferences and registered it. Then nosotros override the onSharedPreferenceChanged method and call the method responsible to change the visibility of the text.
Registering and unregistering the shared preferences is necessary for making dynamic changes to the text or else the changes will have place only on recreating the activity.
By now, we have implemented the beginning settings of making the text visible or invisible.
In future tutorial nosotros volition be adding the other two settings.
The code for this app tin be found in my GitHub account,
How To Create A Settings Page In Android Studio,
Source: https://medium.com/@bhavyakaria/step-by-step-guide-to-create-app-settings-using-preferences-in-android-part-1-fa470305b530
Posted by: reddywassing.blogspot.com
0 Response to "How To Create A Settings Page In Android Studio"
Post a Comment