Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

using onClick on TextView with selectable text - how to avoid double click?

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

like image 621
Vlad Alexeev Avatar asked Mar 26 '14 06:03

Vlad Alexeev


People also ask

How do I restrict double click on Android?

saving a last click time when clicking will prevent this problem. i.e. This is the only solution that actually prevents a double click.

How do you avoid multiple buttons clicking at the same time in flutter?

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.

How does Android handle multiple clicks?

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.


2 Answers

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.

like image 70
Radiokot Avatar answered Sep 18 '22 12:09

Radiokot


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;
    }
}
like image 40
SerjantArbuz Avatar answered Sep 21 '22 12:09

SerjantArbuz