Skip to content

Instantly share code, notes, and snippets.

@billthefarmer
Last active October 4, 2019 02:07
Show Gist options
  • Save billthefarmer/de8ef17c648a727f0aa7ea9a941ce30d to your computer and use it in GitHub Desktop.
Save billthefarmer/de8ef17c648a727f0aa7ea9a941ce30d to your computer and use it in GitHub Desktop.
Android - Make a preference dialog home button work
// 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();
}
@billthefarmer
Copy link
Author

Does not work in Material theme as home button has no id.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment