I have an imageview where I need to have both long presses (for a context menu) and regular presses working. I can get one or the other working, but not both. What did I miss? The code below only works for regular presses. As soon as I touch the screen, it starts executing the onTouch code.
ImageView image = (ImageView)findViewById(R.id.visible_image);
image.setLongClickable(true);
image.setOnTouchListener(new OnTouchListener() {
public boolean onTouch(View v, MotionEvent ev) {
switch (ev.getAction()) {
case MotionEvent.ACTION_DOWN:
// disable the screen taps for 500ms
DecodeActionDownEvent(v, ev, bm2);
case MotionEvent.ACTION_MOVE:
case MotionEvent.ACTION_UP:
}
return true;
}
});
registerForContextMenu(image);
...
@Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
menu.setHeaderTitle("Edit");
ArrayList<String> menuItems = new ArrayList<String>();
menuItems.add("Edit page settings");
menuItems.add("Edit page objects");
menuItems.add("Edit this object");
for (int i = 0; i<menuItems.size(); i++) {
menu.add(Menu.NONE, i, i, menuItems.get(i));
}
}
You can make a view clickable, as a button, by adding the android:onClick attribute in the XML layout. For example, you can make an image act like a button by adding android:onClick to the ImageView .
We will be building a simple application in which we will be displaying an ImageView and when we click on that ImageView we will get into a new activity or simply we can say that we are going to use ImageView as a button to switch between different activities.
ImageView class is used to display any kind of image resource in the android application either it can be android. graphics. Bitmap or android. graphics.
You can try to do it this way:
ImageView imageView = (ImageView) findViewById(R.id.ImageView);
imageView.setOnLongClickListener(new OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
//your stuff
return true;
}
});
imageView.setOnClickListener(new OnClickListener() {
@Override
public boolean onClick(View v) {
//your stuff
return true;
}
});
After much looking and experimenting this is the best solution I have found to achieve this type of behavior:
//Define these variables at the beginning of your Activity or Fragment:
private long then;
private int longClickDuration = 5000; //for long click to trigger after 5 seconds
...
ImageView imageView = (ImageView) findViewById(R.id.visible_image);
imageView.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
then = (long) System.currentTimeMillis();
} else if (event.getAction() == MotionEvent.ACTION_UP) {
if ((System.currentTimeMillis() - then) > longClickDuration) {
/* Implement long click behavior here */
System.out.println("Long Click has happened!");
return false;
} else {
/* Implement short click behavior here or do nothing */
System.out.println("Short Click has happened...");
return true;
}
}
return true;
}
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With