Change NavigationView items when user is logged
I thing the best approach to this is to include all your items in the menu and the change their visibility.
<item
android:id="@+id/login"
android:icon="@drawable/ic_action_person"
android:title="@string/login"
android:visible="true" />
<item
android:id="@+id/logout"
android:icon="@drawable/ic_action_person"
android:title="@string/logout"
android:visible="false" />
then
navigationView.getMenu().findItem(R.id.login).setVisible(false);
navigationView.getMenu().findItem(R.id.logout).setVisible(true);
You can also do this with whole groups of items
<group
android:id="@+id/group_1"
android:checkableBehavior="single"
android:visible="false">
...
</group>
and
navigationView.getMenu().setGroupVisible(R.id.group_1, true)
How can I remove a MenuItem if user is logged in or change the title?
You will have to keep references
to the menu items you want to mutate
(change name, make invisible etc). To achieve this use follwoing code in your activity
// Declate two variables in your activity
private lateinit var register: MenuItem
private lateinit var logIn: MenuItem
// Now initialize them in your activity's onCreate as follows
var navigationView = findViewById(R.id.idOfNavigationView)
register = navigationView.menu.findItem(R.id.idOfRegister)
logIn = navigationView.menu.findItem(R.id.idOfLogin)
Now you have your menu
items and you can change their title or make them invisible.
For example if you want to hide the register menu item and change the title of login, you will do the following
// Once user has logged in successfully, use following lines to change menu items as you wish
register.isVisible = true
logIn.title = "logout"
Navigation Components: Switch Menu of Navigation Drawer (for logged in users)
You can access Activity from Fragment through interface:
- Create and interface say
LoginSuccessListener
- Implements this in your
Activity
- Inside fragment receive this listener inside
onAttach
- Use it as you needed to update
Navigation Menu
change user's name in navigation menu when user logs in
You can use the code below to get the navigation view and replace "Your Text Here" with your username
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
View headerView = navigationView.getHeaderView(0);
TextView navUsername = (TextView) headerView.findViewById(R.id.navUsername);
navUsername.setText("Your Text Here");
This worked for me...
Menu item visibility doesnt change partly
In order to make sure that mAuth.currentUser
is null
, you need to add a listener when the user has logged out so that you can update visibility of menu items; because you might call updateUI()
before the user sign out, so mAuth.currentUser
still has a value.
So in onNavigationItemSelected()
callback: add this listener
R.id.nav_logout_item->{
mAuth.signOut()
AuthUI.getInstance()
.signOut(this) // context
.addOnCompleteListener {
// User has been logged out
updateUI()
}
}
And do the same for the login behavior to return the visibility back to the menu items.
How to dynamically change navigation bar item's logo in Java class?
Try this,
NavigationView navigationView = (NavigationView)findViewById(R.id.nav_view);
navigationView.getMenu().getItem(1).setIcon(R.drawable.com_facebook_button_icon_white);
Hope this helps
Reload navigationView content by pressing an id in the navigationView
Ok, so my way to do seems to be a terrible idea. After some research, it seems that we can use visible true or visible false for an item or a group in a menu.
So it's just necessary to do an onClick method and set the group visible or invisible when clicking on a pic or another.
Source : Change NavigationView items when user is logged
How to change the text view when a new user is logged in?
Create one Global PrefManager class like this and create a three field to set data
public class PrefManager {
// Shared preferences file name
public static final String PREF_NAME = "PACKAGE_NAME" + "_Preferences";
private static final String KEY_EMPID = "Emp_ID";
private static final String KEY_EMPCODE = "Emp_Code";
private static final String KEY_EMP_NAME = "Emp_Name";
SharedPreferences pref;
// Editor for Shared preferences
Editor editor;
// Context
Context _context;
// Shared pref mode
int PRIVATE_MODE = 0;
public PrefManager(Context context) {
this._context = context;
pref = _context.getSharedPreferences(PREF_NAME, PRIVATE_MODE);
editor = pref.edit();
}
public String getEmpID() {
return pref.getString(KEY_EMPID, null);
}
public void setEmpID(String token) {
editor.putString(KEY_EMPID, token);
editor.commit();
}
public String getEmpCode() {
return pref.getString(KEY_EMPCODE, null);
}
public void setEmpCode(String token) {
editor.putString(KEY_EMPCODE, token);
editor.commit();
}
public String getEmpName() {
return pref.getString(KEY_EMP_NAME, null);
}
public void setEmpName(String token) {
editor.putString(KEY_EMP_NAME, token);
editor.commit();
}
}
Login Activity Call Intent Time Set Data to shared Preferences you also save data on the register time
public class LoginActivity{
PrefManager pref;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
// inilize pref manager class
pref = new PrefManager(this);
}
if (currentUser != null) {
//Snackbar.make(buttonLogin, "Login Successful!", Snackbar.LENGTH_LONG).show();
//User Logged in successfully Launch You home screen activity
Intent intent = new Intent (Login.this,Navigation.class);
pref.setEmpID(//passyour employee id);
startActivity(intent);
finish();
}
on navigation, activity get data from shared preferences and store to string and set to drawer textview
public class navigationActivity()
***PrefManager pref;***
String EmpId;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_navigation);
***pref = new PrefManager(this);***
EmpId = pref.getEmpID;
}
// OnnavigationDrawer header
textView.setText("EmpID:"+EmpId);
Related Topics
Android: Is Using Setcontentview Multiple Times Bad While Changing Layouts
Problem About SQLite Database, No Such Table
Handling Keepsynced() While on Background on Android and with Fcm
How to Get Meid and Imei Information Using Adb Commands on Android 5.0
My App Was Rejected by Instagram Basic Display API Review Due to Invalid Reasons
Best Way to Save Large Amount of Data Locally in Unity3D Android
Which Port and Protocol Does Google Cloud Messaging (Gcm) Use
Firebase Messaging Nosuchmethoderror.Zzur Exception
Android Facebook Authorization - Can Not Log in When Official Facebook App Is Installed
Canonical Registration Id and Message Id Format
Outdated Kotlin Runtime Warning in Android Studio
Android Google Maps V2: How to Add Marker with Multiline Snippet
Getting Wifi Signal Strength in Android
Loading an Android Resource into a Webview
Android Downloadmanager Progress
Get a List of Available Content Providers
Custom Global Application Class Breaks with "Android.App.Application Cannot Be Cast To"