This is so strange, but if you put an onClickListener on a TextView (or non-editable EditText) which has android:textIsSelectable="true"
- it needs not one tap, but two.
I checked it on 3 phones and all of them perform onClick only after second tap.
Of course, if you make focusable="false"
or android:textIsSelectable="false"
it works from the 1st tap, but text selection doesn't work.
Please, help me with that issue
saving a last click time when clicking will prevent this problem. i.e. This is the only solution that actually prevents a double click.
The standard way to avoid multiple clicks is to save the last clicked time and avoid the other button clicks within 1 second (or any time span). Example: // Make your activity class to implement View. OnClickListener public class MenuPricipalScreen extends Activity implements View.
Sometimes user clicks button too fast or double time, if button performs some kind of network operation, it'll call the function multiple times. To prevent double click, you can record the last time button clicked, and compare it to threshold of desired time.
Use onTouchListener
to detect clicks and redirect them to the container view:
textView.setOnTouchListener { _, event ->
if (event.action == 1 && !textView.hasSelection()) {
containerView.callOnClick()
}
false
}
This will keep the ability to select and unselect text without calling onClick
event.
Set in XML to your TextView
android:textIsSelectable="true"
After that set onTouchListener to your TextView and in them do this:
if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) view.requestFocus();
It's set focus for every tap on TextView. After all set onClickListener to your TextView.
I have the same problem with a Holder for my custom RecyclerView.Adapter. So, i cut it for you if you need:
class RollHolder extends RecyclerView.ViewHolder implements View.OnClickListener, View.OnTouchListener {
private TextView textView;
RollHolder(View itemView) {
super(itemView);
textView = (TextView) itemView.findViewById(R.id.text_view);
textView.setOnClickListener(this);
textView.setOnTouchListener(this);
}
@Override //Слушатель нажатия
public void onClick(View view) {
switch (view.getId()){
case R.id.text_view:
//Do here that you need
break;
}
}
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
switch (view.getId()){
case R.id.text_view:
if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) view.requestFocus();
break;
};
return false;
}
}
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