Reputation: 428
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 show\hide SearchView
?
Upvotes: 1
Views: 2359
Reputation: 779
(Kotlin)
Here is a simple way to show/hide searchview:
private lateinit var menuItem: MenuItem
private var mSearchView: SearchView? = null
private fun updateSearchUI(visible: Boolean) {
if (isChecked) {
menuItem.isVisible = true
}else{
menuItem.isVisible = false
}
}
On first load of Activity if you want to check some preferences and show/hide:
override fun onCreateOptionsMenu(menu: Menu?): Boolean {
menuInflater.inflate(R.menu.menu, menu)
menuItem = menu!!.findItem(R.id.shortcut_search)
mSearchView = menuItem.actionView as SearchView
updateSearchUI(PreferenceManager.getVisibilityStatus(applicationContext))
return true
}
if you want to toggle searchView show/hide:
switch_on_off.setOnCheckedChangeListener { buttonView, isChecked ->
updateSearchUI(isChecked)
}
}
Upvotes: 0
Reputation: 1856
Try below:
First: Change "app:showAsAction" attribute to "always"
<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="always"
app:actionViewClass="android.support.v7.widget.SearchView"
android:title="Search"/>
</menu>
Second: You can not access SearchView directly in onCreate. So remove below code.
SearchView searchview = (SearchView) findViewById(R.id.action_search);
searchview.setVisibility(SearchView.VISIBLE);
Third: You can access the SearchView when it is add to toolbar, i.e. when onCreateOptionsMenu is called.
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, menu);
MenuItem mSearch = menu.findItem(R.id.action_search);
mSearchView = (SearchView) mSearch.getActionView();//Store the reference in global variable of Base activity.
//Do you customization here, like
mSearchView.setOnQueryTextListener(this);
return true;
}
Fourth: Now you have the reference to SearchView. So you can show/hide search view, like below:
if(mSearchView.isShown()) {
mSearchView.setVisibility(View.GONE);
} else {
mSearchView.setVisibility(View.VISIBLE);
}
Upvotes: 0
Reputation: 7106
Please take note that onCreate
is the first activity life cycle method to be called. onCreateOptionsMenu
is triggered during (depending on your Android version) or after onCreate
. With this in mind, when you findViewById(R.id.action_search)
inside your onCreate
method, it will yield a NULL
object, thus the error.
Please check here for other observations by other developers.
What is correct way to show\hide SearchView?
You don't have to manually show/hide it. In your menu resource, you already have the option android:showAsAction="never"
included. It means:
Never place this item in the app bar. Instead, list the item in the app bar's overflow menu.
By default, your search view item is already hidden. To show it, you need to click the overflow menu (three dots) on the toolbar.
Upvotes: 0
Reputation: 69
In your SearchActivity, use the below code:
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
MenuItem menuItem = menu.findItem(R.id.action_search);
menuItem.setVisible(false);
return true;
}
Upvotes: 1