I'm using the recently Google provided support library to add the navigation drawer to my app. In the navigation drawer's header, I define a CircleImageView to set user's profile pic, an his name under it.
All this info, is first defined by the user in the app's first start, so when the mainActivity starts, the image and the name are loaded into the header. But I'm giving the user the chance to modify these inside the app. If the user goes to the profile frag, and selects a new image as profile pic, or if he changes the name, this parameters won't update in the navigation drawer's header, until the app is closed and loaded again.
This is how I have it defined in mainActivity:
public class MainActivity extends AppCompatActivity implements DrawerLayout.DrawerListener {
private NavigationView navView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
....
navView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
@Override public boolean onNavigationItemSelected(MenuItem menuItem) {
menuItem.setChecked(true);
displayFragment(menuItem.getItemId());
drawerLayout.closeDrawers();
return true;
}
});
}
There is no need to implement DrawerLayout.DrawerListener, but as seen in the top, I've done it to try to update the header in real time. These implements the next methods:
@Override
public void onDrawerSlide(View drawerView, float slideOffset) {
}
@Override
public void onDrawerOpened(View drawerView) {
try {
ContextWrapper cw = new ContextWrapper(getApplicationContext());
File directory = cw.getDir("profile", Context.MODE_PRIVATE);
File mypath = new File(directory, "thumbnail.png");
Bitmap bitmap = BitmapFactory.decodeStream(new FileInputStream(mypath));
thumbview.setImageBitmap(bitmap);
username = mPreferences.getString("NAME", null);
nameview.setText(username);
} catch (FileNotFoundException e) {
Log.e("LOAD_IMAGE", e.getMessage(), e);
thumbview.setImageResource(R.drawable.default_thumbnail);
}
}
@Override
public void onDrawerClosed(View drawerView) {
}
@Override
public void onDrawerStateChanged(int newState) {
}
In the onDrawerOpened() method, I've used the same code I use in the onCreate to load the profile pic and the name. This should update these parameter every time the drawer is opened, but is not doing it, still does not update the header until I close and reopen the app.
Whenever you want to update the Drawer Layout, update the custom adapter by calling, adapter. notifyDataSetChanged(); and then call mDrawerLayout. invalidate(); Make sure that the getView() method inside your adapter picks up the latest points value.
DrawerLayout acts as a top-level container for window content that allows for interactive "drawer" views to be pulled out from one or both vertical edges of the window.
In onCreate, I need to define the next code, to be able to update in real time the navigation drawer's header:
public class MainActivity extends AppCompatActivity implements DrawerLayout.DrawerListener {
private DrawerLayout drawerLayout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
drawerLayout.setDrawerListener(this);
...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With