Settings allow users to change the application behavior and functionality. Preferences screen can be integrated into the application using the AndroidX Preference Library. It manages preferences displayed on screen and interacts with storage.
In the module's build.gradle
file, we added androidx.preference:preference
dependency.
dependencies {
// Other dependencies
// ...
implementation 'androidx.preference:preference:1.1.1'
}
Preferences hierarchy can be defined in XML resource file.
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<EditTextPreference
android:key="username"
android:title="Username"
android:summary="Click to show a dialog"
app:iconSpaceReserved="false" />
</PreferenceScreen>
We create a new class that extends PreferenceFragmentCompat
. An XML resource that contains preferences is provided by using addPreferencesFromResource()
method.
package com.example.app
import android.os.Bundle
import androidx.preference.PreferenceFragmentCompat
class SettingsFragment : PreferenceFragmentCompat()
{
override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?)
{
addPreferencesFromResource(R.xml.app_preferences)
}
}
We create a new activity that initializes fragment and displays preferences.
package com.example.app
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
class SettingsActivity : AppCompatActivity()
{
override fun onCreate(savedInstanceState: Bundle?)
{
super.onCreate(savedInstanceState)
supportFragmentManager.beginTransaction()
.replace(android.R.id.content, SettingsFragment())
.commit()
}
}
In the layout XML file we added a Button
which will used to open activity that displays preferences.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<Button
android:id="@+id/myButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="Open" />
</RelativeLayout>
All preferences are saved as key-value pairs in the default SharedPreferences
.
package com.example.app
import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import androidx.preference.PreferenceManager
import kotlinx.android.synthetic.main.activity_main.*
class MainActivity : AppCompatActivity()
{
override fun onCreate(savedInstanceState: Bundle?)
{
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
myButton.setOnClickListener { showSettings() }
val sharedPref = PreferenceManager.getDefaultSharedPreferences(this)
val username = sharedPref.getString("username", "")
Log.d("MY_APP", username!!)
}
private fun showSettings()
{
val intent = Intent(this, SettingsActivity::class.java)
startActivity(intent)
}
}
Leave a Comment
Cancel reply