0

Hello i wanna make my EditText to occupy all of my toolbar when i clicked it and i want that after the user ends typing to make the toolbar go back to normal.

Here is my xml file for the menu

<item android:id="@+id/action_search"
    android:title="search"
    android:orderInCategory="100"
    android:icon="@drawable/search"
    app:showAsAction="ifRoom"
    />

And here is my java code

 @Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()){

        case R.id.action_search :
            search.setHint("Procura");
            toolbar.addView(search);
            toolbar.setBackgroundColor(Color.WHITE);
            search.setOnEditorActionListener(new EditText.OnEditorActionListener() {
                @Override
                public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                    if (actionId == EditorInfo.IME_ACTION_SEARCH ||
                            actionId == EditorInfo.IME_ACTION_DONE ||
                            event.getAction() == KeyEvent.ACTION_DOWN &&
                                    event.getKeyCode() == KeyEvent.KEYCODE_ENTER) {
                        // the user is done typing.
                        SearchActivity.this.text = search.getText().toString();
                        return true;
                    }
                    return false;
                }
            });

            return true;

        default : return super.onOptionsItemSelected(item);
    }
}

Thank you in advance.

Raidline
  • 81
  • 9

1 Answers1

1

I suggest you to use SearchView,try this way

Gradle

dependencies {
        compile 'com.lapism:searchview:2.2'
        }

XML

<com.lapism.searchview.view.SearchView
    android:id="@+id/searchView"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

Java Code

@Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.action_search: {
                mSearchView.show(true/false); // animate, ONLY FOR MENU ITEM
                return true;
            }
            default:
                return super.onOptionsItemSelected(item);
        }
    }
Aditya Vyas-Lakhan
  • 13,409
  • 16
  • 61
  • 96