Can I really use these setOnLongClickListener and setOnClickListener for same button? Because if I long click the button both longclick and normal click will be executed and I dont know why. Can I really do this? Please help me:)
readDbButton.setOnLongClickListener(
new View.OnLongClickListener() {
public boolean onLongClick(View view) {
//do something
return false;
}
}
);
readDbButton.setOnClickListener(
new View.OnClickListener()
{
public void onClick(View view)
{
//Do something else
}
});
setOnClickListener(this); means that you want to assign listener for your Button “on this instance” this instance represents OnClickListener and for this reason your class have to implement that interface. If you have more than one button click event, you can use switch case to identify which button is clicked.
setOnClickListener it is actually creating an Anonymous Inner Class which implements OnClickListener. And the method onClick must need to be declared since its an abstract method inside that interface class. Any code you write inside onClick will be executed when the button is pressed.
You need to put the setOnClickListener in one of the activity callbacks. In your onCreate() method, move the button there and then setOnClickListener() . Show activity on this post.
return TRUE in your onLongClick
method so that the event will be consumed.
readDbButton.setOnLongClickListener(
new View.OnLongClickListener() {
public boolean onLongClick(View view) {
//do something
return true;
}
}
);
Try this proper way to Implement this
public class MainActivity extends Activity {
private Button button;
private GestureDetector gestureDetector;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
gestureDetector = new GestureDetector(this, new MyGestureDetector());
button = (Button) findViewById(R.id.button);
button.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View arg0, MotionEvent ev) {
return gestureDetector.onTouchEvent(ev);
}
});
}
private class MyGestureDetector extends SimpleOnGestureListener {
@Override
public boolean onSingleTapConfirmed(MotionEvent event) {
Toast.makeText(MainActivity.this, "Single Tap", Toast.LENGTH_SHORT).show();
return true;
}
@Override
public void onLongPress(MotionEvent e) {
Toast.makeText(MainActivity.this, "Long Tap", Toast.LENGTH_SHORT).show();
}
}
}
I got the solution of your Question.Return true instead of false in LongPressed.Just see below:-
readDbButton.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
Toast.makeText(HomeSafeActivity.this, "Long preess", Toast.LENGTH_LONG).show();
return true;
}
});
That is, return true to indicate that you have handled the event and it should stop here; return false if you have not handled it and/or the event should continue to any other on-click listeners
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