0

I've been trying to get a Toolbar to show up in my PreferenceActivity following the answer from Gabor using AppCompatPreferenceActivity shown here: Creating a Preference Screen with support (v21) Toolbar . However I can not get it to work. my code:

Settings.java:

public class Settings extends AppCompatPreferenceActivity{


@Override
public void onBuildHeaders(List<Header> target) {
    loadHeadersFromResource(R.xml.preferences, target);

    setContentView(R.layout.settings_page);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    ActionBar bar = getSupportActionBar();
    bar.setHomeButtonEnabled(true);
    bar.setDisplayHomeAsUpEnabled(true);
    bar.setDisplayShowTitleEnabled(true);
    bar.setHomeAsUpIndicator(R.drawable.abc_ic_ab_back_mtrl_am_alpha);
    bar.setTitle("Test2");
}

@Override
protected boolean isValidFragment(String fragmentName) {
    return MyPreferenceFragment.class.getName().equals(fragmentName);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case android.R.id.home:
            onBackPressed();
            break;
    }
    return super.onOptionsItemSelected(item);
}

public static class MyPreferenceFragment extends PreferenceFragment
{
    @Override
    public void onCreate(final Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        addPreferencesFromResource(R.xml.preferences);
        Preference removeMedalsButton = findPreference("removeMedals");
        removeMedalsButton.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
            @Override
            public boolean onPreferenceClick(Preference preference) {
                Toast.makeText(getActivity(),"Medailles verwijderd",Toast.LENGTH_SHORT).show();
                removeMedals(getActivity());
                return true;
            }
        });

        Preference removeWorkoutsButton = findPreference("removeWorkouts");
        removeWorkoutsButton.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
            @Override
            public boolean onPreferenceClick(Preference preference) {
                Toast.makeText(getActivity(),"Workouts verwijderd",Toast.LENGTH_SHORT).show();
                removeWorkouts();
                return true;
            }
        });

        Preference removeProfileButton = findPreference("removeProfile");
        removeProfileButton.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
            @Override
            public boolean onPreferenceClick(Preference preference) {
                Toast.makeText(getActivity(),"Profiel verwijderd",Toast.LENGTH_SHORT).show();

                removeProfile();
                return true;
            }
        });
    }

  }

  public static void removeMedals(Context context){
    AchievementTracker.removeMedals(context);
  }


  public static void removeWorkouts(){
  }


  public static void removeProfile(){
  }
}

settings_page.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:layout_margin="0dp"
  android:orientation="vertical"
  android:padding="0dp">

  <android.support.v7.widget.Toolbar
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    android:background="?attr/colorPrimary"
    android:elevation="4dp"
    android:theme="@style/ToolbarTheme"/>

<ListView
    android:id="@id/android:list"
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>
</LinearLayout>

preference.xml:

<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<CheckBoxPreference
    android:title="@string/reminder_preference"
    android:defaultValue="false"
    android:summary="@string/reminder_preference_summary"
    android:key="reminderPreference" />
<Preference android:title="@string/remove_medals"
    android:key="removeMedals"
    android:summary="@string/remove_medals_summary"/>

<Preference android:title="@string/remove_profile"
    android:key="removeProfile"
    android:summary="@string/remove_profile_summary"/>

<Preference android:title="@string/remove_workouts"
    android:key="removeWorkouts"
    android:summary="@string/remove_workouts_summary"/>
</PreferenceScreen>

The error i'm getting is:

XML document must start with <preference-headers> tag; foundPreferenceScreen at Binary XML file line #2

I think it's because i'm calling setContentView(R.Layout.settings_page) but that's how i'm supposed to do this according to Gabor's answer.

Community
  • 1
  • 1
Mischa
  • 2,069
  • 4
  • 23
  • 32

0 Answers0