Add up button to PreferenceScreen
Asked Answered
D

3

7

I can't figure out how I would go about implementing an up button in a PreferenceScreen. An up button displays a caret in your action bar next to your app icon that allows you to navigate the app's hierarchy, more info here.

I have a Preference Fragment that displays when my main activity is opened and I can get the up button to display by adding this line " getActionBar().setDisplayHomeAsUpEnabled(true);":

 protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    getActionBar().setDisplayHomeAsUpEnabled(true);
    getFragmentManager().beginTransaction()
    .replace(android.R.id.content, new SettingsFragment())
    .commit();

This causes the up button to display in the Preference Fragment, but I only want to show the up button when one of my PreferenceScreens is opened, allowing navigation to the main PreferenceFragment.

My app is analogous to the main settings app. Only the child screens, like Location Access, that opens from the main Settings app has the up arrow.

enter image description here

Decaffeinate answered 9/8, 2013 at 20:22 Comment(2)
And what do you want to be shown when you are in the main preferences screen? It's not clear what you are asking.Eastereasterday
In the main preference screen, I do not want to be shown an up button. I do not need on because the Main Activity of my app is simply a Preference Fragment. There is nothing else to the app but this Preference Fragment. And within this Fragment, I have only one Preference that opens up another Preference Screen. It is on this Preference Screen only, where I want the up button to be displayed.Decaffeinate
E
5

If your complete application is a preferences screen, then you can make your main activity a PreferenceActivity and the sub-levels can be fragments. This way the 'up' functionality is going to be by default what you are looking for.

Eastereasterday answered 14/8, 2013 at 20:1 Comment(0)
D
14

From this question, I simply added these two code blocks to my Preference Fragment:

@Override
public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference    preference) {
super.onPreferenceTreeClick(preferenceScreen, preference);

// If the user has clicked on a preference screen, set up the action bar
if (preference instanceof PreferenceScreen) {
    initializeActionBar((PreferenceScreen) preference);
}

return false;
}

And this one:

 /** Sets up the action bar for an {@link PreferenceScreen} */
    public static void initializeActionBar(PreferenceScreen preferenceScreen) {
    final Dialog dialog = preferenceScreen.getDialog();

    if (dialog != null) {
        // Inialize the action bar
        dialog.getActionBar().setDisplayHomeAsUpEnabled(true);

        // Apply custom home button area click listener to close the PreferenceScreen because PreferenceScreens are dialogs which swallow
        // events instead of passing to the activity
        // Related Issue: https://code.google.com/p/android/issues/detail?id=4611
        View homeBtn = dialog.findViewById(android.R.id.home);

        if (homeBtn != null) {
            OnClickListener dismissDialogClickListener = new OnClickListener() {
                @Override
                public void onClick(View v) {
                    dialog.dismiss();
                }
            };

            // Prepare yourselves for some hacky programming
            ViewParent homeBtnContainer = homeBtn.getParent();

            // The home button is an ImageView inside a FrameLayout
            if (homeBtnContainer instanceof FrameLayout) {
                ViewGroup containerParent = (ViewGroup) homeBtnContainer.getParent();

                if (containerParent instanceof LinearLayout) {
                    // This view also contains the title text, set the whole view as clickable
                    ((LinearLayout) containerParent).setOnClickListener(dismissDialogClickListener);
                } else {
                    // Just set it on the home button
                    ((FrameLayout) homeBtnContainer).setOnClickListener(dismissDialogClickListener);
                }
            } else {
                // The 'If all else fails' default case
                homeBtn.setOnClickListener(dismissDialogClickListener);
            }
        }    
    }
}
Decaffeinate answered 12/8, 2013 at 22:30 Comment(1)
Interesting solution. Along with your comment it makes more sense.Eastereasterday
E
5

If your complete application is a preferences screen, then you can make your main activity a PreferenceActivity and the sub-levels can be fragments. This way the 'up' functionality is going to be by default what you are looking for.

Eastereasterday answered 14/8, 2013 at 20:1 Comment(0)
M
0

Have a look at this.

For the XML:

<Preference android:title="Acts like a button"
            android:key="button"
            android:summary="This will act like a button"/>

Then for the Java in your onCreate()

Preference button = (Preference)findPreference("button");

button.setOnPreferenceClickListener(
    new Preference.OnPreferenceClickListener() {
        @Override
        public boolean onPreferenceClick(Preference arg0) { 
            //code for what you want it to do   
            return true;
        }
});
Minutely answered 12/8, 2013 at 20:57 Comment(1)
I appreciate the help and I have looked at that. I do not think it is necessary to build my own layout, I just want the up button to appear on the preference screen.Decaffeinate

© 2022 - 2024 — McMap. All rights reserved.