How to return button in searchview in Android

I've been searching the Internet for more than 4 hours on how to make the back button work when searchview is open, but I can't achieve it

This is my search view

    searchview  = (SearchView) findViewById(R.id.searchView);
        searchview.setOnClickListener(this);
  @Override
    public void onClick(View v) {
        if (v.getId() == R.id.searchView){

           //Cursor cursor = manager.obtenerIDColumnaServicio(searchview.getQuery().toString());
            adapterOfDataBase.changeCursor(cursor);
        }
    }

This is XML:

   <SearchView
                    android:layout_width="match_parent"
                    android:layout_height="63dp"
                    android:id="@+id/searchView"
                    android:iconifiedByDefault="true"
                    android:backgroundTint="#ff000000"
                    android:layout_marginRight="0dp" />

Invalid Sign

I've tried 2 options, but none of them work. If you can help me

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) { //MEJORAR ESTO
    if (keyCode == KeyEvent.KEYCODE_BACK) {
        searchview.onActionViewCollapsed();
        return true;
    }
    return super.onKeyDown(keyCode, event);
}
//None of this 2 methods are working. I tried them separately
@Override
public void onBackPressed(){
    if (!this.searchview.isIconified()) {
        this.searchview.setIconified(true);
    } else {
        super.onBackPressed();
    }
}

resolvent:

When searchview is turned on and the back button is pressed, you want to see searchview turn off (that is, seticonfirmed (true)). This is a standard behavior, which is implemented according to the Google application searchview

So that's what you need to do

    mSearchView.setOnQueryTextFocuschangelistener(new View.OnFocuschangelistener() {
        @Override
        public void onFocusChange(View v, boolean hasFocus) {
            if (!hasFocus) {
                mSearchView.setIconified(true);
            }
        }
    });

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>