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:

woniol

Active Member
Licensed User
Longtime User
Hi,

I have the same problem after upgrade to B4A 9.80.
B4X:
activity_preferences_globals (java line: 433)
java.lang.NoClassDefFoundError: Failed resolution of: Landroidx/preference/XpPreferenceManager;
    at de.amberhome.preferences.PreferenceManagerWrapper.<init>(PreferenceManagerWrapper.java:21)
    at com.woniol.domeq.activity_preferences._globals(activity_preferences.java:433)
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:196)
    at com.woniol.domeq.activity_preferences.initializeGlobals(activity_preferences.java:320)
    at com.woniol.domeq.activity_preferences.afterFirstLayout(activity_preferences.java:101)
    at com.woniol.domeq.activity_preferences.access$000(activity_preferences.java:17)
    at com.woniol.domeq.activity_preferences$WaitForLayout.run(activity_preferences.java:82)
    at android.os.Handler.handleCallback(Handler.java:739)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:158)
    at android.app.ActivityThread.main(ActivityThread.java:7224)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
Caused by: java.lang.ClassNotFoundException: Didn't find class "androidx.preference.XpPreferenceManager" on path: DexPathList[[zip file "/data/app/com.woniol.domeq-1/base.apk"],nativeLibraryDirectories=[/data/app/com.woniol.domeq-1/lib/arm, /vendor/lib, /system/lib]]
    at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:469)

Any ideas how to solve it?
 
Last edited:

woniol

Active Member
Licensed User
Longtime User
I know it is directly related to AndroidX rather then B4A version.
I get the error message using version 3 of PreferencesLib.

It is also with the example from first post.
 
Last edited:

woniol

Active Member
Licensed User
Longtime User
Thats what I meant. It is AndroidX problem not B4A.

Think, I'll have to switch to B4XPreferencesDialog but this will require some additional work.
 

corwin42

Expert
Licensed User
Longtime User
I have no idea what you are doing wrong. I can't reproduce the problem.

Which android.jar are you using? Are you sure you have the 3.0 library version installed? Have you jetified everything? Have you tried to clean the project?
 

woniol

Active Member
Licensed User
Longtime User
Hi,

I managed to solve it with the following steps:
1. Fresh install of B4A (9.80)
2. New Additional Libraries folder creation (out of Program Files for permissions)
3. Fresh download of the additional libraries (latest versions) needed for my project.
4. Jetified (Tools - Jetifier)
5. Clean project (Tools - Clean project)
and it works......

So it was definitely related to my installation, which was not refreshed for a long time.
I think it is a good idea to have fresh install after switching to AndroidX.

Thanks for your help
 

Toky Olivier

Active Member
Licensed User
Longtime User
Hello all and @corwin42,
I'm having the same issue but cleaning all and restart from new installation hasn't solved my case.
The main issue is likely "Didn't find class "androidx.preference.XpPreferenceManager""
Switch to B4XPreference dialog may be a solution even if I have to modify many thing but what will replace PreferenceManager class from @corwin42 Preferences library?

Thank you

B4X:
java.lang.RuntimeException: Unable to create service mg.soinalastudio.alimanaka.starter: java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
    at android.app.ActivityThread.handleCreateService(ActivityThread.java:3544)
    at android.app.ActivityThread.access$1300(ActivityThread.java:199)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1666)
    at android.os.Handler.dispatchMessage(Handler.java:106)
    at android.os.Looper.loop(Looper.java:193)
    at android.app.ActivityThread.main(ActivityThread.java:6669)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
Caused by: java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
    at mg.soinalastudio.alimanaka.starter.onCreate(starter.java:39)
    at android.app.ActivityThread.handleCreateService(ActivityThread.java:3532)
    ... 8 more
Caused by: java.lang.reflect.InvocationTargetException
    at java.lang.reflect.Method.invoke(Native Method)
    at mg.soinalastudio.alimanaka.starter.onCreate(starter.java:37)
    ... 9 more
Caused by: java.lang.NoClassDefFoundError: Failed resolution of: Landroidx/preference/XpPreferenceManager;
    at de.amberhome.preferences.PreferenceManagerWrapper.<init>(PreferenceManagerWrapper.java:21)
    at mg.soinalastudio.alimanaka.starter._process_globals(starter.java:207)
    at mg.soinalastudio.alimanaka.main.initializeProcessGlobals(main.java:2291)
    ... 11 more
Caused by: java.lang.ClassNotFoundException: Didn't find class "androidx.preference.XpPreferenceManager" on path: DexPathList[[zip file "/data/app/mg.soinalastudio.alimanaka-A7D0HIE9ESEWxXlHterDYA==/base.apk"],nativeLibraryDirectories=[/data/app/mg.soinalastudio.alimanaka-A7D0HIE9ESEWxXlHterDYA==/lib/x86_64, /system/lib64]]
    at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:134)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:379)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:312)
    ... 14 more
java.lang.RuntimeException: Unable to create service mg.soinalastudio.alimanaka.starter: java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
    at android.app.ActivityThread.handleCreateService(ActivityThread.java:3544)
    at android.app.ActivityThread.access$1300(ActivityThread.java:199)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1666)
    at android.os.Handler.dispatchMessage(Handler.java:106)
    at android.os.Looper.loop(Looper.java:193)
    at android.app.ActivityThread.main(ActivityThread.java:6669)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
Caused by: java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
    at mg.soinalastudio.alimanaka.starter.onCreate(starter.java:39)
    at android.app.ActivityThread.handleCreateService(ActivityThread.java:3532)
    ... 8 more
Caused by: java.lang.reflect.InvocationTargetException
    at java.lang.reflect.Method.invoke(Native Method)
    at mg.soinalastudio.alimanaka.starter.onCreate(starter.java:37)
    ... 9 more
Caused by: java.lang.NoClassDefFoundError: Failed resolution of: Landroidx/preference/XpPreferenceManager;
    at de.amberhome.preferences.PreferenceManagerWrapper.<init>(PreferenceManagerWrapper.java:21)
    at mg.soinalastudio.alimanaka.starter._process_globals(starter.java:207)
    at mg.soinalastudio.alimanaka.main.initializeProcessGlobals(main.java:2291)
    ... 11 more
Caused by: java.lang.ClassNotFoundException: Didn't find class "androidx.preference.XpPreferenceManager" on path: DexPathList[[zip file "/data/app/mg.soinalastudio.alimanaka-A7D0HIE9ESEWxXlHterDYA==/base.apk"],nativeLibraryDirectories=[/data/app/mg.soinalastudio.alimanaka-A7D0HIE9ESEWxXlHterDYA==/lib/x86_64, /system/lib64]]
    at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:134)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:379)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:312)
    ... 14 more
 
Cookies are required to use this site. You must accept them to continue using the site. Learn more…