Skip to content

kmansoft/prefsx

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

87 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

prefsx - a PreferenceActivity for AndroidX

Since API 29, the "good old" PreferenceActivity is deprecated and you will get a lot of warnings trying to use any classes in android.preference.

AndroidX provides replacements for Preference classes, PreferenceScreen, PreferenceManager, etc. - but does not provide a PreferenceActivity.

PreferenceActivity is/was a top level entry point into an app's preference hierarchy, displaying the list of preference headers (logical sections) and allowing the user to navigate into and out of a particular section (screen with actual preferences).

That this activity is absent in AndroidX means that every app has to implement it in its code.

Or, you can use this project instead.

The license is Apache 2.0, the code is Kotlin (should not be a problem to use from Java).

Solves these problems

Provides a PreferenceActivityX class.

Solves the problem of large padding on the left of preferences that don't have icons, this is optional and can be turned off with a resource.

Two panel layout on tablets.

If you have a custom preference that used to subclass from DialogPreference and display a custom dialog, this can be a bit annoying with Android X. An easier way is provided by this library.

Bonus preference classes including RingTonePreference which is also missing in AndroidX.

Installation

At this time I don't have a repository for Gradle, so please clone the project either into a git submodule in your project, or just copy the files over. Add the directory as a dependency in your build.gradle.

Your preference activity and fragments

Just derive your preference activity from PreferenceActivityX and your preference fragment classes from PreferenceFragmentX.

Your activity will need to implement onBuildHeaders to provide the list of top level headers (settings section), same as it did with android.preference

It's easiest to use loadHeadersFromResource. If you are converting from PreferenceActivity, your existing xml should work.

Collapse large padding reserved for icons

The library, by default, will collapse the large padding on the left of each preference or preference category (reserved by Google for icons even if you don't use them).

If you need to turn this off and keep the padding, please add this as a resource to your application:

<bool name="prefsx_collapse_icon_space_reserved">false</bool>

RingtonePreference

Please use org.kman.prefsx.RingtonePreferenceX as a direct replacement.

DialogPreference subclasses

Unlike with android.preference, AndroidX expects the dialog to be separated out into a subclass of PreferenceDialogFragmentCompat. The code to create and show this dialog fragment, by default, is expected to be in your preference activity or fragment.

This library automatically routes dialog creation back to any subclass of DialogPreferenceX:

class MyPreference(context: Context, attrs: AttributeSet)
    : DialogPreferenceX(context, attrs) {
// ......
	override fun createDialogFragment(): DialogFragment? {
        // My dialog fragment
		return MyPreferenceDialog.newInstance(key)
	}
}

Please see IntegerListPreference for an example.

Bonus preference classes

IntegerListPreference is similar to the framework's ListPreference but uses integers as values (not strings as ListPreference does).

This can be more convenient for simple settings with mutually exclusive values, for example the app's theme.

<?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">

	<org.kman.prefsx.IntegerListPreference
		android:defaultValue="0"
		android:key="theme"
		android:title="@string/theme"
		app:entryList="@array/theme_entry_list"
		app:valueList="@array/theme_value_list" />
</PreferenceScreen>
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string-array name="theme_entry_list">
        <item>@string/theme_light</item>
        <item>@string/theme_dark</item>
    </string-array>

    <integer-array name="theme_value_list">
        <item>0</item>
        <item>1</item>
    </integer-array>
</resources>

IntegerMaskPreference is a preference for selecting zero or more options for a single setting at the same time. Each is represented by a single bit in an integer, with the integer-array of these bits coming from the valueList attribute.


Copyright (c) 2020 Kostya Vasilyev

Apache License 2.0

Releases

No releases published

Packages

No packages published

Languages