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
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">
android:key="show_airline_column_pref"
android:title="Airline"
android:summary="Show Airline column" />
android:key="show_departure_column_pref"
android:title="Departure"
android:summary="Show Departure column" />
android:key="show_arrival_column_pref"
android:title="Arrival"
android:summary="Show Arrival column" />
android:key="show_total_travel_time_column_pref"
android:title="Total Travel Time"
android:summary="Show Total Travel Time column" />
android:key="show_price_column_pref"
android:title="Price"
android:summary="Show Price column" />
//my_menu.xml
//xml/main.xml
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>