Last active
October 4, 2019 02:07
-
-
Save billthefarmer/de8ef17c648a727f0aa7ea9a941ce30d to your computer and use it in GitHub Desktop.
Android - Make a preference dialog home button work
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// PreferenceFragment... | |
// On preference tree click | |
@Override | |
public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, | |
Preference preference) | |
{ | |
boolean result = | |
super.onPreferenceTreeClick(preferenceScreen, preference); | |
if (preference instanceof PreferenceScreen) | |
{ | |
dialog = ((PreferenceScreen)preference).getDialog(); | |
ActionBar actionBar = dialog.getActionBar(); | |
actionBar.setDisplayHomeAsUpEnabled(true); | |
// Does not work in Material theme as button has no id | |
View view = dialog.findViewById(android.R.id.home); | |
if (view != null) | |
view.setOnClickListener(this); | |
} | |
return result; | |
} | |
// On click | |
public void onClick(View v) | |
{ | |
dialog.dismiss(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Does not work in Material theme as home button has no id.