package app.test;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class HomeActivity extends Activity implements View.OnClickListener {
Button settingsButton;
TextView displayText;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
PreferenceManager.setDefaultValues(this, R.xml.settings, false);
displayText = (TextView)findViewById(R.id.display);
settingsButton = (Button)findViewById(R.id.settings);
settingsButton.setOnClickListener(this);
}
@Override
public void onResume() {
super.onResume();
SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(this);
StringBuilder builder = new StringBuilder();
builder.append("User Name: "+settings.getString("namePref", "")+"\n");
if(!settings.getBoolean("morePref", false)) {
builder.append("More Settings is DISABLED");
} else {
builder.append("More Settings is ENABLED\n");
builder.append("Favorite Color is "+settings.getString("colorPref", "")+"\n");
builder.append(settings.getBoolean("myGPS", false) ? "GPS is ENABLED\n" : "GPS is DISABLED\n");
}
displayText.setText(builder.toString());
}
@Override
public void onClick(View v) {
Intent intent = new Intent(this, SettingsActivity.class);
startActivity(intent);
}
}
//SettingsActivity.java
package app.test;
import android.os.Bundle;
import android.preference.PreferenceActivity;
public class SettingsActivity extends PreferenceActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.settings);
}
}
//arrays.xml
- Black
- Red
- Green
- BLK
- RED
- GRN
//main.xml
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Current Settings:"
/>
android:id="@+id/display"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="10dip"
/>
android:id="@+id/settings"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Change Settings"
/>
//settings.xml
android:key="namePref"
android:title="Name"
android:summary="Tell Us Your Name"
android:defaultValue="Apress"
/>
android:key="morePref"
android:title="Enable More Settings"
android:defaultValue="false"
/>
android:key="moreScreen"
android:title="More Settings"
android:dependency="morePref">
android:key="colorPref"
android:title="Favorite Color"
android:summary="Choose your favorite color"
android:entries="@array/color_names"
android:entryValues="@array/color_values"
android:defaultValue="GRN"
/>
android:title="Location Settings">
android:key="myGPS"
android:title="Use GPS Location"
android:summary="Use GPS to Find You"
android:defaultValue="true"
/>