3

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>
  • Could you show your java code and XML layout as well? – Prudhvi Feb 26 '15 at 04:01
  • Why not use the Android Studio Preference Activity template to give you a head start? You can modify it to your liking. – Daniel Nugent Feb 26 '15 at 04:11
  • this might help you http://stackoverflow.com/questions/26509180/no-actionbar-in-preferenceactivity-after-upgrade-to-support-library-v21 – dlohani Feb 26 '15 at 04:14
  • Daniel, I don't see a specific Preference Activity in my template list when I create a new activity. Where is it? –  Feb 26 '15 at 04:20

1 Answers1

4

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

hidro
  • 12,333
  • 6
  • 53
  • 53
  • 1
    Thank you! This is working the way I want it to now. –  Feb 26 '15 at 04:26
  • In [Support Library Revision Archive, Revision 22.1.0](https://developer.android.com/topic/libraries/support-library/rev-archive.html#rev22-1-0), Changes for v7 appcompat library, it says: "Updated the `AppCompatActivity` as the base class for activities that use the support library action bar features. This class replaces the **deprecated** `ActionBarActivity`." – Eido95 Sep 04 '17 at 18:06