Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to determine when text is selected [Android [EditText]]

Is possible to determine when text is selected and when is not?

I was googling and I find onSelectionChanged() method or setOnLongClickListener() for determining when user longClicks the editText so when he made selection, but in both cases it can't help me with determining, when the user is not selecting any text (I could set button invisible)...

like image 549
Mr. Robot Avatar asked Oct 24 '25 06:10

Mr. Robot


2 Answers

You can use

yourEditText.setOnTouchListener(new View.OnTouchListener() {
          @Override
          public boolean onTouch(View v, MotionEvent event) {

            if (MotionEvent.ACTION_UP == event.getAction()) {

              if (yourEditText.hasSelection()) {
                // if true, the text in the EditText is selected
              }

            return false;
          }
        });


That should do it.

like image 154
HedeH Avatar answered Oct 26 '25 20:10

HedeH


int startSelection=et.getSelectionStart();
int endSelection=et.getSelectionEnd();  

The getSelectionStart() method will return the start of the selection anchor/cursor or -1 if the user has not selected any text. You could try using this.

like image 23
An SO User Avatar answered Oct 26 '25 20:10

An SO User