waldemar
waldemar

Reputation: 695

Search button doesn't work

I need to do search button in soft keyboard of my device. MyCode:

XML file:

<EditText
    android:id="@+id/searchText"
    android:layout_width="match_parent"
    android:layout_height="40dp"
    android:textSize="15sp"
    android:hint="@string/search"
    android:drawableLeft="@drawable/ic_btn_search"
    android:singleLine="true"
    android:drawablePadding="5dp"
    android:imeOptions="actionSearch"
    android:imeActionLabel="Search" android:layout_weight="1"/>

Java file:

   searchFild = (EditText) findViewById(R.id.searchEditTxt);
    searchFild.setImeOptions(EditorInfo.IME_ACTION_SEARCH);
    searchFild.setOnEditorActionListener(new TextView.OnEditorActionListener() {
        @Override
        public boolean onEditorAction(TextView textView, int arg, KeyEvent keyEvent) {
            if(arg == EditorInfo.IME_ACTION_SEARCH) {
                String searchString=searchFild.getText().toString();
                searchBibles(searchString);
                return true;
            }
            return false;
        }
    });

Search button is preset in keyboard, but after click on it is nothing happends. arg is zerro. Its work in android 2.2, but not work in android 4

Upvotes: 0

Views: 1703

Answers (1)

mvieghofer
mvieghofer

Reputation: 2896

in the xml you need to configure the inputType to be text. In the Java code you can remove searchFild.setImeOptions(EditorInfo.IME_ACTION_SEARCH);, at least this made it work on Android 4.1.2 (that's the only version I've tested)

The full code I use looks like this:

<EditText
        android:id="@+id/txtSearch"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:drawableLeft="@drawable/action_search"
        android:drawablePadding="5dp"
        android:hint="@string/input_keywords_for_search"
        android:imeActionLabel="Search"
        android:inputType="text"
        android:imeOptions="actionSearch" />

And the Java Code

final EditText txtSearch = (EditText) findViewById(R.id.txtSearch);
txtSearch.setOnEditorActionListener(new OnEditorActionListener() {

    @Override
    public boolean onEditorAction(TextView v, int actionId,
        KeyEvent event) {
        if (actionId == EditorInfo.IME_ACTION_SEARCH) {
        String searchString = txtSearch.getText().toString();
        search(searchString);
        return true;
    }
    return false;
    }
});

I hope this helps!

Upvotes: 2

Related Questions