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.
Related
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.
I want to implement Navigation drawer it's working well but when i set app:elevation="0dp" in AppBarLayout at that time the navigation drawer is not opening. i have tried folowing code.
toolbar.setNavigationOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerLayout.openDrawer(GravityCompat.START);
LOGD("Clicked:::","Drawer");
}
});
and also using toggle
drawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, toolbar,
R.string.app_name, R.string.app_name);
drawerToggle.setDrawerIndicatorEnabled(true);
drawerToggle.setHomeAsUpIndicator(R.drawable.ic_menu);
mDrawerLayout.setDrawerListener(drawerToggle);
drawerToggle.syncState();
but it's not working, i stuck from many days, pls help to solve it!!!
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);
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);
}
});
In my application , i created a navigation drawer using Android studio template.
It works perfectly in Android 5.0 above devices but the title bar does not appear in Kitkat devices (only statius bar can be seen) , I was unable to find a solution,
The below is my code
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().show();
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) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
Intent intent = getIntent();
linkNo = intent.getIntExtra(FirstMenuFragment.LINK_NO, 0);
//Make firstItem active
navigationView.getMenu().getItem(linkNo).setChecked(true);
}
Thanks in advance
In KitKat, making the statusbar (semi-)transparent is more complex compared to Lollipop+. So the titlebar is still there, it's just below the status bar ;)
Edit: If you absolutely want to achieve this on KitKat, you could use a SystemBarTintManager (https://github.com/jgilfelt/SystemBarTint/blob/master/library/src/com/readystatesoftware/systembartint/SystemBarTintManager.java).
But I don't recommend that, I've done it myself and it's not so easy to get it right.