Using PreferenceScreen : Screen « Hardware « Android






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

1.Multiscreen size
2.Nested PreferenceScreen
3.Get the size of Default Display Screen
4.Screen Orientation
5.Rows have different number of columns and content doesn't fit on screen: column 4 of row 2 shrinks all of the other columns
6.Demonstrates the Tab scrolling when too many tabs are displayed to fit in the screen.
7.Splash Screen
8.Get the optimal preview size for the given screen size.
9.Methods for converting between the physics world coordinates and the screen coordinates.
10.Get screen Orientation
11.add Home Screen Shortcut
12.Return the supported picture size that best fits on the device screen.
13.Screen Short
14.Set full screen mode