madara09
madara09

Reputation: 81

How to convert my SearchView into an EditText?

I want to transfer my SearchView function to an EditText. How can I do that? when I tried to transfer it, I got an error or crashing so can you help me with my little problem.

and here is my edittext

    <EditText
        android:id="@+id/editText"
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:layout_gravity="center"
        android:background="@drawable/rounded_edittext"
        android:ems="10"
        android:inputType="textPersonName"
        android:textAlignment="center" />

Sorry I'm a newbie in Android.

Upvotes: 2

Views: 3351

Answers (2)

OneCricketeer
OneCricketeer

Reputation: 191983

Well, you can't put an EditText inside of the menu xml the way you've written

You can try this instead

<menu 
  xmlns:android="http://schemas.android.com/apk/res/android" >
    <item 
        android:id="@+id/action_edit"
        android:icon="@drawable/actionbar_button_search"
        android:title="Search"
        android:showAsAction="always"
        android:actionViewClass="android.widget.EditText"  />

If that's not what you mean, and you've instead put the EditText in the Activity, then you're missing a findViewById to get it, and it isn't part of the options menu, so you're not going to be replacing the SearchView where it is in the question.

Instead, go to onCreate method, and set the hint and add a TextWatcher instead of the Query listener

Upvotes: 0

Vygintas B
Vygintas B

Reputation: 1694

And answer to your question:

You can not convert searchview into EditText, because searchView is Widget which contains a lot of Views like EditText, Textview.

If you need to manipulate editText of searchview for hint message, color etc you can get EditText from searchview. Actually its called AutoCompleteTextView

AutoCompleteTextView EditText = (AutoCompleteTextView) searchView.findViewById(R.id.search_src_text);

EDIT

If you want EditText behave like SearchView

editText.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {

        }

        @Override
        public void afterTextChanged(Editable s) {

        }
    });

Upvotes: 3

Related Questions