B4A Library AHPreferenceActivity Library

This Library is based on Erels PreferenceActivity Library and adds some hopefully useful things:

  • Checkbox entry can show On and Off summary
  • Dependencies to other entries
  • Another Listview type with Map as value/displayvalue pairs
  • AddPassword() for password entries
  • AddRingtone() for selecting notification, ringtone or alert sound

V1.01:
  • Bugfix for FC with nested PreferencesScreens
  • Added support for calling Intents (other activities)
  • Preferencescreens support dependency

V1.02:
  • Bugfix for GetUpdatedKeys not working sometimes (thanks, Erel)

V1.03:
  • Never published

V1.04:
  • Added AddEditText2() method with support for InputType and some other properties
  • !!! Changed package name to "de.amberhome.objects" !!! Change in your Manifest!

For a tutorial see PreferenceActivity Tutorial
 

Attachments

  • AHPreferenceActivityExample.zip
    6.6 KB · Views: 2,093
  • AHPreferenceActivity1_02.zip
    21.4 KB · Views: 2,003
  • AHPreferenceActivity1_04.zip
    22.1 KB · Views: 2,573
  • AHPreferenceActivityExample1_04.zip
    7.1 KB · Views: 2,010
Last edited:
D

Deleted member 103

Guest
Hallo corwin42,

ich möchte die Liste der "Ringtone" in einen Spinner anzeigen und zum auswählen anbieten, kann man die funktion "AddRingtone" irgendwie dafür verwenden?

I would like to view the list of the "Ringtone" into a spinner and choose to offer, can the function "AddRingtone" use it somehow?

edit:
habe mein Problem gelöst. Ich suche unter "/system/media/audio/alarms" und erstelle eine Liste für den Spinner.

Ciao,
Filippo
 
Last edited by a moderator:

kiwi

Member
Licensed User
Longtime User
Oposit dependency in AHPreferences

Hello,

Here is example I use the screen dependens on "check1"

cat1.Initialize("Nast")
cat1.AddCheckBox("check1", "Manual/Auto", "Manual", "Auto", True, "")
prac.Initialize("Po-Pa","check")
prac.AddCheckBox("pr1","1","","",False,"")
prac.AddCheckBox("pr2","2","","",False,"")
cat1.AddPreferenceScreen(prac,"check1")

If check1 is true screen will be accessible and if not checked screen will not be accessible. It is clear, but I need together add another checkbox dependes oposit on check1. Something like this:

cat1.AddCheckBox("checkoposit", "Auto1/Auto2", "Auto ver.1", "Auto ver.2", True, NOT "check1")

If "check1" is true check oposit will not be accessible and if not checked it will be accesible

Thanks for your suggestions........ kiwi
 

thedesolatesoul

Expert
Licensed User
Longtime User
Okay, with the help of NJDude I got that one working.

However try to get a 'Share via' intent, I am struggling.

This code to put the intent in the preference (using it in an Activity with StartActivity works great):
B4X:
Dim share As Intent
   Dim sharetext As String 
   sharetext = "Hey," 
   share.Initialize(share.ACTION_SEND,"")
   share.SetType("text/plain")
   share.PutExtra("android.intent.extra.TEXT", sharetext)
   share.WrapAsIntentChooser("Share text via")
   
   
   cat2.AddIntent("Share","Share this app with someone",share,"")
when using this code I get the following exception:
B4X:
android.app.SuperNotCalledException: Activity {android/com.android.internal.app.ChooserActivity} did not call through to super.onCreate()
   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1922)
   at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
   at android.app.ActivityThread.access$600(ActivityThread.java:123)
   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
   at android.os.Handler.dispatchMessage(Handler.java:99)
   at android.os.Looper.loop(Looper.java:137)
   at android.app.ActivityThread.main(ActivityThread.java:4424)
   at java.lang.reflect.Method.invokeNative(Native Method)
   at java.lang.reflect.Method.invoke(Method.java:511)
   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:787)
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:554)
   at dalvik.system.NativeStart.main(Native Method)

If I remove the line for WrapAsIntentChooser, I get the following exception:
B4X:
android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.SEND }
   at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1512)
   at android.app.Instrumentation.execStartActivity(Instrumentation.java:1384)
   at android.app.Activity.startActivityForResult(Activity.java:3190)
   at android.app.Activity.startActivity(Activity.java:3297)
   at android.preference.Preference.performClick(Preference.java:957)
   at android.preference.PreferenceScreen.onItemClick(PreferenceScreen.java:202)
   at android.widget.AdapterView.performItemClick(AdapterView.java:292)
   at android.widget.AbsListView.performItemClick(AbsListView.java:1060)
   at android.widget.AbsListView$PerformClick.run(AbsListView.java:2516)
   at android.widget.AbsListView$1.run(AbsListView.java:3170)
   at android.os.Handler.handleCallback(Handler.java:605)
   at android.os.Handler.dispatchMessage(Handler.java:92)
   at android.os.Looper.loop(Looper.java:137)
   at android.app.ActivityThread.main(ActivityThread.java:4424)
   at java.lang.reflect.Method.invokeNative(Native Method)
   at java.lang.reflect.Method.invoke(Method.java:511)
   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:787)
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:554)
   at dalvik.system.NativeStart.main(Native Method)
 

corwin42

Expert
Licensed User
Longtime User
Support for Intents is very minimal because I couldn't store the complete intent. This is a limitation of how the library is designed and would be hard to solve. The problem was that all data used in the library must be parcelable (something like serializable) and this is not true for Intents. So the AddIntent() method uses a small hack to store the most important data of an Intent in strings to recreate the intent later.

Only action, component and data URI are stored in the moment. There is no support for extra data or other advanced features of intents.

