Manager Preference with PreferenceActivity : PreferenceActivity « UI « Android






Manager Preference with PreferenceActivity

 

package app.test;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.res.Resources;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.TextView;

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

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

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

        prefs = PreferenceManager.getDefaultSharedPreferences(this);

      PreferenceManager.setDefaultValues(this, R.xml.main, false);

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

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

    @Override
    public void onActivityResult(int reqCode, int resCode, Intent data)
    {
      super.onActivityResult(reqCode, resCode, data);
      setOptionText();
    }
    
    private void setOptionText()
    {
      String option = prefs.getString("", null);
      String[] optionText = new String[]{};
      
        tv.setText(option + " (" +optionText[Integer.parseInt(option)] + ")");
    }
}

//main.xml

<?xml version="1.0" encoding="utf-8"?>
<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>


//my_menu.xml


<?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"
    />
</menu>


//xml/main.xml

<?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="match_parent"
    android:layout_height="match_parent"
    >
    
<TextView android:text="" android:id="@+id/text1" 
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    />

</LinearLayout>

   
  








Related examples in the same category

1.Using PreferenceActivity
2.PreferenceActivity demo