How to disable onClick when I click for LongClick?
It's a code from recyclerView, when I try long click I just see that normal click just spamming like hell.
holder.title.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(context, "To clear your recomendations, press for few seconds. ", Toast.LENGTH_SHORT).show();
}
});
holder.title.setOnLongClickListener(new View.OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
clearPreferences(R.string.preferences_reminder);
Toast.makeText(context, "Recomendations cleared.", Toast.LENGTH_SHORT).show();
return true;
}
});
In case if there is necessity to listen both onLongClick and onClick,
Here is another approach:
Example:
#Override
public boolean onLongClick(View view) {
//return value true to make sure only onLongClick is executed without triggering normal onClick
return true; // or false
}
return true means that the event is consumed. It is handled. No other click events will be notified.
return false means the event is not consumed. Any other click events will continue to receive notifications.
So to make sure both onClick and onLongClick are not triggered at the same time, return true from onLongClick event.
Add this line
holder.title.setOnClickListener(null);
Related
I have a button like a switch where I am trying to setClickable(false) after I click it so that only the first click will be handled
(additional clicks are ignored in the case of accidental double-clicks/multiple-clicks).
Here is a similar code:
Button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Button.setClickable(false);
//do other things
}
});
Then eventually, I have a code somewhere where I will reset the clickable to true, depending on a state variable, so I can switch-off.
The problem is when I click the button very quickly, it seems the succeeding clicks are still handled.
Is there a delay to the effects of setClickable()?
Also, I have read about using setEnabled(false) instead, but I cannot use it in my case. I need the button to still be enabled but not clickable.
Judging from your comment you probably need something like this.
Boolean SWITCH_ON = false;
Button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
if(!SWITCH_ON ){
SWITCH_ON = true;
}
}
});
Button.setOnLongClickListener(new View.OnLongClickListener() {
public boolean onLongClick(View v) {
if(SWITCH_ON ){
// do your task for long click here ...SWITCH_ON
}
return true;
}
});
You can use button.setEnabled(false); within your onClick method to disable the button.
Disabled buttons don't trigger the onClick method, and you can easily re-enable it with button.setEnabled(true); when needed.
You could add another variable named buttonEnabled or so and initialize it with true. Then in the onclick do this:
Button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Button.setClickable(false);
if(buttonEnabled) {
//do other things
}
buttonEnabled = false;
}
});
Note that you need to change the variable to if you want to reactivate it
I recently implemented a small RecyclerView setup and tried out the recyclerview-selection library. So far, the selection works fine but I also connected a click handler on the recyclerview items and now every time I long-press an item to activate the selection mode, it also counts as a simple tap and the activity changes (because that is what I programmed it to do on item tap). I managed to avoid this by adding a simple boolean to my recyclerview adapter, which is called when the selection mode starts:
void setIgnoreClicks(boolean b) {
this.ignoreClicks = b;
}
and then in the bind-function of my viewholder:
void bind(MyModelClass m) {
...
view.setOnClickListener(() -> {
if(!adapter.isIgnoreClicks()) {
...
}
});
}
now when the selection mode ends, the boolean is set back to false and the taps go through again.
The problem is that when only one item selected and you tap on it to deselt it, the selection mode is also exited - which is fine, except that that tap is now not ignored anymore and so, the activity changes. What I want basically is to ignore that last tap too. Is there some way to stop the event if the selection mode is still active?
Thanks
Ok, solved this myself. What I did was to add a touch listener to my recyclerview which sets the ignoreClick to true if the actionmode is active and no item is clicked:
modelList.addOnItemTouchListener(new RecyclerView.OnItemTouchListener() {
#Override
public boolean onInterceptTouchEvent(#NonNull RecyclerView rv, #NonNull MotionEvent e) {
if (e.getAction() != MotionEvent.ACTION_UP)
return false;
if(actionMode != null)
ignoreClick = rv.findChildViewUnder(e.getX(), e.getY()) != null; // ignore click if child is null (not clicked on a child, but the empty background of the recycler view)
return false;
}
...
});
and then in my item click handler:
private void showDetails(final Model model) {
if(ignoreClick)
ignoreClick = false; // the click is ignored, reset to false
else if (!selectionTracker.hasSelection()) {
final Intent intent = new Intent(MainActivity.this, ModelViewActivity.class);
intent.putExtra(Codes.DATA_MODEL, model);
startActivityForResult(intent, Codes.INTENT_MODEL_SHOW);
}
}
I have a button that when clicked it will increase my mediaplayer for 10s but I want to press and hold it will increase continuously until released.
btn10s.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mp.seekTo(mp.getCurrentPosition() + 10000);
}
});
Thank !
The method onClick works when you touch and release the button.
But when you want to do something in an event of touching the button,
you need the method called
onTouch (View v, MotionEvent event){}
When I click an Android button the Android's Software Keys are shown (hidden before) instead of firing the onClick() method for the actual button that I click.
I have two methods for showing or hiding the system UI:
1.
// Set up the user interaction to manually show or hide the system UI.
contentView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (TOGGLE_ON_CLICK) {
mSystemUiHider.toggle();
} else {
mSystemUiHider.show();
}
}
});
and 2:
/**
* Touch listener to use for in-layout UI controls to delay hiding the
* system UI. This is to prevent the jarring behavior of controls going away
* while interacting with activity UI.
*/
View.OnTouchListener mDelayHideTouchListener = new View.OnTouchListener() {
#Override
public boolean onTouch(View view, MotionEvent motionEvent) {
if (AUTO_HIDE) {
delayedHide(AUTO_HIDE_DELAY_MILLIS);
}
return false;
}
};
And I also have another method which a switch to get the onClick() event on buttons
public void onClick(View v) { switch() { } }
Is it possible to fix this behaviour and when I click the actual button will be fired and the System UI will be shown?
There is no workaround to overcome this behaviour until you have both touch listener and also a click listener inside the same view. Try using
View.SYSTEM_UI_FLAG_LOW_PROFILE|View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
instead. Add the below code before setcontentview() method.
View decorView = getWindow().getDecorView();
int uiOptions = View.SYSTEM_UI_FLAG_LOW_PROFILE|View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY;
decorView.setSystemUiVisibility(uiOptions);
I have problem with unwanted delay after click on toggleButton using own OnClickListener.
I make my listener by this advice on stackoverflow, like below:
public class ToggleButtonOnClickListener implements OnClickListener{
private String _name;
public ToggleButtonOnClickListener(String name) {
_name = name;
}
#Override
public void onClick(View v) {
Log.i("toggle button clicked",_name);
}
}
and using this:
toggle.setOnClickListener(new ToggleButtonOnClickListener(device.GetName()));
But it not fire onClick method after first click, but the next one.
And because I have group of toggleButtons is this very unhappy, when I click on first, and onClick method fire after click again or even after click to second (or any) from the group.
The OnCheckChangeListener behaves the same.
Please refer developer's example.
You can implement something like below:
public void onToggleClicked(View view) {
// Is the toggle on?
boolean on = ((ToggleButton) view).isChecked();
if (on) {
// Enable vibrate
} else {
// Disable vibrate
}
}
After looking for errors and testing other options, I found that delay caused not Listener, but the log statement.
So, the code above working well except for
Log.i("toggle button clicked",_name);
and an alternative without needs have own class(parametrs) using OnCheckedChangeListener is:
toggle.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener(){
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
Toast.makeText(buttonView.getContext(), "test", Toast.LENGTH_LONG).show();
}
});
I don't know why this Log do, but I used them only for debug, so problem solved!