Search code examples
androidsearchview

Custom SearchView TextChange


Hi I have created a Custom SearchView but i cannot receive the entered text by user

this is my view_search.xml

        <EditText
            android:id="@+id/search_input_text"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginStart="16dp"
            android:layout_marginEnd="16dp"
            android:layout_toEndOf="@id/close_search_button"
            android:hint="جستجو..."
            android:layout_toRightOf="@+id/close_search_button"/>

and i put it to toolbar

            <ir.mahdi.circulars.CustomView.SearchView
                android:id="@+id/searchView"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:theme="@style/SearchView"
                app:showDividers="none" />

and this is my searchview class

class SearchView(
    context: Context,
    attrs: AttributeSet
) : FrameLayout(context, attrs) {

    init {
        LayoutInflater.from(context)
            .inflate(R.layout.view_search, this, true)

    }

i dont know how to handle searchview textchange


Solution

  • Did you try this in Kotlin?

    var searchView = findViewById<EditText>(R.id.search_input_text)
    
    var searchValue = searchView?.editableText.toString()
    

    If you want use listener in Java;

     searchView.addTextChangedListener(new TextWatcher() {
    
       @Override
       public void afterTextChanged(Editable s) {}
    
       @Override    
       public void beforeTextChanged(CharSequence s, int start,
         int count, int after) {
       }
    
       @Override    
       public void onTextChanged(CharSequence s, int start,
         int before, int count) {
    
       }
      });