Android Tutorial PreferenceActivity tutorial

This tutorial explains how to use the new PreferenceActivity library. This library allows you to create the "standard" settings screen.

The settings screen is hosted in its own activity. As this is an external activity its declaration should be manually added to the manifest file.
I will take this opportunity to give some tips about manual modification of the manifest file which is sometimes required.

Editing AndroidManifest.xml (general overview)
As of Basic4android v1.8 it is no longer required to manually maintain the manifest file. Instead the manifest editor allows you to add extra elements as needed.
See this link for more information: http://www.b4x.com/forum/showthread.php?p=78136
For PreferenceActivity you should add the following code to the manifest editor:
B4X:
AddApplicationText(<activity android:name="anywheresoftware.b4a.objects.preferenceactivity"/>)
Back to PreferenceActivity

preference_1.png


* This image was taken on a Samsung Galaxy Tab device. On other devices the screen will look a bit different based on their default style.

PreferenceActivity library includes two main objects: PreferenceScreen and PreferenceManager.
PreferenceScreen is responsible for building the settings UI. PreferenceManager allows you to read the settings and also modify them by code.
Both these objects should usually be declared in Sub Process_Globals.
The settings are saved in a file in the internal files folder. File handling is done automatically. Note that when you reinstall an application (without uninstalling it) the files are kept so the settings will also be kept.

Each preference entry is stored as a key/value pair. The key is a string (case sensitive) and the value can be either a string or a boolean value.
Each entry must have a unique key.

Building the UI
In order to build the UI we start with a PreferenceScreen object and add entries to this object directly or to PreferenceCategory which can hold other entries.
In the above screenshot you can see a PreferenceScreen with two categories holding other entries.
Note that a PreferenceScreen can also hold secondary PreferenceScreens. In this case when the user presses on the secondary entry a new screen will appear with its entries.
There are three types of preference entries:
- Checkbox
- EditText - Opens an input dialog when the user presses on it.
- List - Open a list dialog when the user presses on it, allowing the user to select a single item.
The code that creates the above UI is:
B4X:
Sub CreatePreferenceScreen
    screen.Initialize("Settings", "")
    'create two categories
    Dim cat1, cat2 As PreferenceCategory
    cat1.Initialize("Category 1")
    cat1.AddCheckBox("check1", "Checkbox1", "This is Checkbox1", True)
    cat1.AddCheckBox("check2", "Checkbox2", "This is Checkbox2", False)
    cat1.AddEditText("edit1", "EditText1", "This is EditText1", "Hello!")
 
    cat2.Initialize("Category 2")
    cat2.AddList("list1", "List1", "This is List1", "Black", _
        Array As String("Black", "Red", "Green", "Blue"))
     
    'add the categories to the main screen
    screen.AddPreferenceCategory(cat1)
    screen.AddPreferenceCategory(cat2)
End Sub
The first string in the Add methods is the entry key followed by the title, summary and default value.
The default value is used if the key does not yet exist.

To show the preferences we use this code:
B4X:
Sub btn_Click
    StartActivity(screen.CreateIntent)
End Sub
Reading the settings
Reading the settings is done with PreferenceManager.
To retrieve the value of a specific key you should use Manager.GetString or Manager.GetBoolean with this key.
You can get a Map with all the stored keys and values by calling Manager.GetAll
If you want to only handle updated settings then you can call Manager.GetUpdatedKeys. This will return a List with the keys that were updated since the last call to this method.
Usually you will want to handle settings updated in Activity_Resume. Activity_Resume will be called right after Activity_Create when you start your program and also when the user closes the preferences screen.
Whether you want to handle all keys or just the updated keys depends on your application.

Tip:
The default values set in the PreferenceScreen.Add calls have no effect till the user actually sees the screen. It is recommended to explicitly set the default values if they do not already exist.
A simple way to do it is with code similar to:
B4X:
Sub Activity_Create(FirstTime As Boolean)
    If FirstTime Then
        CreatePreferenceScreen
        If manager.GetAll.Size = 0 Then SetDefaults
    End If
End Sub

Sub SetDefaults
    'defaults are only set on the first run.
    manager.SetBoolean("check1", True)
    manager.SetBoolean("check2", False)
    manager.SetString("edit1", "Hello!")
    manager.SetString("list1", "Black")
