Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding settings to android app

Tags:

android

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?

like image 330
batoutofhell Avatar asked Feb 28 '13 02:02

batoutofhell


2 Answers

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);
    }
like image 58
Justin Vartanian Avatar answered Nov 09 '22 11:11

Justin Vartanian


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);
    }
like image 34
Dut A. Avatar answered Nov 09 '22 11:11

Dut A.