B4A Library Preferences - Material Design Preferences

This library provides a framework to create nice looking Material Design Preferences down to API9. It uses this GitHub project to fix some bugs in the android support preferences stuff. It also adds some nice features like simple icon tinting, simple menu list preferences and a color picker.

For a guide on how to implement settings for Material Design read this.

Installation:
  • Extract all of the PreferencesLibX_XX.zip file to your additional libraries folder. These are .xml, .jar, 4 .aar files.
  • Requires AppCompat library.
  • Requires B4A 6.31+
minimum SDK Version: 14
Needs support library 27

If you are updating you can delete the old library aar files.

Your Support:
Creating libraries and wrappers for existing library projects is a lot of work. The use of this library is totally free and you even don't need to mention in your app that you use it.
But if you use this library in your projects and you think it is useful to you please consider to make a donation:


Thanks very much for your support.

Usage:

You need to use AppCompat Library together with this library.
Don't forget to add the Preference library to your project.

1. Enhance your theme in manifest editor
Add a preferenceTheme item and the two items for the IconTint:
B4X:
CreateResource(values, theme.xml,
<resources>
    <style name="MyAppTheme" parent="@style/Theme.AppCompat.Light.DarkActionBar">
        <item name="colorPrimary">#0098FF</item>
        <item name="colorPrimaryDark">#007CF5</item>
        <item name="colorAccent">#FF5522</item>
        <item name="windowNoTitle">true</item>
        <item name="windowActionBar">false</item>
        <item name="preferenceTheme">@style/PreferenceThemeOverlay.Material</item>
        <item name="asp_preferenceIconTint">?colorAccent</item>
        <item name="asp_preferenceDialogIconTint">?asp_preferenceIconTint</item>
        <item name="windowActionModeOverlay">true</item>
    </style>
</resources>
)

2. Extend from AppCompatPreferenceActivity
B4X:
#Extends: de.amberhome.preferences.AppCompatPreferenceActivity

3. Add PreferenceView to your Layout (with designer or by code)


4. Load the Layout in Activity_Create() as normal

5. Create your Preference elements in PreferenceView_Ready() event.

See the example for detailed usage.

The example has minSdkVersion set to 14. This is because the MaterialDateTimePicker needs this. The Preference library itself will work with a minimal SDK version of 9.

The example projects additionally needs MaterialDateTimePicker, XMLLayoutBuilder and DateUtils libraries.

Reference:

History:
V1.00:
  • Initial version
V1.01:
  • Fix: Change packagename to anywheresoftware.b4a.orbjects to reduce resource fields
V1.10:
  • New: MultiSelectListPreference
V1.11
  • Fix: Removed DialogMessage property from dialogs with lists (not supported)
  • Fix: Added missing properties to MultiSelectPreference
V1.12
  • Fix: PreferenceCategory.AddMultiSelectListPreference() should work now
  • Fix: AddPreference() should work with all preferences
  • New: Added RemoveKey() to PreferenceManager
  • Fix: Remove internal resource files
V1.14
  • Fix: Fix crash on screen rotation
  • Fix: Updated to support-preferences library 1.2.7 (fixes crash with ringtone manager on Samsung devices, fixes memory leak)
  • New: Better support for CharSequences
V2.00
  • Fix: Updated to support-preferences library 2.2.0 (fixes crashes with support libraries 27)
V3.00
  • Fix: Should work with androidx support libraries.
  • Fix: Updated to support-prefernces library 3.0.0dev
  • Attention: Removed RingtonePreference since it is not supported by androidx support library anymore!
ToDo:
  • Currently there is nearly no popup help.
 

Attachments

  • PreferencesLib2_0.zip
    405.7 KB · Views: 783
  • PreferencesExample1_12.zip
    19.7 KB · Views: 664
  • PreferencesLib3_0.zip
    427.2 KB · Views: 795
  • PreferencesExample2_00.zip
    19.8 KB · Views: 568
Last edited:

Pencil3

Member
Licensed User
Running into something strange. I added a second Edit.InputType, the first being set to INPUT_TYPE_DECIMAL_NUMBERS and the second set to INPUT_TYPE_DECIMAL_Text. The correct keyboard comes up for both. The EditTextPreference set to INPUT_TYPE_DECIMAL_NUMBERS I can see update in the log. The EditTextPreference set to INPUT_TYPE_DECIMAL_Text does not update in the log. Any ideas?
 

ernschd

Active Member
Licensed User
Longtime User
Thanks for this library.

Is it possible to use divider lines to group together related settings?
 

Scantech

Well-Known Member
Licensed User
Longtime User
After the SDK update this occurs?

 

corwin42

Expert
Licensed User
Longtime User
I can confirm this. I will try to update the original library. Hopefully this will fix the problem. The author says that Version 2.3.2 is compatible with AndroidX. My wrapper still uses 2.2.0.
 

so27

Active Member
Licensed User
Longtime User
I can confirm this. I will try to update the original library. Hopefully this will fix the problem. The author says that Version 2.3.2 is compatible with AndroidX. My wrapper still uses 2.2.0.

Same problem. How can I use this now?
 

so27

Active Member
Licensed User
Longtime User
In "tools..." I don't have a "b4a_local" Folder. This is only available in the root directory "Extras". Even in an older backup.

 

Scantech

Well-Known Member
Licensed User
Longtime User
While we are waiting, if you want to update your SDK and want this library to work, then avoid updating Androidx.Preference.Preference
 

so27

Active Member
Licensed User
Longtime User
While we are waiting, if you want to update your SDK and want this library to work, then avoid updating Androidx.Preference.Preference

Thanks for the tip. Now I have updated my SDK again after the backup. Great.
 

corwin42

Expert
Licensed User
Longtime User
You will find library version 3.0 in the first post which should fix the issue with the latest androidx support library.

I could only fix this issue removing the RingtonePreference. RingtonePreference is not supported anymore in AndroidX libraries. You need to create a custom preference and work with Phone.RingtoneManager to work around this.
 

Ilya G.

Active Member
Licensed User
Longtime User
Good day, I have this error after update B4A 9.50. Library version 3.0

B4X:
main_process_globals (java line: 770)
java.lang.NoClassDefFoundError: Failed resolution of: Landroidx/preference/XpPreferenceManager;
    at de.amberhome.preferences.PreferenceManagerWrapper.<init>(PreferenceManagerWrapper.java:21)
    at kz.logist.main._process_globals(main.java:770)
    at kz.logist.main.initializeProcessGlobals(main.java:752)
    at kz.logist.main.afterFirstLayout(main.java:100)
    at kz.logist.main.access$000(main.java:17)
    at kz.logist.main$WaitForLayout.run(main.java:82)
    at android.os.Handler.handleCallback(Handler.java:754)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:163)
    at android.app.ActivityThread.main(ActivityThread.java:6221)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:904)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:794)
    at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:107)
Caused by: java.lang.ClassNotFoundException: Didn't find class "androidx.preference.XpPreferenceManager" on path: DexPathList[[zip file "/data/app/kz.logist-2/base.apk"],nativeLibraryDirectories=[/data/app/kz.logist-2/lib/arm64, /system/lib64, /vendor/lib64]]
    at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:380)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:312)
    ... 14 more
 

Pencil3

Member
Licensed User
You may need to run the Jetifier tool.

 
Cookies are required to use this site. You must accept them to continue using the site. Learn more…