It mainly can be used to start Android settings dialogs or start the browser with a URL.
 

thedesolatesoul

Expert
Licensed User
Longtime User
Support for Intents is very minimal because I couldn't store the complete intent. This is a limitation of how the library is designed and would be hard to solve. The problem was that all data used in the library must be parcelable (something like serializable) and this is not true for Intents. So the AddIntent() method uses a small hack to store the most important data of an Intent in strings to recreate the intent later.

Only action, component and data URI are stored in the moment. There is no support for extra data or other advanced features of intents.

It mainly can be used to start Android settings dialogs or start the browser with a URL.
Okay, I see the problem.
I was trying to open a filedialog or timepicker dialog as well to set some preferences but it seems it is not possible.
I was also hoping to be able to use StartActivityForResult to start an activity that would return some path to the preferences activity but that also seems not possible.
My last option would be to implement a CustomDialogPreference as here: java - Concise way of writing new DialogPreference classes? - Stack Overflow
 

barx

Well-Known Member
Licensed User
Longtime User
Not sure if I'm missing something or if the following is not possible.

Basically I want to add an item to preference screen, but I don't want it to be an edit text or a checkbox, etc. I want it to be used to start a service within the app.

i.e. use taps 'advanced' and up pops preference screen. Then there would be an item that says start service. upon tapping, the preference screen closes and can check the returned data to see if the selected value is present. Then if found, start the service.

possible???

thanks
 

barx

Well-Known Member
Licensed User
Longtime User
sounds a bit overkill. using intent to call a service with the same app.

Think I'll just bail on AHPrefs lol
 

barx

Well-Known Member
Licensed User
Longtime User
Use AddIntent, and create an intent for your service. Also read the 2 posts above if you're intent is more complicated.

Sent from my GT-I9000 using Tapatalk 2

OK, maybe this isn't overkill. I'm coming round to the idea a little, so.........

How do I do it, never used intents and not 100% on their use.

thank you please...
 

barx

Well-Known Member
Licensed User
Longtime User
Can somebody tell me, as I can't find the info either way, are the settings that are created on a preference screen persistent.

By that I mean, I write an app and use AHPreferenceActivity Library, Create my preference screen with categories and and items, etc. Then a user edits the preferences. Now I know I need to use the preference manager to read the settings but, do I need to store them say, in a file using Map? and then load the preference values back in when app starts, or are they already saved somewhere and persist an app being killed and re-started.

Thanks
 

barx

Well-Known Member
Licensed User
Longtime User
I know i shouldn't really do this but could really do with knowing this info. So.....

Bump!
 

corwin42

Expert
Licensed User
Longtime User
Can somebody tell me, as I can't find the info either way, are the settings that are created on a preference screen persistent.

Yes, the settings will be persistent automatically. See the example.
 

barx

Well-Known Member
Licensed User
Longtime User
Thanks corwin
 

barx

Well-Known Member
Licensed User
Longtime User
Not sure if I'm missing something or if the following is not possible.

Basically I want to add an item to preference screen, but I don't want it to be an edit text or a checkbox, etc. I want it to be used to start a service within the app.

i.e. use taps 'advanced' and up pops preference screen. Then there would be an item that says start service. upon tapping, the preference screen closes and can check the returned data to see if the selected value is present. Then if found, start the service.

possible???

thanks

Use AddIntent, and create an intent for your service. Also read the 2 posts above if you're intent is more complicated.

Sent from my GT-I9000 using Tapatalk 2

OK, on a similar note to this, I now wish to have a menu item that is 'About' and upon tapping it launches an About activity. I understand this will need to use addintent but I have zero knowledge or experience of intents (some Android developer eh?). Can anyone give me a clue how I would achieve this?

thanks
 

Ionut Indigo

Member
Licensed User
Longtime User
I'm having a problem when compiling the application.
I added the code to a new activity, next to my other activities and when compiling i get this :
B4X:
Convert byte code - optimized dex.      Error
UNEXPECTED TOP-LEVEL EXCEPTION:
java.lang.IllegalArgumentException: already added: Lanywheresoftware/b4a/objects/preferenceactivity$B4APreference;
   at com.android.dx.dex.file.ClassDefsSection.add(ClassDefsSection.java:123)
   at com.android.dx.dex.file.DexFile.add(DexFile.java:163)
   at com.android.dx.command.dexer.Main.processClass(Main.java:486)
   at com.android.dx.command.dexer.Main.processFileBytes(Main.java:455)
   at com.android.dx.command.dexer.Main.access$400(Main.java:67)
   at com.android.dx.command.dexer.Main$1.processFileBytes(Main.java:394)
   at com.android.dx.cf.direct.ClassPathOpener.processArchive(ClassPathOpener.java:245)
   at com.android.dx.cf.direct.ClassPathOpener.processOne(ClassPathOpener.java:131)
   at com.android.dx.cf.direct.ClassPathOpener.process(ClassPathOpener.java:109)
   at com.android.dx.command.dexer.Main.processOne(Main.java:418)
   at com.android.dx.command.dexer.Main.processAllFiles(Main.java:329)
   at com.android.dx.command.dexer.Main.run(Main.java:206)
   at com.android.dx.command.dexer.Main.main(Main.java:174)
   at com.android.dx.command.Main.main(Main.java:91)
1 error; aborting
   Standard dexer.
 

Ionut Indigo

Member
Licensed User
Longtime User
I just copied the example code into a new activity, and i'm not using the library in other activities. Having 4 activities and one o them is the example one and the only one using the library... (Maybe it's a clash between the activities?)
I've tried removing the app and than compiling it again, and i still get that.
 
Top