How to change hamburger icon in Android (NavigationDrawer) - android

Before write this thread I have try to implement the different solution that I found in stackoverflow, but nothing work properly.
I'm developing an Android applucation that use the custom navigation drawer, I have to change the standard icon of actionbar (now is toolbar, right ?), and settings icon.
This is my code:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
toolbar.setTitleTextColor(Color.parseColor("#009754"));
setSupportActionBar(toolbar);
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.setDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
}
And this is what i try to implement:
This solution not work:
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
toolbar.setTitleTextColor(Color.parseColor("#009754"));
toolbar.setNavigationIcon(R.drawable.ic_draw);
setSupportActionBar(toolbar);
This solution not work:
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDefaultDisplayHomeAsUpEnabled(false);
toggle.setDrawerIndicatorEnabled(false);
toggle.setHomeAsUpIndicator(R.drawable.ic_custom_drawer_icon);
I don't understand why i can't change the icon, i have no idea what's the problem...

Simple and Elegant solution
Place
getSupportActionBar().setHomeButtonEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeAsUpIndicator(R.drawable.dashboardicon);//your icon here
after
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer,toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.addDrawerListener(toggle);
toggle.syncState();
Overall Solution in Navigation Activity
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer,toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.addDrawerListener(toggle);
toggle.syncState();
getSupportActionBar().setHomeButtonEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeAsUpIndicator(R.drawable.dashboardicon);
navigationView= (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
Note: It doesnt need any setNavigationOnClickListener()

Disable drawer indicator for ActionBarDrawerToggle:
toggle.setDrawerIndicatorEnabled(false);
and then:
toolbar.setNavigationIcon(R.drawable. ic_custom_drawer_icon);

Just use this :
toolbar.post(new Runnable() {
#Override
public void run() {
Drawable d = ResourcesCompat.getDrawable(getResources(), R.mipmap.ic_launcher, null);
toolbar.setNavigationIcon(d);
}
});

Related

Cannot restore the navigation drawer button after changing it with a back button

When I go back after changing it to a back button it disappears with the following code:
ActionBar actionBar = getSupportActionBar();
actionBar.setDisplayHomeAsUpEnabled(false);
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
ActionBarDrawerToggle mToggle = new ActionBarDrawerToggle(this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
mToggle.setDrawerIndicatorEnabled(true);
drawer.setDrawerListener(mToggle);
drawer.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED);
I think that code should make the navigation drawer button to open menu to appear again, but it doesn't.
Any idea of what could be wrong in that code, so the navigation drawer button appears again?
Try like this:
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
if (getSupportActionBar() != null) getSupportActionBar().setDisplayHomeAsUpEnabled(true);
//.....
Also, you could use androidx.navigation.ui.NavigationUI package to setup your drawer menu.
Here are good articles about NavigationUI.

ActionBarDrawerToggle does not open drawer until first slide

I am using Google Design Support Library and DrawerLayout.
Setup:
final DrawerLayout drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
NavigationView drawer = (NavigationView) findViewById(R.id.drawer);
if(mDrawerToggle == null) {
mDrawerToggle = new ActionBarDrawerToggle(this, drawerLayout, mToolbar, R.string.open, R.string.feather_close);
drawerLayout.addDrawerListener(mDrawerToggle);
mDrawerToggle.syncState();
}
When I start my app, the ripple effect of the hamburger icon is shown but the drawer is not opened. When I open the drawer at least one time by sliding from the left, the hamburger icon works for the entire runtime.
I don't have a special listener on the toggle button or the drawer itself and the onOptionsItemSelected method is not called.
Please help me to find out what happens.
Thank you.
I found it: I accidentally had android:visibility="gone" in my NavigationView.
What a freaky side effect.
I solved it using it :
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.setDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);

How to remove elevation (drop shadow) from a DrawerLayout in Android Studio?

How can I remove the elevation (drop shadow) from a DrawerLayout in Android Studio? I've looked everywhere, and tried adding app:elevation="0dp" and android:elevation="0dp" to my DrawerLayout xml, but it doesn't work.
I even tried adding setElevation(0f) to my MainActivity, but that didn't work either.
What am I doing wrong? Thanks.
(P.S. The project's base API is 16... I tried changing it to 21, but that didn't make any difference.)
Here's my onCreate:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Snackbar.make(view, "MUHAHA BURP BARF BARF WHAHAHA", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
DrawerLayout drawer = (DrawerLayout) findViewById(drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.setDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
populateListView();
}
Solved it. Turns out I was trying to remove the elevation from drawer_layout when I should've been removing it from drawer. Putting drawer.setDrawerElevation(0f); in my MainActivity fixed the problem.

Icon in ActionBar blinking

I want to create an Action Bar for my app that displays a blinking logo. I want to display the image every two seconds and when the image hides, display the fragment title.
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
//getSupportActionBar().setLogo(R.drawable.logo_nuevo);
/* getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowTitleEnabled(false);
getSupportActionBar().setIcon(R.drawable.logo_nuevo); */
mDrawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawerToggle = new ActionBarDrawerToggle(this, mDrawer, toolbar, R.string.drawer_open, R.string.drawer_close);
mDrawer.addDrawerListener(drawerToggle);
nvDrawer = (NavigationView) findViewById(R.id.nvView);
nvDrawer.inflateHeaderView(R.layout.nav_header);
setupDrawerContent(nvDrawer);

Open NavigationView only clicking on top-left icon but not with swype gesture

I am implementing the navigation view in my app.
Actually, I can open it both clicking on "hamburger" icon (in my toolbar) and swypeing from left to right.
I want to open it only through the icon in my toolbar disabling the swype. Is it possible to do that?
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, drawer, sToolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.addDrawerListener(toggle);
toggle.syncState();
EDIT
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED); // HERE
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, drawer, sToolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.addDrawerListener(toggle);
toggle.syncState();
If I use the code above, where I used DrawerLayout.LOCK_MODE_LOCKED_CLOSED, I disable both icon and swype.
This works for my case
Lock it:
drawer.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);
And unlock it :
drawer.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED);
Hope to help!
To make only the click work on the hamburger icon and not the swipe, i did the following,
DrawerLayout mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, mDrawerLayout, mToolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
mDrawerLayout.addDrawerListener(toggle);
toggle.syncState();
mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);
mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED);
mDrawerLayout.openDrawer(GravityCompat.START);
}
});

Categories

Resources