Ch11 Listings

satya - Saturday, February 27, 2010 9:55:32 PM

Listing 11-1


package com.syh;

import android.os.Bundle;
import android.preference.PreferenceActivity;

public class FlightPreferenceActivity extends PreferenceActivity
{
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        addPreferencesFromResource(R.xml.flightoptions);
    }
}

satya - Saturday, February 27, 2010 9:55:58 PM

Listing 11-1 xml


<?xml version="1.0" encoding="utf-8"?>
<!-- This file is /res/xml/flightoptions.xml -->
<PreferenceScreen
        xmlns:android="http://schemas.android.com/apk/res/android"
    android:key="flight_option_preference"
    android:title="@string/prefTitle"
    android:summary="@string/prefSummary">

  <ListPreference
    android:key="@string/selected_flight_sort_option"
    android:title="@string/listTitle"
    android:summary="@string/listSummary"
    android:entries="@array/flight_sort_options"
    android:entryValues="@array/flight_sort_options_values"
    android:dialogTitle="@string/dialogTitle"
    android:defaultValue="@string/flight_sort_option_default_value" />

</PreferenceScreen>

satya - Saturday, February 27, 2010 9:56:18 PM

Listing 11-2


// This file is MainActivity.java
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.TextView;

public class MainActivity extends Activity {
    private TextView tv = null;
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        tv = (TextView)findViewById(R.id.text1);
        
        setOptionText();
    }
    
    @Override
    public boolean onCreateOptionsMenu(Menu menu) 
    {
       MenuInflater inflater = getMenuInflater();
       inflater.inflate(R.menu.mainmenu, menu);
       return true;
    }

    @Override
    public boolean onOptionsItemSelected (MenuItem item)
    {
       if (item.getItemId() == R.id.menu_prefs)
       {
           Intent intent = new Intent()
           	    	    .setClass(this, com.syh.FlightPreferenceActivity.class);
           this.startActivityForResult(intent, 0);
       }
       else if (item.getItemId() == R.id.menu_quit)
       {
            finish();
       }
       return true;
    }

    @Override
    public void onActivityResult(int reqCode, int resCode, Intent data)
    {
        super.onActivityResult(reqCode, resCode, data);
        setOptionText();
    }
    
    private void setOptionText()
    {
        SharedPreferences prefs = getSharedPreferences("com.syh_preferences", 0);
        String option = prefs.getString(
        	    	    this.getResources().getString(R.string.selected_flight_sort_option),
        	    	    this.getResources().getString(R.string.flight_sort_option_default_value));
        String[] optionText = this.getResources().getStringArray(R.array.flight_sort_options);
    	
        tv.setText("option value is " + option + " (" + 
        	    	    optionText[Integer.parseInt(option)] + ")");
    }
}

satya - Saturday, February 27, 2010 9:57:40 PM

Listing 11-2 a


<?xml version="1.0" encoding="utf-8"?>
<!-- This file is /res/values/arrays.xml -->
<resources>
<string-array name="flight_sort_options">
    <item>Total Cost</item>
    <item># of Stops</item>
    <item>Airline</item>
</string-array>
<string-array name="flight_sort_options_values">
    <item>0</item>
    <item>1</item>
    <item>2</item>
</string-array>
</resources>

satya - Saturday, February 27, 2010 9:58:14 PM

Listing 11-2 b


<?xml version="1.0" encoding="utf-8"?>
<!-- This file is /res/values/strings.xml -->
<resources>
    <string name="app_name">Preferences Demo</string>
    <string name="prefTitle">My Preferences</string>
    <string name="prefSummary">Set Flight Option Preferences</string>
    <string name="flight_sort_option_default_value">1</string>
    <string name="dialogTitle">Choose Flight Options</string>
    <string name="listSummary">Set Search Options</string>
    <string name="listTitle">Flight Options</string>
    <string name="selected_flight_sort_option">selected_flight_sort_option</string>
    <string name="menu_prefs_title">Settings</string>
    <string name="menu_quit_title">Quit</string>
</resources>

satya - Saturday, February 27, 2010 9:58:43 PM

Listing 11-2 c


