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.

JJD
  • 50,076
  • 60
  • 203
  • 339
Tomcat
  • 1,405
  • 3
  • 22
  • 37

3 Answers3

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://stackoverflow.com/a/16247111/582004)

Community
  • 1
  • 1
liucheia
  • 364
  • 6
  • 16
  • 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. – Tomcat Apr 29 '13 at 15:15
  • Do you already have a styles.xml? If so, add the theme defined in – liucheia Apr 29 '13 at 20:57
  • No I don't have styles.xml. – Tomcat Apr 30 '13 at 01:26
  • Android loads styles from a file in res/values that has . 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 – liucheia Apr 30 '13 at 23:35
  • 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. – Tomcat May 02 '13 at 00:55
  • 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? – liucheia May 02 '13 at 23:46
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>
Shubham Goel
  • 1,962
  • 17
  • 25
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

Community
  • 1
  • 1
span
  • 5,405
  • 9
  • 57
  • 115
  • @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. – Tomcat Dec 22 '12 at 10:42