End Sub
This way our code in Activity_Resume will work correctly whether it is the first time the program runs or after the user has closed the settings page.

The program is attached.
 

Attachments

  • PreferenceActivity.zip
    6.5 KB · Views: 1,054
Last edited:

Cüneyt Gargin

Member
Licensed User
Longtime User
Hi, Erel,,


When I call
StartActivity(screen.CreateIntent)

I get exception :

Error occurred on line: 71 (passcheck)
android.content.ActivityNotFoundException: Unable to find explicit activity class {yupoc.trinoks.com/anywheresoftware.b4a.objects.preferenceactivity}; have you declared this activity in your AndroidManifest.xml?
at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1622)
at android.app.Instrumentation.execStartActivity(Instrumentation.java:1419)
at android.app.Activity.startActivityForResult(Activity.java:3354)
at android.app.Activity.startActivityForResult(Activity.java:3315)
at android.app.Activity.startActivity(Activity.java:3552)
at android.app.Activity.startActivity(Activity.java:3520)
at anywheresoftware.b4a.keywords.Common.StartActivity(Common.java:686)
at yupoc.trinoks.com.passcheck._btnok_click(passcheck.java:402)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:636)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:305)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:238)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:121)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:162)
at anywheresoftware.b4a.BA.raiseEvent(BA.java:158)
at anywheresoftware.b4a.objects.ViewWrapper$1.onClick(ViewWrapper.java:66)
at android.view.View.performClick(View.java:4084)
at android.view.View$PerformClick.run(View.java:16966)
at android.os.Handler.handleCallback(Handler.java:615)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4881)
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:789)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:556)
at dalvik.system.NativeStart.main(Native Method)




I added
<activity android:name="anywheresoftware.b4a.objects.preferenceactivity"/>

to manifest file...

I manifest editor I added....

'This code will be applied to the manifest file during compilation.
'You do not need to modify it in most cases.
'See this link for for more information: http://www.b4x.com/forum/showthread.php?p=78136
AddManifestText(
<uses-sdk android:minSdkVersion="4" android:targetSdkVersion="14"/>
<supports-screens android:largeScreens="true"
android:normalScreens="true"
android:smallScreens="true"
android:anyDensity="true"/>)
AddApplicationText(<activity android:name="yupoc.trinoks.objects.preferenceactivity"/>)
SetApplicationAttribute(android:icon, "@drawable/icon")
SetApplicationAttribute(android:label, "$LABEL$")
'End of default text.


Can you help me ?
 

Bitcrawler

Member
Licensed User
Longtime User
Hi All,

for my project I use the PreferenceActivity and the CameraExClass.
It works fine until I use the Preferences Screen.

B4X:
Sub Flashlight_Click
   
    Try
        Dim flashModes As List = camEx.GetSupportedFlashModes
        If flashModes.IsInitialized = True Then
            Dim flash As String = flashModes.Get((flashModes.IndexOf(camEx.GetFlashMode) + 1) Mod flashModes.Size)
            camEx.SetFlashMode("torch")
            camEx.CommitParameters
        End If
           
        Catch
            Log("error")
    End Try
   
End Sub


Sub Flashlight_LongClick

    Try
        Dim flashModes As List = camEx.GetSupportedFlashModes
        If flashModes.IsInitialized = True Then
            camEx.SetFlashMode("off")
            camEx.CommitParameters
        End If
        Catch
            Log("error")
                   
    End Try
       
End Sub

In the Preferences Screen I use the Back Button, then I try to start Flashlight_Click again.
Now I get LastException java.lang.NoSuchMethodException: getSupportedFlashModes()


B4X:
Sub Activity_Create(FirstTime As Boolean)

    If FirstTime Then
        CreatePreferenceScreen
        If manager.GetAll.Size = 0 Then SetDefaults
    End If
    Activity.LoadLayout("P1")
    Detector.Initialize("Detector")
               
End Sub

B4X:
Sub Activity_Resume

    InitializeCamera
    HandleSettings
    Detector.Start
   
End Sub

