Try to search your question here, if you can't find : Ask Any Question Now ?

How to hideshow SearchView from Toolbar?

HomeCategory: stackoverflowHow to hideshow SearchView from Toolbar?
Avatarsourav asked 4 months ago

I have ToolBar and I have defined Searchview as an item:

<menu xmlns:android="http://schemas.android.com/apk/res/android"
    <item
        android:id="@+id/action_search"
        android:icon="@android:drawable/ic_menu_search"
        app:showAsAction="never"
        app:actionViewClass="android.support.v7.widget.SearchView"
        android:title="Search"/>
</menu>

Then I have BaseActivity class where I define Toolbar:

protected final void onCreate(Bundle savedInstanceState, int layoutId)
{
    super.onCreate(savedInstanceState);
    setContentView(layoutId);

    Toolbar myToolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(myToolbar);
    getSupportActionBar().setDisplayShowTitleEnabled(false);

    assert myToolbar != null;

}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.menu, menu);

    return true;
}

I have search activity where I show toolbar and I would like to show searchview. The problem is that APP is crashing with null pointer exception that it can’t find SearchView action_search

public class SearchActivity extends BaseActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search);

        Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        SearchView searchview = (SearchView) findViewById(R.id.action_search);
        searchview.setVisibility(SearchView.VISIBLE);

    }
    }

What is correct way to showhide SearchView?

1 Answers
Best Answer
AvatarMatthias answered 4 months ago
Your Answer

7 + 18 =

Popular Tags

WP Facebook Auto Publish Powered By : XYZScripts.com