Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Action Bar Home Button not functional with nested PreferenceScreen

Tags:

I found a workaround to actually enable the ActionBar home button on the nested PreferenceScreen... however it doesn't call OnOptionsItemSelected in my PreferenceActivity. Anyone know a way to actually 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;     } 
like image 339
jsstp24n5 Avatar asked May 04 '13 13:05

jsstp24n5


Video Answer


1 Answers

I had this problem recently and this is how I solved it. Firstly to access the PreferenceScreen I use the exact same method 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 into what a PreferenceScreen is, and I was saddened to find out it is just wrapper of a Dialog. Moving forward, I then set the actionbar display options and attempt find the home button area. This unfortunately wasn't too easy to get, but with the help of the hierarchy viewer I managed to gain access by finding the home icon and then its parent views. Once we have access to the containing LinearLayout, we can attach an onClickListener where we dismiss the PreferenceScreen's dialog, which calls PreferenceScreen's 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);             }         }         } } 
like image 102
jimmithy Avatar answered Sep 24 '22 14:09

jimmithy