B4X:
Private Sub InitializeCamera
    camEx.Initialize(PanelMain, frontCamera, Me, "Camera1")
    frontCamera = camEx.Front
End Sub

B4X:
Sub Activity_Pause (UserClosed As Boolean)
    camEx.Release
    Detector.Stop
'    Dim bmpClear As Bitmap
'    bmpClear.InitializeMutable(1,1)
'    pnlRose.SetBackgroundImage(bmpClear)
'    pnlNeedle.SetBackgroundImage(bmpClear)
       
End Sub

B4X:
Sub Camera1_Ready (Success As Boolean)
    If Success Then
        camEx.SetJpegQuality(90)
        camEx.CommitParameters
        camEx.StartPreview
        Log(camEx.GetPreviewSize)
        PanelMain.Visible = (False)

    Else
        ToastMessageShow("Cannot open camera.", True)
    End If
End Sub


When I go in to the Preferences, change something, close the app and start again, it works.

Any Idea?
 

Bitcrawler

Member
Licensed User
Longtime User
Thank you Erel.

Now I have the answer.
I insert Panel1.Visible = True in Activity_Resume. It works :)
 

Douglas Farias

Expert
Licensed User
Longtime User
@Erel
have a way to put a button to preference activity?
or a label with label_Click?

i need to make a log out on the preference activity, user open click at log out
show a msgbox2(yes, no) etc...

i see this on many apps configs, have a way to put a label with click or a button ?
 

Douglas Farias

Expert
Licensed User
Longtime User
See AHPreferenceActivity library.
i see but have only list, edittext, checkbox etc =(
any button or label click *-*
have another way to make this?
 

rosippc64a

Active Member
Licensed User
Longtime User
This library perfect for me, but one thing lack: an encryption. I cant't store password if anybody can read in the stored xml...

*******
SOLVED
*******

I solve the encryption: in the setdefaults part I set the 'default' values from my encrypted ini file. Then I open the setting screen: (StartActivity(screen.CreateIntent) see at example program).
After closing the setting screen I run a sub like in demo named handlesettings, where I save setted values to my encrypted ini file (and of course I use in the program), and clear all value from manager: manager.clearall. So what xml is saved by ahpreferenceactivity contains nothing.
 
Last edited:

David S

Member
Licensed User
Longtime User
In the v1.04 example for EditText2 you have the following line....

cat1.AddEditText2("edit2", "EditText2", "This is a number input field", "1", 2, False, True, "")

The magic number "2" is the input type. I've been trying to find the documentation that coverts DECIMAL_NUMBER (TYPE_NUMBER_FLAG_DECIMAL) into the integer value this represents.

if someone could point me in the right direction I'd appreciate it.
 

David S

Member
Licensed User
Longtime User
You can use:
B4X:
Dim et As EditText
cat1.AddEditText2("edit2", "EditText2", "This is a number input field", "1", et.INPUT_TYPE_NUMBERS, False, True, "")

ahhh... well... that is one way to get a needed value. Does seem a bit like stealing. lol

So I can user et.INPUT_TYPE_DECIMAL_NUMBERS? which I assume is the value 8194.


Thanks

*** I was wrong! INPUT_TYPE_DECIMAL_NUMBERS = 12,290 (0x3002) *** 11/19 @ 1528
 
Last edited:

David S

Member
Licensed User
Longtime User
Last edited:

David S

Member
Licensed User
Longtime User
cat2.AddList("list1", "List1", "This is List1", "Black", _
Array As String("Black", "Red", "Green", "Blue"))

In the tutorial in post 1 this method is missing the Dependency parameter and causes an error when compiled.
 

bluejay

Active Member
Licensed User
Longtime User
InputType
Bit definitions for an integer defining the basic content type of text held in an Editable object. Supported classes may be combined with variations and flags to indicate desired behaviors.

Google could do better with the documentation.

First you select what type of input, in this case
TYPE_CLASS_NUMBER (value = 2)

then add flags to vary the behavious eg
TYPE_NUMBER_FLAG_DECIMAL (value = 8192)

So the final integer value = Bit.OR(TYPE_CLASS_NUMBER ,TYPE_NUMBER_FLAG_DECIMAL ) = 8194

BlueJay


 
Top