Show Context Menu With Short Click not Long Click
I think you should use popup menu instead of context menu. Check documentation https://developer.android.com/guide/topics/ui/menus, or do it like this:
private void showMenu(View v){
PopupMenu popup = new PopupMenu(context, v);
MenuInflater inflater = popup.getMenuInflater();
inflater.inflate(R.menu.your_menu, popup.getMenu());
popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_item1:
//your code
return true;
case R.id.menu_item2:
//your code
return true;
case R.id.menu_item3:
//your code
return true;
default:
return false;
}
}
});
popup.show();
}
Call this method in onClickListener of your button and pass your button.
Without adding any OnClickListener in the code,you can do it only in the xml.Just go to your ImageView and add:
android:onClick="openContextMenu"
Here is an example.
<ImageView
android:id="@+id/btnRutas"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="openContextMenu" />
The only way I can think of is to use an onClickListener() as part of the activity:
public class MyActivity extends Activity implements OnClickListener{
protected void onCreate(Bundle bundle) {
//Usual Activity Stuff
View v = (View)findViewById(R.id.view);
v.setOnClickListener(this);
}
public void onClick(View v) {
super.onClick(v);
this.openContextMenu(v);
}
}
Instead of creating a new View specifically for this, I guess you would use whatever View you wanted this to apply to. I hope this is what you were going for and that this helps.