setHomeButtonEnabled on PreferenceActivity and nested preference
F

3

6

I have preference screen extended PreferenceActivity. For targeting OS 4.0.3, I wanted to add < icon on action bar so I did this in onCreate().

ActionBar actionBar = getActionBar();
actionBar.setHomeButtonEnabled(true);
actionBar.setDisplayHomeAsUpEnabled(true);

It worked. < was added to left of app icon. But when I tap the item which goes into the next level (more detail screen), the < won't be displayed. Returning to the top level, the < appears again.

I have never thought about a mechanism of nested preference since smart the PreferenceActivity hides it. Now my question is, why won't PreferenceActivity display the < on nested preference?

I don't want to argue that I don't need to add < to the preference screen. (Even some of Google's app add, some don't, so I think there is no solid rule for this.)

If there is a simple solution for this, I want to solve this issue.

Floccule answered 15/8, 2012 at 15:1 Comment(0)
S
1

Instead of dynamically adding this, you should add the arrow by writing a custom ActionBar style to be used with your application theme. (Basically, see https://mcmap.net/q/353917/-show-up-back-button-on-android-nested-preferencescreen)

Shier answered 27/4, 2013 at 0:45 Comment(6)
I could not make your solution work. I got "No resource found that matches the given name (at 'theme' with value '@android:style/MyTheme').". I googled to solve this but failed.Floccule
Do you already have a styles.xml? If so, add the theme defined in <style> to that file.Shier
No I don't have styles.xml.Floccule
Android loads styles from a file in res/values that has <resources>. It shouldn't matter what you name the file (themes.xml or styles.xml), but you need a styles.xml in res/values/ where you define your different <style>'s, so Android can load them.Shier
I renamed themes.xml to styles.xml and it worked. But it is different from what I wanted to do. Anyway, thank you for your help. I accept your answer.Floccule
Perhaps I misunderstood your question - what is it that you wanted to do that was outside of adding the "up" arrow to the second level preference screen?Shier
F
1

This is my working code for Home button on Settings activity

    @Override   
    public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    LinearLayout root = (LinearLayout)findViewById(android.R.id.list).getParent().getParent().getParent();
    Toolbar bar = (Toolbar) LayoutInflater.from(this).inflate(R.layout.settings_toolbar, root, false);
    root.addView(bar, 0); // insert at top
    bar.setTitle("Settings");
    bar.setTitleTextColor(Color.WHITE);

    bar.setNavigationOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            finish();
        }
    });
    addPreferencesFromResource(R.layout.settings);

}

This is the settings_toolbar.xml

    <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"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="@color/colorPrimary"
android:elevation="2dp"
android:fitsSystemWindows="true"
android:minHeight="?attr/actionBarSize"
app:navigationIcon="@drawable/gobackleftarrow"
app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

</android.support.v7.widget.Toolbar>
Fortyniner answered 19/9, 2016 at 10:36 Comment(0)
V
0

You need to enable the home button and actionbar for each Activity that you wish to show the UP navigation. You must also set the parent activity in the manifest.

See this question for the gory details: Up indicator does not show up

Vermeil answered 20/12, 2012 at 8:29 Comment(1)
@sapn, Thank you for your answer. But I could not solve my issue. I could not find out how to add '<' to 2nd level screen of PreferenceActivity.Floccule

© 2022 - 2024 — McMap. All rights reserved.