I want to make my Searchview to be visible only when I open product option from the drawer, to do that I am trying using onOptionsItemSelected method to listen which options is being used. The problem is I am always get a NullPointerException everytime I tried to get my SearchView id from onOptionsItemSelected.
I knew how to hide the SearchView in onCreateOptionsMenu using :
menu.findItem(R.id.action_search).setVisible(false);
This is the source code for my onCreateOptionsMenu :
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
View lay = findViewById(R.id.nav_header);
//id in this line is found
menu.findItem(R.id.action_search).setVisible(false);
sharedpreferences = getSharedPreferences(Login.my_shared_preferences, Context.MODE_PRIVATE);
session = sharedpreferences.getBoolean(session_status, false);
id = sharedpreferences.getString(TAG_ID, null);
username = sharedpreferences.getString(TAG_USERNAME, null);
name = sharedpreferences.getString(TAG_NAME, null);
txt_id = lay.findViewById(R.id.txt_id_admin);
txt_username = lay.findViewById(R.id.txt_textView);
txt_name = lay.findViewById(R.id.txt_name_admin);
imb = lay.findViewById(R.id.btn_exit);
txt_id.setText("ID : " + id);
txt_name.setText("Nama : " + name);
txt_username.setText("Status : " + username);
imb.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
SharedPreferences.Editor editor = sharedpreferences.edit();
editor.clear();
editor.commit();
intent = new Intent(getApplicationContext(), Login.class);
finish();
startActivity(intent);
}
});
return true;
But I can’t do the same thing in onOptionsItemSelected method, because the argument it used was MenuItem instead of Menu.
This is my latest try for the onOptionsItemSelected method :
@Override
public boolean onOptionsItemSelected(MenuItem item) {
NavigationView navigationView = findViewById(R.id.nav_view);
Menu menu = navigationView.getMenu();
//id in this line is null
MenuItem item_search = menu.findItem(R.id.action_search);
Log.d(TAG, "Response : " + item_search);
SearchView searchView = (SearchView) item_search.getActionView();
if (item.getItemId() == R.id.nav_produk) {
searchView.setVisibility(View.VISIBLE);
return true;
} else {
Log.d(TAG, "Response : " + searchView);
searchView.setVisibility(View.INVISIBLE);
return super.onOptionsItemSelected(item);
}
}
And this is my res/menu/main.xml file :
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/action_settings"
android:visible="false"
android:orderInCategory="100"
android:title="@string/action_settings"
app:showAsAction="never"/>
<item
android:id="@+id/action_search"
android:visible="true"
android:orderInCategory="100"
android:icon="@android:drawable/ic_menu_search"
android:title="@string/search"
app:actionViewClass="android.widget.SearchView"
app:showAsAction="ifRoom|collapseActionView"/>
Advertisement
Answer
I laready found answer! Apparently I don’t need onOptionsItemSelected at all.
I am using NavController on my DrawerLayout, so the easier way to make a listener is using onDestinationChanged method inside onCreateOptionsMenu
This is the result :
@Override
public boolean onCreateOptionsMenu(final Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
View lay = findViewById(R.id.nav_header);
menu.findItem(R.id.action_search).setVisible(false);
sharedpreferences = getSharedPreferences(Login.my_shared_preferences, Context.MODE_PRIVATE);
session = sharedpreferences.getBoolean(session_status, false);
id = sharedpreferences.getString(TAG_ID, null);
username = sharedpreferences.getString(TAG_USERNAME, null);
name = sharedpreferences.getString(TAG_NAME, null);
txt_id = lay.findViewById(R.id.txt_id_admin);
txt_username = lay.findViewById(R.id.txt_textView);
txt_name = lay.findViewById(R.id.txt_name_admin);
imb = lay.findViewById(R.id.btn_exit);
txt_id.setText("ID : " + id);
txt_name.setText("Nama : " + name);
txt_username.setText("Status : " + username);
imb.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
SharedPreferences.Editor editor = sharedpreferences.edit();
editor.clear();
editor.commit();
intent = new Intent(getApplicationContext(), Login.class);
finish();
startActivity(intent);
}
});
//Only show SearchView when produk is open
NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
navController.addOnDestinationChangedListener(new NavController.OnDestinationChangedListener() {
@Override
public void onDestinationChanged(@NonNull NavController controller, @NonNull NavDestination destination, @Nullable Bundle arguments) {
if (destination.getId() == R.id.nav_produk) {
menu.findItem(R.id.action_search).setVisible(true);
} else {
menu.findItem(R.id.action_search).setVisible(false);
}
}
});
return true;
}