<?xml version="1.0" encoding="utf-8"?>
<!-- This file is /res/menu/mainmenu.xml -->
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/menu_prefs"
    android:title="@string/menu_prefs_title"
    />
<item android:id="@+id/menu_quit"
    android:title="@string/menu_quit_title"
    />
</menu>

satya - Saturday, February 27, 2010 9:59:37 PM

Listing 11-2 d


<?xml version="1.0" encoding="utf-8"?>
<!-- This file is /res/layout/main.xml -->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
    
<TextView android:text="" android:id="@+id/text1" 
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    />

</LinearLayout>

satya - Saturday, February 27, 2010 10:00:04 PM

Listing 11-2 e


<?xml version="1.0" encoding="utf-8"?>
<!-- This file is AndroidManifest.xml -->
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.syh"
      android:versionCode="1"
      android:versionName="1.0">
    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".MainActivity"
                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <activity android:name=".FlightPreferenceActivity"
                  android:label="@string/prefTitle">
            <intent-filter>
                <action android:name="com.syh.intent.action.FlightPreferences" />
                <category android:name="android.intent.category.PREFERENCE" />
            </intent-filter>
        </activity>

    </application>
    <uses-sdk android:minSdkVersion="3" />

</manifest>

satya - Saturday, February 27, 2010 10:00:34 PM

Listing 11-3


<?xml version='1.0' encoding='utf-8' standalone='yes' ?>
<map>
    <string name="selected_flight_sort_option">1</string>
</map>

satya - Saturday, February 27, 2010 10:01:26 PM

Listing 11-4


// CheckBoxPreferenceActivity.java

import android.os.Bundle;
import android.preference.PreferenceActivity;

public class CheckBoxPreferenceActivity extends PreferenceActivity
{
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        addPreferencesFromResource(R.xml.chkbox);
    }
}

satya - Saturday, February 27, 2010 10:01:54 PM

Listing 11-4 xml


<?xml version="1.0" encoding="utf-8"?>
<!-- This file is /res/xml/chkbox.xml -->
    <PreferenceScreen
        xmlns:android="http://schemas.android.com/apk/res/android"
                android:key="flight_columns_pref"
                android:title="Flight Search Preferences"
                android:summary="Set Columns for Search Results">
        <CheckBoxPreference
                android:key="show_airline_column_pref"
                android:title="Airline"
                android:summary="Show Airline column" />
        <CheckBoxPreference
                android:key="show_departure_column_pref"
                android:title="Departure"
                android:summary="Show Departure column" />
        <CheckBoxPreference
                android:key="show_arrival_column_pref"
                android:title="Arrival"
                android:summary="Show Arrival column" />
         <CheckBoxPreference
                android:key="show_total_travel_time_column_pref"
                android:title="Total Travel Time"
                android:summary="Show Total Travel Time column" />
        <CheckBoxPreference
                android:key="show_price_column_pref"
                android:title="Price"
                android:summary="Show Price column" />

</PreferenceScreen>

satya - Saturday, February 27, 2010 10:02:23 PM

Listing 11-5


<?xml version='1.0' encoding='utf-8' standalone='yes' ?>
<map>
    <boolean name="show_total_travel_time_column_pref" value="false" />
    <boolean name="show_price_column_pref" value="true" />
    <boolean name="show_arrival_column_pref" value="false" />
    <boolean name="show_airline_column_pref" value="true" />
    <boolean name="show_departure_column_pref" value="false" />
</map>

satya - Saturday, February 27, 2010 10:02:50 PM

Listing 11-6


// EditTextPreferenceActivity.java

import android.os.Bundle;
import android.preference.PreferenceActivity;

public class EditTextPreferenceActivity extends PreferenceActivity{

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    
        addPreferencesFromResource(R.xml.packagepref);
    }

}

satya - Saturday, February 27, 2010 10:03:20 PM

Listing 11-6 xml


<?xml version="1.0" encoding="utf-8"?>
<!-- This file is /res/xml/packagepref.xml -->
<PreferenceScreen
        xmlns:android="http://schemas.android.com/apk/res/android"
                android:key="package_name_screen"
                android:title="Package Name"
                android:summary="Set package name">

        <EditTextPreference
                android:key="package_name_preference"
                android:title="Set Package Name"
                android:summary="Set the package name for generated code"
                android:dialogTitle="Package Name" />

