Skip to content Skip to sidebar Skip to footer

How To Find Text In Android Through A Searchview Or Edittext?

I'm new in Android and I am making an application that has a TextView with a very long string, and I want the user to be able to search for particular word within the TextView thro

Solution 1:

Well will you clearify how you wanted to search the text? do you just want to find it that is there or not? and in addition do you want to search by entering each letter in the EditText?

if you just want to do the both above the trick is like below:

boolean hasText=false;
editText.addTextChangedListener(newTextWatcher() {
        @OverridepublicvoidbeforeTextChanged(CharSequence s, int start, int count, int after) {
        }

        @OverridepublicvoidonTextChanged(CharSequence s, int start, int before, int count) {
            if (count > 0 && (s.charAt(0) != '1')) {
               hasText=textView.getText().toString().contains(s.toString());
            }
        }

        @OverridepublicvoidafterTextChanged(Editable s) {

        }
    });

By checking the hasText true or false you will no is it there or not.

Post a Comment for "How To Find Text In Android Through A Searchview Or Edittext?"