November 28th, 2013, 05:04 PM
-
Android ContextMenu Preference list - implement
I am trying to implement a context menu. I have a preference list that I can get the view and ID of each item in the pref list but, for the life of me
, I can not get my context menu to come up. When I longclick on a pref item I can Toast its ID and view. Where should I put the onCreateContextMenu and the onContextItemSelected to get my menu to come up? I commented out registerForContextMenu(view); because I was still able to Toast the view and ID without it. Any help would be great!
public class MainActivity extends PreferenceActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.prefs);
ListView listView = getListView();
listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
public boolean onItemLongClick(AdapterView parent, View view, int position, long id) {
ListView listView = (ListView) parent;
ListAdapter listAdapter = listView.getAdapter();
Object obj = listAdapter.getItem(position);
if (obj != null && obj instanceof View.OnLongClickListener) {
View.OnLongClickListener longListener = (View.OnLongClickListener) obj;
return longListener.onLongClick(view);
} else {
Preference pref = (Preference) obj;
// prefs
makeToast("pref toast - " + id + view);
//registerForContextMenu(view);
//onCreateContextMenu(R.menu.context_menu, view, ContextMenu.ContextMenuInfo menuInfo);
}
return true;
}
});
}
public boolean onLongClick(View v) {
// long click
//registerForContextMenu(v);
return true;
}
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
MenuInflater inflateLayout = getMenuInflater();
inflateLayout.inflate(R.menu.context_menu, menu);
super.onCreateContextMenu(menu, v, menuInfo);
}
public boolean onContextItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.item1:
makeToast("mmm 1 slice");
break;
case R.id.item2:
makeToast("mmm 2 slices");
break;
}
return super.onOptionsItemSelected(item);
}
public void makeToast(String message) {
Toast.makeText(this, message, Toast.LENGTH_SHORT).show();
}
}
Last edited by Stingalingaling; November 28th, 2013 at 05:11 PM.
Reason: better title