/MaterialPreference

A library designed to replace default preferences on Android framework with something beauty.

Primary LanguageKotlinApache License 2.0Apache-2.0

Material Preference Download

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.
Google Play


Screenshots

Alt text Alt text Alt text Alt text

Usage

Basic

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

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/' }
    // If error "Failed to resolve com.afollestad.material-dialogs" appears, add the following line
    maven { url "https://dl.bintray.com/drummer-aidan/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)
    }
}

From your SettingsActivity:

class SettingsActivity : PreferenceActivityMaterial() {

    private var settingsFragment: SettingsFragment? = null
    
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        
        if (savedInstanceState == null) {
            settingsFragment = SettingsFragment.newInstance(null)
            supportFragmentManager.beginTransaction().add(R.id.fragment_container, settingsFragment!!, TAG).commit()
        } else {
            settingsFragment = supportFragmentManager.findFragmentByTag(TAG) as SettingsFragment?
            title = settingsFragment!!.preferenceFragmentTitle
        }
    }
}

Preference Key Constants Generator Download

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.2'
    kapt 'com.anggrayudi:materialpreference-compiler:1.0.0'
}

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 Alt text Make Project in Android Studio.
  • This generator wont work with Android Studio 3.3.0 since Google does not fix this bug yet.

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.

Donate with PayPal

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.