问题
I'm trying to find how to change the hamburger icon that opens the navigation drawer to some custom image that can be loaded with Picasso or something like that. Just like in the image above, where Twitter managed to put my profile picture replacing the hamburger icon they had before.
I couldn't find anything like this here and I don't have a clue about how to do it.
If anyone got a sample or some guidance on how to do it, I would really appreciate it.
EDIT
Some of my Activity's code:
ActivityMapsBinding binding;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
binding = DataBindingUtil.setContentView(this, R.layout.activity_maps);
...
setSupportActionBar(binding.mapsContent.toolbar);
getSupportActionBar().setTitle("");
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, binding.drawerLayout, binding.mapsContent.toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
binding.drawerLayout.addDrawerListener(toggle);
toggle.syncState();
binding.navView.setNavigationItemSelectedListener(this);
}
@Override
public void onBackPressed() {
if (binding.drawerLayout.isDrawerOpen(GravityCompat.START)) {
binding.drawerLayout.closeDrawer(GravityCompat.START);
} else if (doubleBackToExitPressedOnce) {
super.onBackPressed();
return;
}
this.doubleBackToExitPressedOnce = true;
Toast.makeText(this, R.string.message_press_twice, Toast.LENGTH_SHORT).show();
new Handler().postDelayed(() -> doubleBackToExitPressedOnce = false, 2000);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()){
case R.id.nav_profile:
break;
case R.id.nav_settings:
break;
}
binding.drawerLayout.closeDrawer(GravityCompat.START);
return true;
}
EDIT 2 - FOUND THE ANSWER
After the help of Bruno Pinto here in this thread and a little bit of research I finally made it work. You need to create a Target and set the tag of your ImageView as this Target, and when you load with Picasso, you reference this ImageView you created. Inside the Target's constructor you replace your views with the loaded Bitmap.
Here's how I did it:
private void changeImageDrawable(){
ImageView profileImage = (ImageView) binding.navView.getHeaderView(0).findViewById(R.id.iv_profile_image);
final Target target = new Target() {
@Override
public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
profileImage.setImageBitmap(bitmap);
Drawable drawable = new BitmapDrawable(getResources(), bitmap);
binding.mapsContent.toolbar.setNavigationIcon(drawable);
}
@Override
public void onBitmapFailed(Drawable errorDrawable) {
}
@Override
public void onPrepareLoad(Drawable placeHolderDrawable) {
}
};
profileImage.setTag(target);
Picasso.with(this)
.load("YOUR_IMAGE_URL_HERE")
.placeholder(R.drawable.placeholder_profile)
.error(R.drawable.placeholder_profile)
.into(target);
}
You should probably change you views drawable or bitmap in case of error as well.
回答1:
You can use .setNavigationIcon() method from the Toolbar.
Something like this:
((Toolbar) getSupportActionBar()).setNavigationIcon(YOUR_ICON);
来源:https://stackoverflow.com/questions/45445646/android-navigation-icon-profile-picture-instead-of-hamburger-icon