Download the free trial version
Basic4android Video
Features
Tutorials and manuals
Showcase
Screenshots

Go Back   Android Development Forum - Basic4android > Basic4android > Basic4android Getting started & Tutorials
Documentation Wiki Register Members List B4P Search Today's Posts Mark Forums Read

Basic4android Getting started & Tutorials Android development starts here. Please do not post questions in this sub-forum.

PreferenceActivity tutorial

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 08-03-2011, 09:08 AM
Erel's Avatar
Administrator
 
Join Date: Apr 2007
Posts: 15,689
Awards Showcase
Basic4ppc Founder 
Total Awards: 1
Default 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.basic4ppc.com/forum/basic4android-getting-started-tutorials/13818-manifest-editor-post78136.html
For PreferenceActivity you should add the following code to the manifest editor:
Code:
AddApplicationText(<activity android:name="anywheresoftware.b4a.objects.preferenceactivity"/>)
Back to PreferenceActivity



* 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:
Code:
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:
Code:
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:
Code:
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.
The library is available here: http://www.basic4ppc.com/forum/addit...y-library.html
Attached Files
File Type: zip PreferenceActivityExample.zip (6.3 KB, 587 views)
Reply With Quote
  #2 (permalink)  
Old 08-03-2011, 11:07 AM
Erel's Avatar
Administrator
 
Join Date: Apr 2007
Posts: 15,689
Awards Showcase
Basic4ppc Founder 
Total Awards: 1
Default

The attached file was updated. The manifest file was located in the wrong folder in the previous zip file.
Reply With Quote
  #3 (permalink)  
Old 08-03-2011, 11:32 AM
Erel's Avatar
Administrator
 
Join Date: Apr 2007
Posts: 15,689
Awards Showcase
Basic4ppc Founder 
Total Awards: 1
Default

Last tip regarding the manifest file. The manifest file created each time is generated by a template. The template is located in the program folder (C:\Program Files\Anywhere Software\Basic4android) and is named AndroidManifest-template.xml.

Changes to the template will affect all the generated manifest files. If you are working on a single project you can in some cases add the changes to the template file and then let the IDE create the manifest file automatically. It is not always possible as sometimes you need to change values which are part of the dynamic content. In the case of PreferenceActivity it is possible to apply the change to the template file and then let the IDE create it each time.
Reply With Quote
  #4 (permalink)  
Old 08-03-2011, 05:18 PM
Cableguy's Avatar
Basic4ppc Expert
 
Join Date: Apr 2007
Location: N 41º11'30.30" W 8º39'46.60"
Posts: 2,344
Awards Showcase
Forum Contributer 
Total Awards: 1
Default

This is a very good tip and leads me to a "wish"....Wich I will post on the wish thread...hehehe
__________________
Paulo Gomes - Porto, Portugal - Living/Working in France
Mobile Device: Samsung Galaxy S, Android 2.3.4 CUstom ROM
Laptop: Toshiba NB100-130 (running on Win7Ultimate)

My Posts helped you? Consider Buying me a Porto Glass!
Reply With Quote
  #5 (permalink)  
Old 08-10-2011, 04:31 PM
Knows the basics
 
Join Date: Apr 2009
Posts: 52
Default

i run the sample in htc wildfire S. it is 10minutes now but display is still "Waiting for IDE debugger to connect".
Reply With Quote
  #6 (permalink)  
Old 08-11-2011, 06:08 AM
Erel's Avatar
Administrator
 
Join Date: Apr 2007
Posts: 15,689
Awards Showcase
Basic4ppc Founder 
Total Awards: 1
Default

You should either disable debugging or add the INTERNET permission to the manifest file: <uses-permission android:name="android.permission.INTERNET" />
Reply With Quote
  #7 (permalink)  
Old 08-11-2011, 08:05 PM
Junior Member
 
Join Date: Mar 2011
Posts: 39
Default

How do I create the "settings menu" that pops up at the bottom of the screen when you press the Settings key in most applications - see example at the bottom of the Android screen here:

http://www.slipperybrick.com/wp-cont...bile-phone.jpg
Reply With Quote
  #8 (permalink)  
Old 08-11-2011, 11:33 PM
Junior Member
 
Join Date: Jul 2011
Location: Earth
Posts: 18
Default

Quick question, as this has always bugged me about some Android apps.

I want to see the value of the setting reflected in the settings list, and some programs do it, but, most notabley, they actual OS very rarely does. It just says something like "Vibration Intensity" and then a "tool tip" like "Set your touch feedback vibration intensity"

It does not mention the actual value that it stored there.

So I took your wonderful example and move the list creation stuff to the button click

If manager.GetAll.Size = 0 Then SetDefaults
CreatePreferenceScreen
StartActivity(screen.CreateIntent)

So that we always create the Activity from scratch.

In the Create I amended a line to suck the value from the Manager

cat2.AddList("list1", "List1", "Value(" & manager.GetString("list1") & ")", "", _
Array As String("Black", "Red", "Green", "Blue"))

So now I at least see (Black) or some other string.

My question is "how do i trap the event" that says "single setting updated" so that I can update my screen as soon as the user has made a new selection? Right now I have to press "back" to go to the main screen and then press "Settings" again.

Do you expose those kind of events?

Thanks.
And again, great work.

Duncan
Reply With Quote
  #9 (permalink)  
Old 08-12-2011, 03:24 AM
Junior Member
 
Join Date: Mar 2011
Posts: 39
Default

Second question - can event handlers be attached to radio boxes being selected in a PreferenceCategory?

For instance, in the example you provided, I may want to run a custom method when I select the "Green" value in list1.

If this is not possible, I think it should be added.
Reply With Quote
  #10 (permalink)  
Old 08-12-2011, 07:36 AM
Erel's Avatar
Administrator
 
Join Date: Apr 2007
Posts: 15,689
Awards Showcase
Basic4ppc Founder 
Total Awards: 1
Default

Currently no events are available. Events can be added in the future. However you will need to handle the events from a service as your activity is paused when the preferences are displayed. This makes it a bit cumbersome.
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
GPS tutorial Erel Basic4android Getting started & Tutorials 62 05-22-2012 02:04 PM
SQL Tutorial klaus German Tutorials 2 11-11-2011 03:15 PM
GPS Tutorial klaus German Tutorials 3 02-04-2011 06:08 PM
Need SQL tutorial Cableguy Basic4android Updates and Questions 11 11-30-2010 07:03 AM
New GPS tutorial Erel Announcements 2 11-05-2007 11:39 AM


All times are GMT. The time now is 09:58 AM.


Powered by vBulletin® Version 3.6.12
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0