Action Button Does Not Work Button with Preferences Sub-Screen

I found a workaround to actually activate the ActionBar button for the home nested PreferenceScreen ... however it does not call OnOptionsItemSelected in my PreferenceActivity. Does anyone know a way to use the home button on a nested PreferenceScreen?

Modification of post 35 here:

http://code.google.com/p/android/issues/detail?id=4611

@Override public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference) { super.onPreferenceTreeClick(preferenceScreen, preference); if (preference!=null) if (preference instanceof PreferenceScreen) if (((PreferenceScreen)preference).getDialog()!=null) ((PreferenceScreen)preference).getDialog().getActionBar().setHomeButtonEnabled(true); return false; } 
+25
java android android-actionbar sharedpreferences preferenceactivity
May 4 '13 at
source share
1 answer

I have had this problem recently, and that is how I solved it. First, to access the PreferenceScreen, I use the same method that you mentioned above.

 @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; } 

From here I looked at what PreferenceScreen is , and I was saddened to learn that this was just a shell of dialogue. Moving forward, I then set the display options for the action bar and try to find the home button. Unfortunately, this was not so simple, but by viewing the hierarchy, I was able to access by finding the house icon and then its parent views. When we have access to the containing LinearLayout, we can attach the onClickListener, where we cancel the PreferenceScreen dialog, which calls the PreferenceScreen onDismissListener and returns us to the previous screen.

 /** 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); } } } } 
+46
May 28 '13 at 20:05
source share



All Articles