Today in France, we celebrate ""
Posts
04/07/10 : Android sorting
16/06/10 : Flex XML parsing
15/06/10 : Android settings
14/06/10 : Pax daemon
10/06/10 : Bundle CPU %
04/06/10 : Wicket CheckBox
03/06/10 : Thread monitor

By tag
Ajax
Android
AspectJ
Flex
Java
JMX
OSGi
Shell
Wickets

Here are some lights and tips on various Object Oriented Programming aspects that I would have dealt with during my software development and architecture activities..., for sharing.

"Beauty is an inexhaustible source of happyness for the one who discovers it" (Alexis Carrel)



Tuesday, 15 June 2010

Android application preferences

Question : how to access/set preferences of an Android application ?

Answer :
- think about the MENU_SETUP of the onOptionsItemSelected method (override)
- from there, start a specialized PreferenceActivity


Details :

Step #1 : MENU_SETUP press handling :

@Override
public boolean onOptionsItemSelected(MenuItem item)
{
switch (item.getItemId())
{
case MENU_SETUP:
showPreferences();
break;

default:
break;
}

return true;
}

Step #2 : an Android intent allows to start a to-be-specialized PreferenceActivity, named here : Prefs

/**
* Dialog box display of the preferences setting window
*/
private void showPreferences()
{
Intent prefs = null; // Preferences activity display intent

prefs = new Intent(getApplicationContext(), Prefs.class);
startActivity(prefs);
}

Step #3 : specialized Prefs class body

public final class Prefs extends PreferenceActivity
{
@Override
public void onCreate(Bundle savedInstanceState)
{
// Setting activity content
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.layout.prefs);
}
}
The 'R.layout.prefs' being given by a './res/layout/Prefs.xml' resource file ; here is such a resource contents in case just 2 edit text components are needed :
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
android:title="Preferences">
<EditTextPreference
// ...
/>
<EditTextPreference
// ...
/>
</PreferenceScreen>


Step #4 : finally, add this entry into the AndroidManifest.xml :
<activity android:name=".Prefs" android:label="Configuration"/>

No comments:

Post a Comment

Previous Post Next Post