Wednesday, September 11, 2019

android kotlin - PreferenceFragmentCompat example

MainActivity.kt

package com.example.jetpack

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.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)

        // Get the preferences
        val prefs = PreferenceManager.getDefaultSharedPreferences(this)

        // Get the user dark theme settings
        val isDarkTheme = prefs.getBoolean("key_dark_theme",false)

        textView.text ="Dark Theme Enabled ? $isDarkTheme"

        btn.setOnClickListener{
            // Load the settings fragment
            supportFragmentManager
                .beginTransaction()
                .replace(R.id.linearLayout,MySettingsFragment())
                .commit()
        }
    }
}
activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <Button
        android:id="@+id/btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="16dp"
        android:text="Load Settings"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        />
    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="16dp"
        android:layout_marginBottom="24dp"
        android:text="TextView"
        android:textAppearance="@style/TextAppearance.AppCompat.Large"
        app:layout_constraintBottom_toTopOf="@+id/linearLayout"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/btn"
        />
    <LinearLayout
        android:id="@+id/linearLayout"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:orientation="vertical"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/textView"
        />
</androidx.constraintlayout.widget.ConstraintLayout>
MySettingsFragment.kt

package com.example.jetpack


import android.os.Bundle
import androidx.preference.PreferenceFragmentCompat
import android.widget.Toast
import androidx.preference.SwitchPreferenceCompat


class MySettingsFragment : PreferenceFragmentCompat(){
    override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
        setPreferencesFromResource(R.xml.prefs,rootKey)

        // Get the switch preference
        val switchDarkMode: SwitchPreferenceCompat? = findPreference("key_dark_theme")

        // Switch preference change listener
        switchDarkMode?.setOnPreferenceChangeListener{ preference, newValue ->
            if (newValue == true){
                Toast.makeText(activity,"enabled",Toast.LENGTH_LONG).show()
            }else{
                Toast.makeText(activity,"disabled",Toast.LENGTH_LONG).show()
            }

            true
        }
    }
}
res/xml/prefs.xml

<?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"
    >
    <SwitchPreferenceCompat
        app:key="key_dark_theme"
        app:title="Enable Dark Theme?"
        />
    <Preference
        app:key="key_sample"
        app:title="Sample Title"
        app:summary="This is sample summary"
        />
</PreferenceScreen>
AndroidManifest.xml [add]

// AndroidX Preference Library
implementation 'androidx.preference:preference:1.1.0'
More android examples