This is definitely a noob question. I've followed the instructions here http://developer.android.com/guide/topics/ui/settings.html#Activity and when I click on settings, nothing happens.
Here's what I have in MainActivity.java:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
PreferenceManager.setDefaultValues(this, R.xml.preferences, false);
}
Then I have a new java file called PrefsActivity.java
public class PrefsActivity extends PreferenceActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.preferences);
} }
Then I have res/xml/preferences.xml
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<CheckBoxPreference
android:key="face_up"
android:title="@string/face_up"
android:summary="@string/face_up_desc"
android:defaultValue="false" />
</PreferenceScreen>
I am trying to make it compatible with minsdk 7 if possible. What am I missing?
You need to open your activity when you click on your settings button. If your using an action bar, use something like this:
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_settings:
Intent intent = new Intent(this, SettingsActivity.class);
startActivity(intent);
return true;
}
return super.onOptionsItemSelected(item);
}
In your main Activity
// Ensure the right menu is setup
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
// Start your settings activity when a menu item is selected
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == R.id.action_settings) {
Intent settingsIntent = new Intent(this, PrefsActivity.class);
startActivity(settingsIntent);
}
return super.onOptionsItemSelected(item);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With