user4212315
user4212315

Reputation:

How to setup preference activity correctly?

I am working on getting my preferences screen setup, but I can't seem to get my preference activity working right.

I created a new blank activity called SettingsActivity that extends PreferenceActivity and removed setContentView(R.layout.activity_settings); from the onCreate method, and replaced it with addPreferencesFromResource(R.xml.preferences);.

In my main activity class, I setup a new intent to call my SettingsActivity class when I clicked the settings button that android creates by default.

I added a test item to my preferences.xml and ran it with this result. What happened to my Nav bar? Do I have to do something to add it? Android Studio shows that addPreferencesFromResource is deprecated. What am I supposed to do?

enter image description here

MainActivity method that calls SettingsActivity

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {

        Intent intent = new Intent(this, SettingsActivity.class);
        startActivity(intent);

        return true;
    }

    return super.onOptionsItemSelected(item);
}

SettingsActivity

public class SettingsActivity extends PreferenceActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    addPreferencesFromResource(R.xml.preferences);
}

Here is my preferences.xml

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen          
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"   
android:layout_height="match_parent">

<CheckBoxPreference
    android:key="test check box"
    android:title="Test"
    android:defaultValue="false"/>

</PreferenceScreen>

Upvotes: 3

Views: 6506

Answers (1)

hidro
hidro

Reputation: 12521

From API 11 you should switch to PreferenceFragment, which allows you to have an Activity that extends from ActionBarActivity (which gives you action bar).

public static class SettingsFragment extends PreferenceFragment {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // Load the preferences from an XML resource
        addPreferencesFromResource(R.xml.preferences);
    }
    ...
}

public class SettingsActivity extends ActionBarActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // Display the fragment as the main content.
        getFragmentManager().beginTransaction()
                .replace(android.R.id.content, new SettingsFragment())
                .commit();
    }
}

Source: http://developer.android.com/guide/topics/ui/settings.html#Fragment

Upvotes: 4

Related Questions