The most common way to handle a button click is:
button.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
//exeute some code here
}
});
So I click the button, hold for a while and it executes the code when my finger actually leaves the button. When I hold it, I can't click it anymore (I mean while clicking - nothing happens).
1) Is it possible to execute the code when I my finger touches the button (not when it leaves)?
2) Is it possible to execute the code when I hold the button and then my second finger touches it (I want to use multitouch feature)?
As #Raghunandan commented, use setOnTouchListener as follow..
button.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction()==MotionEvent.ACTION_DOWN) {
Log.e(TAG,"Down");
return true;
}
if (event.getAction()==MotionEvent.ACTION_MOVE){
Log.e(TAG,"Move");
return true;
}
if (event.getAction()==MotionEvent.ACTION_UP){
Log.e(TAG,"Up");
return true;
}
return false;
}
});
Related
I would like to create a RecyclerView in which a user can long click an image and preview the full sized image until they release the long click.
I have it mostly working but the issue I am having is that if I begin the long click, then drag my finger (while still holding the click down), the listener no longer waits for my ACTION_UP event and the preview image never goes away. Is there a way to sort of ignore the dragging/scrolling so that my preview image view goes away when I release the long click?
This is what I have for event listeners:
/* Long press will trigger hover previewer */
holder.thumbnailImageView.setOnLongClickListener(new View.OnLongClickListener() {
#Override
public boolean onLongClick(View pView) {
holder.thumbnailImageView.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View pView, MotionEvent pEvent) {
pView.onTouchEvent(pEvent);
// We're only interested in when the button is released.
if (pEvent.getAction() == MotionEvent.ACTION_UP) {
if (isImageViewPressed) {
// Do something when the button is released.
isImageViewPressed = false;
mHoverView.setVisibility(View.GONE);
}
}
return false;
}
});
isImageViewPressed = true;
GlideApp.load(item.getUrl()).into(mHoverView);
mHoverView.setVisibility(View.VISIBLE);
return true;
}
});
/* Long press will trigger hover previewer */
holder.thumbnailImageView.setOnLongClickListener(new View.OnLongClickListener() {
#Override
public boolean onLongClick(View pView) {
isImageViewPressed = true;
GlideApp.load(item.getUrl()).into(mHoverView);
mHoverView.setVisibility(View.VISIBLE);
return true;
}
});
holder.thumbnailImageView.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View pView, MotionEvent pEvent) {
pView.onTouchEvent(pEvent);
// We're only interested in when the button is released.
if (isImageViewPressed && pEvent.getAction() == MotionEvent.ACTION_UP) {
// Do something when the button is released.
isImageViewPressed = false;
mHoverView.setVisibility(View.GONE);
}
return true;
}
});
This will work and your code is not working as longClickListener does't get's the event of action down(and neither of action down) and what you are doing currently is setting the listener for touch which never got Action_DOWN i.e by default the View's ontouch() return false on Action_Down so u have to override and return true before action down is called so that it get's action move and action up etc.
I have yet to find the solution to this problem. I'm trying to make a game. I want to make an image move to the left, right, up or down when a button is clicked. Please help me as I have struggled with this problem for two days. Please show and explain to me how to move an ImageVıew as explained above.
Thanks in advance.
bt = (Button) findViewById(R.id.button2);
bt.setOnLongClickListener(new View.OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
playerY+=20;`
player.setY(playerY);
return true;
}
});
Try this
UpButton.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
switch(event.getAction()) {
case MotionEvent.ACTION_DOWN:
// Do something
return true;
case MotionEvent.ACTION_UP:
// No longer down
return true;
}
return false;
}
});
Action Down means the button is clicked and action up gets triggered when the finger is no longer on the button
How can I detect clicked, pressed and released states of a Button. I want to perform different functions on these states. On click I want to call function1, on press I want to call function2 and on receive I want to call function3.
We can detect click state using View.OnClickListener. We can detect Pressed and Released states of a Button using View.OnTouchListener and handling ACTION_DOWN and ACTION_UP. I am able to detect these states individually, however, not together.
Below is code for OnCLickListener.
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
System.out.println(" clicked ");
}
});
Below is code for OnTouchListener.
button.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
switch(event.getAction()) {
case MotionEvent.ACTION_DOWN:
System.out.println(" pressed ");
return true;
case MotionEvent.ACTION_UP:
System.out.println(" released ");
return true;
}
return false;
}
});
When I set click and touch listeners on a Button, Click event never gets called. Instead I receive pressed and released state.
How can I handle these three states together?
EDIT:
I added the OnClickListener and OnTouchListener code I have used.
Easy since Button is a View:
button.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
// Pressed
} else if (event.getAction() == MotionEvent.ACTION_UP) {
// Released
}
return true;
}
});
Change the return true; inside case MotionEvent.ACTION_DOWN: and case MotionEvent.ACTION_UP:to return false; or break;
See this link.
You can handle click manually in MotionEvent.ACTION_UP event by
button.performClick();
clicked event include pressed and released state,if you want to fire clicked event,put method after ACTION_UP
I want to keep a button displayed pressed. I want to use another way instead of onPressed() in onTouch(). I add setPressed(), setSelected() but not worked. when i add these methods in onTouch() the program is good but my animation is very slow.
Can i use these method in onClick() method but works in this?
Please explain for me
Instead of using the onClick-event which returns the button to the non-pressed state, you can set the pressed state in the onTouch-event:
yourbutton.setOnTouchListener(new OnTouchListener()
{
#Override
public boolean onTouch(View v, MotionEvent event)
{
if(event.getAction() == MotionEvent.ACTION_DOWN)
{
((Button) v).setPressed(true);
//TODO: Add the code of your onClick-event here
}
return true;//Return true, so there will be no onClick-event
}
});
If this affects any animation, you should look for the problems there.
This piece of code makes the button work as a toggle button. It sets the pressed property to its opposite.
If the button is pressed, it's set as unpressed.
If the button is unpressed, it's set as pressed.
#Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
Button b = ((Button) v);
b.setPressed(!b.isPressed());//if pressed, unpress; if unpressed, press
}
return true;
}
What you are describing seems to be a toggle button. Maybe you should check this out, instead of doing a hackish solution :
Toggle Buttons
Extend ToggleButton or:
Button button = (Button) view.findViewById(R.id.button1);
button.setTag(false);
button.setOnTouchListener(new OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_UP)
if ((boolean) v.getTag()) {
v.setPressed(false);
v.setTag(false);
return false;
} else {
v.setPressed(true);
v.setTag(true);
return true;
}
return false;
}
});
I have a View and I need to detect a single tap on a LinearLayout.
I don't care about movements, All I want to detect is a single tap.
Basically a touch detection just like the touch detection on buttons. How can I achieve this?
myView.setOnTouchListener(new OnTouchListener(){
#Override
public boolean onTouch(View v, MotionEvent event) {
// This gets called so many times on every movement
return true;
}
});
I'm pretty sure, this would work aryaxt:
myView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// myView tapped //
}
});