Manish Yadav Manish Yadav - 2 months ago 21
Android Question

How to hide navigation drawer `menuitems` when particular fragment is selected?

Here is Login fragment :

public class Login extends Fragment {

TextInputEditText u_name,u_pass;
String name_user,password_user;
Button login_btn;
TextView user_register;
String PREF="myData";
SharedPreferences preferences;
SharedPreferences.Editor editor;
NavigationView navigationView;


public Login() {
// Required empty public constructor
}


@Override
public View onCreateView(final LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment

View view= inflater.inflate(R.layout.fragment_login, container, false);
u_name=(TextInputEditText) view.findViewById(R.id.user_name);
u_pass=(TextInputEditText) view.findViewById(R.id.user_password);
login_btn=(Button) view.findViewById(R.id.submit_btn);
user_register=(TextView) view.findViewById(R.id.register_user);

//setting up navigationview text and image

navigationView=(NavigationView) view.findViewById(R.id.nav_view);

navigationView.setNavigationItemSelectedListener(this);

login_btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
name_user=u_name.getText().toString().trim();
password_user=u_pass.getText().toString();

if (name_user.isEmpty())
{
u_name.setError("Enter email address");
}
else if (password_user.isEmpty())
{
u_pass.setError("Enter password");
}
else
{
loginUser();
}

}
});

user_register.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {

FragmentManager fragmentManager=getFragmentManager();
FragmentTransaction fragmentTransaction=fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.containerView,new RegisterUser()).addToBackStack(null).commit();

}
});
return view;
}

private void loginUser()
{
final ProgressDialog pd=ProgressDialog.show(getActivity(),"Login","Please wait",false);
StringRequest loginRequest=new StringRequest(Request.Method.POST, "http://mywebdata.com/webservice/logincheck.php", new Response.Listener<String>() {
@Override
public void onResponse(String response) {

pd.dismiss();
//Toast.makeText(getActivity(), ""+response, Toast.LENGTH_LONG).show();
if (response.contains("Invalid Email Address and Password"))
{
Toast.makeText(getActivity(), "Invalid Email Address and Password", Toast.LENGTH_SHORT).show();
}
else
{
getLoginUser(response);
}


//Log.e("login",""+response);

}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
pd.dismiss();
Log.e("Error",""+error.toString());
Toast.makeText(getActivity(), "Network error please try again", Toast.LENGTH_SHORT).show();

}
}){
@Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String,String> map=new HashMap<String, String>();
map.put("usrname",name_user);
map.put("passwrd",password_user);
return map;
}
};
RequestQueue queue= Volley.newRequestQueue(getActivity());
queue.add(loginRequest);
}

private void getLoginUser(String user)
{
ParseJson parseJson=new ParseJson(user);
parseJson.parseUserLogin();

if (ParseJson.login_mail[0].equals(name_user))
{
preferences=getActivity().getSharedPreferences(PREF, Context.MODE_PRIVATE);
editor=preferences.edit();
editor.putString("LOGIN_USER",ParseJson.login_id[0]);
editor.putString("LOGIN NAME",ParseJson.login_name[0]);
editor.commit();
}
else
{
Toast.makeText(getActivity(), "Invalid user name,Try again", Toast.LENGTH_SHORT).show();
}
}

}


problem is that when is call
navigationView.setNavigationItemSelectedListener(this);
it gives me error that i can not call this witihn fragment Login.I tried many SO answer but that not work in my case all of them are working fine with Activity but not within Fragment.

Thanks for your time...

Answer

Your fragment has no implemented 'NavigationView.OnNavigationItemSelectedListener' when you do

navigationView.setNavigationItemSelectedListener(this);

so do this

public class Login extends Fragment implements
                                    NavigationView.OnNavigationItemSelectedListener

and override the methods and you are good to go

Update : To hide and show menuitems in navigationView when specific fragment selected

1.) Initialize the navigationView in your activity

2.) Inside navigation listener

navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {

   @Override
   public boolean onNavigationItemSelected(MenuItem menuItem) {
      if(menuItem.getItemId()==R.id.login_fragment )  {
           // to hide items  
          navigationView.getMenu().findItem(R.id.item_to_hide).setVisible(false);
           //  to show items
          navigationView.getMenu().findItem(R.id.item_to_show).setVisible(true);
   }   
      return true;
   }
});

where getMenu() gives you menu object

findItem(id) gives you the menuitem to hide/show

setVisibile() will modify the state of menuitem to visible/invisible