Using PreferenceScreen
import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.DialogInterface.OnClickListener;
import android.os.Bundle;
import android.preference.CheckBoxPreference;
import android.preference.ListPreference;
import android.preference.Preference;
import android.preference.PreferenceActivity;
import android.preference.PreferenceCategory;
import android.preference.PreferenceScreen;
import android.preference.Preference.OnPreferenceClickListener;
public class Preferences extends PreferenceActivity{
private AlertDialog skinError;
@Override
protected void onStart() {
super.onStart();
PreferenceScreen root = getPreferenceManager().createPreferenceScreen(this);
// List preference
ListPreference listPref = new ListPreference(this);
File f = new File("/sdcard/ADRINK");
final List<String> skins = new ArrayList<String>();
skins.add("Default");
if(f.exists()){
skins.addAll(Arrays.asList(f.list(null)));
}
PreferenceCategory dialogBasedPrefCat = new PreferenceCategory(this);
dialogBasedPrefCat.setTitle("aDrink Settings");
dialogBasedPrefCat.setOnPreferenceClickListener(new OnPreferenceClickListener(){
@Override
public boolean onPreferenceClick(Preference preference) {
if("skin".equalsIgnoreCase(preference.getKey()) && skins.size() < 1){
skinError.show();
}
return false;
}
});
root.addPreference(dialogBasedPrefCat);
String[] names = new String[skins.size()];
skins.toArray(names);
listPref.setEntries(names);
listPref.setEntryValues(names);
listPref.setDialogTitle("Select a skin");
listPref.setKey("skin");
listPref.setTitle("Select a different texture skin");
listPref.setSummary("You must download and put additional skins in your sdcard.");
listPref.setDefaultValue("Default");
// Toggle preference
CheckBoxPreference gamePref = new CheckBoxPreference(this);
gamePref.setKey("game");
gamePref.setTitle("Enable drunk test game");
gamePref.setSummary("Uncheck to disable the drunk test math game.");
gamePref.setDefaultValue(true);
dialogBasedPrefCat.addPreference(gamePref);
dialogBasedPrefCat.addPreference(listPref);
setPreferenceScreen(root);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
skinError = new AlertDialog.Builder(this)
.setCancelable(false)
.setTitle("No additional skins can be found, do you want to go to the download site?")
.setPositiveButton("Ok", new OnClickListener(){
@Override
public void onClick(DialogInterface dialog, int which) {
Intent i = new Intent("http://www.google.com");
startActivity(i);
finish();
}
})
.create();
}
}
Related examples in the same category