masmic
masmic

Reputation: 3574

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.

Upvotes: 7

Views: 3445

Answers (1)

masmic
masmic

Reputation: 3574

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);
        ...

Upvotes: 3

Related Questions