[Java for Android App Development] xcesco89:
great! :D

i've managed today this thing :p

i just want to add a little thing to your HtcPreferenceActivity guide , you have forgot the listeners! :D

layout:

<com.htc.preference.HtcPreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >

<com.htc.preference.HtcPreferenceCategory
android:summary="GPU Settings"
android:title="GPU Frequencies" >
<com.htc.preference.HtcListPreference
android:key="maxgpu"
android:summary="Select max frequency"
android:entries="@array/gpumaxfreq"
android:entryValues="@array/gpumaxfreq"
android:title="GPU Max frequency" />
<com.htc.preference.HtcListPreference
android:key="mingpu"
android:summary="Select min frequency"
android:entries="@array/gpuminfreq"
android:entryValues="@array/gpuminfreq"
android:title="GPU Min frequency" />
<com.htc.preference.HtcSwitchPreference
android:key="applygpu"
android:summary="Apply selected GPU values"
android:title="Apply values" />
<com.htc.preference.HtcCheckBoxPreference
android:key="gpuboot"
android:summary="Apply settings on boot ( init.d)"
android:title="Set On Boot" />
</com.htc.preference.HtcPreferenceCategory>

</com.htc.preference.HtcPreferenceScreen>

android:key="xyz" => this will identify our item in sharedPreferences and in the activity ( we need it!)

Note: for HtcListPreference you can remove android:entries="@array/xzy" and android:entryValues="@array/xyz" if you want to fill the list programmatically ( EX: with a file content) ( we will see this later).

the Activity:

OnCreate:

public class yourPrefsClass extends HtcPreferenceActivity implements OnPreferenceChangeListener, OnPreferenceClickListener {

public SharedPreferences prefs;
public HtcListPreference maxgpu;
public HtcListPreference mingpu;
public HtcCheckBoxPreference gpuboot;
public String maxValue, minValue, boot;
public HtcTwoStatePreference applygpu;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
[B]/*
* Set the layout
*/[/B]
addPreferencesFromResource(R.xml.preftwo);
[B]*/
* SharedPreferences to retrieve and store values ( booleans , strings ... )
*/[/B]
prefs = PreferenceManager.getDefaultSharedPreferences(layTwoPrefs.this);

[B] */
* find preferenceViews!
* Note: i've declared these as public, so i don't need to recall the class
*/[/B]

maxgpu = (HtcListPreference) findPreference("maxgpu");
maxgpu.setOnPreferenceChangeListener(this);
mingpu = (HtcListPreference) findPreference("mingpu");
mingpu.setOnPreferenceChangeListener(this);
gpuboot = (HtcCheckBoxPreference) findPreference("gpuboot");
gpuboot.setOnPreferenceClickListener(this);
applygpu = (HtcTwoStatePreference) findPreference("applygpu");
applygpu.setOnPreferenceClickListener(this);

}

for HtcListPreference you need to use OnPreferenceChangeListener

now, lets' take a look at the HtcSwitchPreference:

in the XML file we have <com.htc.preference.HtcSwitchPreference > but in the activity i've wrote HtcTwoStatePreference. why?

Because HtcTwoStatePreference allow us to get the boolean true or false when we toggle the button!! ( and it's the parent class of HtcSwitchPreference)

for HtcTwoStatePreference you MUST use OnPreferenceClickListener instead of OnPreferenceChangeListener, this because, if we use OnPreferenceChangeListener, we will not see the "toggle animation" when we click on the item and we can't retrieve the boolean state of the toggle!

Implemented methods:

onPreferenceChange

@Override
public boolean onPreferenceChange(HtcPreference pref, Object newvalue) {
// TODO Auto-generated method stub
[B] /*
* we can't use the case statement here, but we can get the "clicked" item using the IF statement
*/[/B]
if(pref == maxgpu) {
maxValue = (String)newvalue;
SharedPreferences.Editor editor = prefs.edit();
editor.putString("maxgpu", maxValue);
editor.commit();
maxgpu.setSummary(maxValue);

}
if(pref == mingpu) {
minValue = (String)newvalue;
SharedPreferences.Editor editor = prefs.edit();
editor.putString("mingpu", minValue);
editor.commit();
mingpu.setSummary(minValue);
}

return false;
}

maxValue = (String)newvalue; => newvalue is the object we click on the ListPreference and we can convert it to a simple String if we need to do something with that.

[B]SharedPreferences.Editor editor = prefs.edit();
editor.putString("maxgpu", maxValue);
editor.commit()[/B]

^^^

^^^=====> here i'm storing in sharedpreferences the value i've selected , so i can retrieve this when i want by calling:

[B]prefs.getString("maxgpu", "0");[/B]

( "0" is the default value if in SharedPreferences the field doesn't exists. you can change this in what you want)

onPreferenceClick:

@Override
public boolean onPreferenceClick(HtcPreference pref) {
// TODO Auto-generated method stub
[B]/*
* as above, get the item clicked
*/[/B]
if (pref == applygpu) {
[B] /*
* retrieve the state of the toggle ( true=ON / false=OFF)
*/[/B]
boolean value = applygpu.isChecked();
if(value == true) {
//Do something when the switch is toggled ON
}
if(value == false) {
//Do something when the switch is toggled OFF
}
}

For HtcCheckBoxPreference, you need to set the activity as the same as for HtcSwitchPreference , but in OnCreate, you MUST use

yourCheckBoxPreference= (HtcCheckBoxPreference) findPreference("yourCheckBoxPreference");
yourCheckBoxPreference.setOnPreferenceClickListener(this);

Now, if you need to fill your HtcListPreference Programmatically:

i will use the frequencies file as example for this list ( located in /sys/devices/system/cpu/cpu0/cpufreq/scaling_available_frequencies):

On your XML remove the fields android:entries="@array/xzy" and android:entryValues="@array/xyz"

in your activity, inside onCreate and after View finding, add this:

("s1" is set as public String)

File file = new File("/sys/devices/system/cpu/cpu0/cpufreq/scaling_available_frequencies");
FileInputStream fin = null;
try {
fin = new FileInputStream(file);
byte fileContent[] = new byte[(int)file.length()];
fin1.read(fileContent);
s1 = new String(fileContent);
}
catch (FileNotFoundException e1) {
System.out.println("File not found" + e1);
}
catch (IOException ioe1) {
System.out.println("Exception while reading file " + ioe1);
}
finally {
try {
if (fin != null) {
fin.close();
}
}
catch (IOException ioe1) {
System.out.println("Error while closing stream: " + ioe1);
}
}
String[] frequencies = s1.split(" ");
yourListPreference.setEntryValues(frequencies);
yourListPreference.setEntries(frequencies);

so, what are we doing here??

we are trying to get the content of a file and this will be stored in a String (s1).

The problem:

ListPreference can handle only String arrays, so we need to conver our String "s1" to a String-array ( String[] ) by slitting this where , in the strings, the are empty spaces (" ")

String[] frequencies = s1.split(" ");

The last thing we nedd to do is set this String-array ad the content of our ListPreference, so:

yourListPreference.setEntryValues(frequencies);
yourListPreference.setEntries(frequencies);

NOTE: you MUST set both entries!! ( if you set only one of these you will get an error/ NullPointerExeption)

easy! :D