Skip to content

Instantly share code, notes, and snippets.

@deakjahn
Last active August 29, 2015 14:11
Show Gist options
  • Save deakjahn/b12f10e3951cf595664c to your computer and use it in GitHub Desktop.
Save deakjahn/b12f10e3951cf595664c to your computer and use it in GitHub Desktop.
public class MenuPreference extends ListPreference {
private View anchor;
public MenuPreference(Context context) {
super(context);
}
public MenuPreference(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
protected View onCreateView(ViewGroup parent) {
return anchor = super.onCreateView(parent);
}
@Override
protected void showDialog(Bundle state) {
final PopupMenu popup = new PopupMenu(getContext(), anchor, Gravity.TOP);
final Menu menu = popup.getMenu();
for (int i = 0; i < getEntries().length; i++) {
MenuItem item = menu.add(1, i, Menu.NONE, getEntries()[i]);
item.setChecked(item.getTitle().equals(getEntry())); // 1
}
menu.setGroupCheckable(1, true, true); // 2
popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
popup.dismiss();
String value = getEntryValues()[item.getItemId()].toString();
if (callChangeListener(value))
setValue(value);
return true;
}
});
popup.show();
}
}
@ZacSweers
Copy link

Would a spinner work here instead? Specifically to achieve the effect of having the popup appear on top of the preference rather than below/above it

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