</PreferenceScreen>

satya - Saturday, February 27, 2010 10:03:41 PM

Listing 11-7


// RingtonePreferenceActivity.java

import android.os.Bundle;
import android.preference.PreferenceActivity;

public class RingtonePreferenceActivity extends PreferenceActivity
{
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        addPreferencesFromResource(R.xml.ringtone);
    }
}

satya - Saturday, February 27, 2010 10:04:03 PM

Listing 11-7 xml


<?xml version="1.0" encoding="utf-8"?>
<!-- This file is /res/xml/ringtone.xml -->
<PreferenceScreen
        xmlns:android="http://schemas.android.com/apk/res/android"
                android:key="ringtone_option_preference"
                android:title="My Preferences"
                android:summary="Set Ring Tone Preferences">
    <RingtonePreference
        android:key="ring_tone_pref"
        android:title="Set Ringtone Preference"
        android:showSilent="true"
        android:ringtoneType="alarm"
        android:summary="Set Ringtone" />
</PreferenceScreen>

satya - Saturday, February 27, 2010 10:04:34 PM

Listing 11-8


<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen
        xmlns:android="http://schemas.android.com/apk/res/android"
                android:key="using_categories_in_root_screen"
                android:title="Categories"
                android:summary="Using Preference Categories">

    <PreferenceScreen
        xmlns:android="http://schemas.android.com/apk/res/android"
                android:key="meats_screen"
                android:title="Meats"
                android:summary="Preferences related to Meats">

        <CheckBoxPreference
                android:key="fish_selection_pref"
                android:title="Fish"
                android:summary="Fish is great for the healthy" />
        <CheckBoxPreference
                android:key="chicken_selection_pref"
                android:title="Chicken"
                android:summary="A common type of poultry" />
        <CheckBoxPreference
                android:key="lamb_selection_pref"
                android:title="Lamb"
                android:summary="Lamb is a young sheep" />

    </PreferenceScreen>
    <PreferenceScreen
        xmlns:android="http://schemas.android.com/apk/res/android"
                android:key="vegi_screen"
                android:title="Vegetables"
                android:summary="Preferences related to vegetable">
        <CheckBoxPreference
                android:key="tomato_selection_pref"
                android:title="Tomato "
                android:summary="It's actually a fruit" />
        <CheckBoxPreference
                android:key="potato_selection_pref"
                android:title="Potato"
                android:summary="My favorite vegetable" />

    </PreferenceScreen>

</PreferenceScreen>

satya - Saturday, February 27, 2010 10:05:00 PM

Listing 11-9


<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen
        xmlns:android="http://schemas.android.com/apk/res/android"
                android:key="using_categories_in_root_screen"
                android:title="Categories"
                android:summary="Using Preference Categories">

    <PreferenceCategory
        xmlns:android="http://schemas.android.com/apk/res/android"
                android:key="meats_category"
                android:title="Meats"
                android:summary="Preferences related to Meats">

        <CheckBoxPreference
                android:key="fish_selection_pref"
                android:title="Fish"
                android:summary="Fish is great for the healthy" />
        <CheckBoxPreference
                android:key="chicken_selection_pref"
                android:title="Chicken"
                android:summary="A common type of poultry" />
        <CheckBoxPreference
                android:key="lamb_selection_pref"
                android:title="Lamb"
                android:summary="Lamb is a young sheep" />

    </PreferenceCategory>
    <PreferenceCategory
        xmlns:android="http://schemas.android.com/apk/res/android"
                android:key="vegi_category"
                android:title="Vegetables"
                android:summary="Preferences related to vegetable">
        <CheckBoxPreference
                android:key="tomato_selection_pref"
                android:title="Tomato "
                android:summary="It's actually a fruit" />
        <CheckBoxPreference
                android:key="potato_selection_pref"
                android:title="Potato"
                android:summary="My favorite vegetable" />

    </PreferenceCategory>

</PreferenceScreen>