0

I have an android app with a UserSettingsActivity derived from PreferenceActivity. When I open this activity, no menu bar is shown.

In order to have the 'bar' or 'action bar' on top of the settings screen, I searched for it in google on found this link and this link. Because the first answer seems outdated, I tried the suggestion in the second link which does not work. Therefore I am posting this question again.. Here are the pieces of code I am using:

preferences.xml ( I added just the android.support.v7.widget.Toolbar part):

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

<android.support.v7.widget.Toolbar
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/toolbar"
    app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:minHeight="?attr/actionBarSize"
    app:navigationContentDescription="@string/abc_action_bar_up_description"
    android:background="?attr/colorPrimary"
    app:navigationIcon="?attr/homeAsUpIndicator"
    app:title="@string/action_settings"
    />

    <ListPreference
        android:title="@string/UpdateInterval"
        android:summary="@string/UpdateIntervalText"
        android:key="updateInterval"
        android:defaultValue="24"
        android:entries="@array/listArray"
        android:entryValues="@array/listValues" />

    <ListPreference
        android:title="@string/LimitSetting"
        android:summary="@string/LimitSettingText"
        android:key="limitSetting"
        android:entries="@array/limitArray"
        android:entryValues="@array/limitValues" />

    <Preference
        android:key="reset"
        android:title="Reset database"
        android:summary="This will remove every entry in the database"
         />
</PreferenceScreen>

and UserSettingsActivity.java (I added the onPostCreate part):

public class UserSettingActivity extends PreferenceActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getFragmentManager().beginTransaction().replace(android.R.id.content, new MyPreferenceFragment()).commit();

    }

    @Override
    protected void onPostCreate(Bundle savedInstanceState) {
        super.onPostCreate(savedInstanceState);

        LinearLayout root = (LinearLayout)findViewById(android.R.id.list).getParent().getParent().getParent();
        Toolbar bar = (Toolbar) LayoutInflater.from(this).inflate(R.xml.preferences, root, false);
        root.addView(bar, 0); // insert at top
        bar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    public class MyPreferenceFragment extends PreferenceFragment {
        @Override
        public void onCreate(final Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            addPreferencesFromResource(R.xml.preferences); // <<-- ERROR HERE
            // doing something here

        }
    }

}

However, I get the following error:

Caused by: java.lang.ClassCastException: android.support.v7.widget.Toolbar cannot be cast to android.preference.Preference

on the marked line. Without the added pieces in those two files the app did work, without the (action)-bar, however...

What seems to be the problem here?

Community
  • 1
  • 1
Alex
  • 41,580
  • 88
  • 260
  • 469
  • Possible duplicate of [No ActionBar in PreferenceActivity after upgrade to Support Library v21](http://stackoverflow.com/questions/26509180/no-actionbar-in-preferenceactivity-after-upgrade-to-support-library-v21), oh you already found my question. Did you also try [my own solution](http://stackoverflow.com/a/26995921/995926)? – rekire Mar 01 '16 at 18:54
  • Yes, I found your question. But what is the correct solution? Shall I try all different possibilities and combinations blindly? – Alex Mar 01 '16 at 18:56
  • Well I ended for that case with my own answer. I would suggest to use the newer preferences support library: https://plus.google.com/+AndroidDevelopers/posts/9kZ3SsXdT2T – rekire Mar 01 '16 at 18:57
  • Since I am absolute beginner, as it seems, I cannot use that email information... – Alex Mar 01 '16 at 19:30
  • here is my answer which might help to you. https://stackoverflow.com/a/49532356/8203967 – Rk215 Tech Mar 28 '18 at 10:45

2 Answers2

0

Try this.
Extend your activity with AppCompatActivity
Then add toolbar. Refer here: -http://coderzpassion.com/android-working-with-material-design/

Jagjit Singh
  • 1,909
  • 1
  • 14
  • 19
  • Please give a code example on how exactly change my code. I am a beginner and the information in the link you provided looks to me like astrophysical calculation would appear to you... – Alex Mar 02 '16 at 16:59
0

PreferenceActivity is now deprecated. Use PreferenceFragment inside any other activity, which can be extended from any parent activity other than PreferenceActivity

  • 1
    Without some code examples your answer is useless to me. I am an absolute beginner and I have no idea where to put `PreferenceFragment`, or how to change all of the other code to make it work as you suggest... – Alex Mar 02 '16 at 16:57