Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Update material navigation drawer header

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.

enter image description here

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.

like image 908
masmic Avatar asked Jun 18 '15 10:06

masmic


People also ask

How do you refresh a navigation drawer?

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.

What is DrawerLayout?

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.


1 Answers

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);
        ...
like image 123
masmic Avatar answered Sep 24 '22 08:09

masmic