I have a navigation drawer. When an event is called, I want to hide one of my navigation menu item for user. How can I do that?
protected void onCreate(Bundle sa
NavigationView navigationView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
hideItem();
}
private void hideItem()
{
navigationView = (NavigationView) findViewById(R.id.nav_view);
Menu nav_Menu = navigationView.getMenu();
nav_Menu.findItem(R.id.nav_settings).setVisible(false);
}
//hide logout menu item when the user is not loggrd in
//Checking the condition whether the user is logged in or not
if (!SharedPrefManager.getmInstance(this).isLoggedIn()){
Menu nav_Menu = navigationView.getMenu();
nav_Menu.findItem(R.id.nav_logout).setVisible(false);
}
//SharedPrefManager .java class where we define the isLoggedIn Method()
import android.content.Context;
import android.content.SharedPreferences;
import com.motortrade.motortrade.t_Model.User;
public class SharedPrefManager {
private static final String SHARED_PREF_NAME = "my_shared_pref";
private static SharedPrefManager mInstance;
private Context mCtx;
private SharedPrefManager(Context mCtx){
this.mCtx = mCtx;
}
public static synchronized SharedPrefManager getmInstance(Context mCtx){
if (mInstance == null){
mInstance = new SharedPrefManager(mCtx);
}
return mInstance;
}
public boolean isLoggedIn(){
SharedPreferences preferences = mCtx.getSharedPreferences(SHARED_PREF_NAME, Context.MODE_PRIVATE);
return preferences.getInt("user_id", -1) != -1;
}