Material Preference

Additional

Language
Kotlin
Version
N/A
Created
Jun 30, 2018
Updated
Feb 18, 2019
Owner
Anggrayudi H (anggrayudi)
Contributor
Anggrayudi H (anggrayudi)
1
Activity
Badge
Generate
Download
Source code
APK file

Advertisement

Material Preference

A library designed for people who love simplicity. Hate the old preference style? Try this library.

It combines libraries from androidx.preference and net.xpece.android.support.preference. Available from API 17.




Screenshots

Note

This library is available in 2 versions:

  1. Version 2.x.x, built in Java
  2. Version 3.x.x and higher, built in Kotlin

The Java library will be the second priority. So I will be more active in Kotlin library. You can fork the Java branch and build your own version if you feel it is slow in maintenance.

Writing code in Java is slow, and that's why I decided to migrate to Kotlin.

Usage

Basic

dependencies {
    implementation 'com.anggrayudi:materialpreference:3.1.4'
}

Note: If you encounter error Failed to resolve com.anggrayudi:materialpreference:x.x.x, then add the following config:

repositories {
    maven { url 'https://dl.bintray.com/anggrayudi/maven/' }
}

From your preferences.xml:

<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">

    <!-- To make Preferences floating, you must wrap them inside PreferenceCategory -->
    <PreferenceCategory>
        <Preference
            android:key="about"
            android:title="About"
            android:icon="@drawable/..."
            app:tintIcon="?colorAccent"
            app:legacySummary="false"/>
    </PreferenceCategory>
</PreferenceScreen>

From your SettingsFragment:

class SettingsFragment : PreferenceFragmentMaterial() {

    override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
        addPreferencesFromResource(R.xml.preferences)
    }
    
    companion object {
        fun newInstance(rootKey: String?): SettingsFragment {
            val args = Bundle()
            args.putString(PreferenceFragmentMaterial.ARG_PREFERENCE_ROOT, rootKey)
            val fragment = SettingsFragment()
            fragment.arguments = args
            return fragment
        }
    }
}

From your SettingsActivity:

class SettingsActivity : PreferenceActivityMaterial() {

    private var settingsFragment: SettingsFragment? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_settings)
        
        if (savedInstanceState == null) {
            settingsFragment = SettingsFragment.newInstance(null)
            supportFragmentManager.beginTransaction().add(R.id.fragment_container, settingsFragment!!, TAG).commit()
        } else {
            onBackStackChanged()
        }
    }

    override fun onBuildPreferenceFragment(rootKey: String?): PreferenceFragmentMaterial {
        return SettingsFragment.newInstance(rootKey)
    }

    override fun onBackStackChanged() {
        settingsFragment = supportFragmentManager.findFragmentByTag(TAG) as SettingsFragment?
        title = settingsFragment!!.preferenceFragmentTitle
    }
}

Preference Key Constants Generator

Material Preference has a capability to auto-generate your preference keys in a constant class. By default, this class is named PrefKey. With this generator, you don't need to rewrite constant field each time you modify preference key from file res/xml/preferences.xml. It improves accuracy in writing constant values.

To enable this feature, simply add the following configuration to your build.gradle:

apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-kapt' // Add this line

dependencies {
    implementation 'com.anggrayudi:materialpreference:3.1.4'
    kapt 'com.anggrayudi:materialpreference-compiler:1.1'
}

From your SettingsFragment class:

@PreferenceKeysConfig // Add this annotation
class SettingsFragment : PreferenceFragmentMaterial() {

    override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
        addPreferencesFromResource(R.xml.preferences)
        // You can access the constant values with auto-generated class named PrefKey
        findPreference(PrefKey.ABOUT)!!.summary = BuildConfig.VERSION_NAME
    }
}

Note:

  • If PrefKey does not update constant fields, click Make Project in Android Studio.
  • This generator wont work with Android Studio 3.3.0 Stable, 3.4 Beta 3, and 3.5 Canary 3 because of this bug. The fixes is available in the next version of Android Studio.

Preferences

  • Preference
  • CheckBoxPreference
  • SwitchPreference
  • EditTextPreference
  • ListPreference
  • MultiSelectListPreference
  • SeekBarDialogPreference
  • SeekBarPreference
  • RingtonePreference
  • IndicatorPreference
  • FolderPreference
  • DatePreference
  • TimePreference
  • ColorPreference

RingtonePreference

RingtonePreference will show only system ringtone sounds by default. If you want to include sounds from the external storage your app needs to request android.permission.READ_EXTERNAL_STORAGE permission in its manifest. Don't forget to check this runtime permission before opening ringtone picker on API 23.

Donation

Any donation you give is really helpful for us to develop this library. It feels like energy from power stone.

License

Copyright 2018-2019 Anggrayudi Hardiannicko A.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.