ActionBarDrawerToggle is not setting Drawer Indicator - android

I am trying to add the navigation drawer in my app.. Everything is working fine But now I still got the arrow icon although I replaced it with the ic_drawer from Android? Here's my code:
private ActionBarDrawerToggle mDrawerToggle;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
DrawerLayout mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
ExpandableListView mDrawerList = (ExpandableListView) findViewById(R.id.left_drawer);
mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow,
GravityCompat.START);
mDrawerList.setAdapter(new DrawerListAdapter(this));
mDrawerList.setOnChildClickListener(new DrawerItemClickListener());
mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,
R.drawable.ic_launcher, R.string.drawer_open,
R.string.drawer_close);
Log.d("Mudit",
"mDrawerToggle" + mDrawerToggle.isDrawerIndicatorEnabled()); // returns true
mDrawerLayout.setDrawerListener(mDrawerToggle);
getActionBar().setDisplayHomeAsUpEnabled(true);
getActionBar().setHomeButtonEnabled(true);
getActionBar().setDisplayShowHomeEnabled(false);
}
/**
* When using the ActionBarDrawerToggle, you must call it during
* onPostCreate() and onConfigurationChanged()...
*/
#Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
mDrawerToggle.syncState();
}
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
mDrawerToggle.onConfigurationChanged(newConfig);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if (mDrawerToggle.onOptionsItemSelected(item)) {
return true;
}
return super.onOptionsItemSelected(item);
}
XML:
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ScrollView
android:id="#+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
</LinearLayout>
</ScrollView>
<ExpandableListView
android:id="#+id/left_drawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="#ffffff"
android:dividerHeight="0dp" />
</android.support.v4.widget.DrawerLayout>
I have looking at various code samples online but all of them are doing same thing. I dont know why it is not working for me.
Please help.

You'll have to use
getActionBar().setDisplayShowHomeEnabled(true);
instead of
getActionBar().setDisplayShowHomeEnabled(false);
Cheers,
Felix
EDIT: Here's how to hide the Icon via XML:
add this to your styles.xml: (Replace android:Widget.Holo.Light.ActionBar.Solid with android:Widget.Holo.ActionBar.Solid if you are using the dark action bar)
<style name="ActionBarStyle" parent="android:Widget.Holo.Light.ActionBar.Solid">
<item name="android:icon">#android:color/transparent</item>
</style>
and then add this to your Theme you have defined in AndroidManifest.xml (Standard is the AppBaseTheme in /res/values-v11/styles.xml - AppBaseTheme:
<item name="android:actionBarStyle">#style/ActionBarStyle</item>
Hope this helps!

try this code
#Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
// Sync the toggle state after onRestoreInstanceState has occurred.
actbardrawertoggle.syncState();
}

This worked for me when I wanted to get the menu icon back from the back arrow
private void updateNavigationIcon() {
if (getSupportFragmentManager().getBackStackEntryCount() == 0
&& getSupportActionBar() != null) {
getSupportActionBar().setDisplayHomeAsUpEnabled(false);
toggle.syncState();
}
}

Just add the following line inside showGlobalContextActionBar() method (NavigationDrawerFragment.java class)
actionBar.setHomeAsUpIndicator(R.drawable.ic_action_menu);
Code snippet:
private void showGlobalContextActionBar() {
ActionBar actionBar = getActionBar();
actionBar.setDisplayShowTitleEnabled(true);
actionBar.setHomeAsUpIndicator(R.drawable.ic_action_menu);
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
actionBar.setTitle(R.string.app_name);
}

Related

Android 5 Hamburger doen't open the drawer

I'm trying to make a simple navigation drawer, matching the material guidelines. I'm following along the official training. Everything is running well, only a tap on the hamburger icon won't open the drawer. I can open the drawer with a swipe from the side, only the hamburger isn't working. I already looked up some other questions like this one, but nothing helped. What did I miss?
Here is my code:
Activity.java:
public class stream extends ActionBarActivity {
private DrawerLayout mDrawerLayout;
private ActionBarDrawerToggle mDrawerToggle;
private String[] mDrawerTitles;
private ListView mDrawerList;
private Toolbar mToolbar;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_stream);
// Init the Support-toolbar
mToolbar = (Toolbar) findViewById(R.id.toolbar);
if (mToolbar != null) {
setSupportActionBar(mToolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, mToolbar,
R.string.navigation_drawer_open, R.string.navigation_drawer_close);
mDrawerLayout.setDrawerListener(mDrawerToggle);
// enable ActionBar app icon to behave as action to toggle nav drawer
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = new MenuInflater(this);
inflater.inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if (mDrawerToggle.onOptionsItemSelected(item)) {
return true;
}
return super.onOptionsItemSelected(item);
}
#Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
mDrawerToggle.syncState();
}
#Override
public void onBackPressed() {
if(mDrawerLayout.isDrawerOpen(Gravity.START|Gravity.LEFT)){
mDrawerLayout.closeDrawers();
return;
}
super.onBackPressed();
}
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
// Pass any configuration change to the drawer toggle
mDrawerToggle.onConfigurationChanged(newConfig);
}
}
Layout.xml
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="de.square7.gtz.lwenzahn.stream"
android:background="#color/ColorBackground">
<!-- Toolbar -->
<include layout="#layout/toolbar"/>
<android.support.v4.widget.DrawerLayout
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- Content View -->
<FrameLayout
android:id="#+id/content_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingTop="56dp"/>
<!-- Drawer -->
<ListView
android:id="#+id/left_drawer"
android:layout_width="270dp"
android:layout_height="match_parent"
android:focusableInTouchMode="false"
android:choiceMode="singleChoice"
android:background="#ffffffff"
android:layout_gravity="start"
android:elevation="8dp" >
</ListView>
</android.support.v4.widget.DrawerLayout>
You are not using the DrawerLayout as the root layout of the activity. You are almost there but you see how you use the RelativeLayout as the root (or at least that is what I infer from the posted xml - the RelativeLayout does not look like it has a closing tag so maybe you just missed it)?
The NavigationDrawer opens on the tap of the "hamburger" (or icon) only when it is the root so that Android knows that the DrawerLayout is encompassing everything. My guess is the toolbar you are including is interfering with the layout.

Why my Drawer menu looks black?

I have created a Drawer Menu Example, Following is my source code,
public class DrawerActivity extends Activity
{
private String [] menuOptions = new String[] { "Option1", "Option2","Option3" };
private DrawerLayout mDrawerLayout;
private ListView mDrawerList;
private ActionBarDrawerToggle mDrawerToggle;
private ArrayAdapter<String> adapter;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.drawer);
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerList = (ListView) findViewById(R.id.left_drawer);
adapter = new ArrayAdapter<String> ( this, android.R.layout.simple_list_item_1, menuOptions );
mDrawerList.setAdapter(adapter);
mDrawerList.setOnItemClickListener(new DrawerItemClickListener());
getActionBar().setDisplayHomeAsUpEnabled(true);
getActionBar().setHomeButtonEnabled(true);
// ActionBarDrawerToggle ties together the the proper interactions
// between the sliding drawer and the action bar app icon
mDrawerToggle = new ActionBarDrawerToggle(
this, /* host Activity */
mDrawerLayout, /* DrawerLayout object */
R.drawable.ic_drawer, /* nav drawer image to replace 'Up' caret */
R.string.drawer_open, /* "open drawer" description for accessibility */
R.string.drawer_close /* "close drawer" description for accessibility */
)
{
public void onDrawerClosed(View view)
{
invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
}
public void onDrawerOpened(View drawerView)
{
invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
}
};
mDrawerLayout.setDrawerListener(mDrawerToggle);
}
/**
* When using the ActionBarDrawerToggle, you must call it during
* onPostCreate() and onConfigurationChanged()...
*/
#Override
protected void onPostCreate(Bundle savedInstanceState)
{
super.onPostCreate(savedInstanceState);
// Sync the toggle state after onRestoreInstanceState has occurred.
mDrawerToggle.syncState();
}
private class DrawerItemClickListener implements ListView.OnItemClickListener
{
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position,long arg3)
{
// update selected item and title, then close the drawer
mDrawerList.setItemChecked(position, true);
mDrawerLayout.closeDrawer(mDrawerList);
}
}
#Override
public boolean onOptionsItemSelected(MenuItem item)
{
// The action bar home/up action should open or close the drawer.
// ActionBarDrawerToggle will take care of this.
if (mDrawerToggle.onOptionsItemSelected(item))
{
return true;
}
else return false;
}
}
This is the code of my AndroidManifest.xml file
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.drawerdemo"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="21" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".DrawerActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
The code is working fine but my Drawer menu appears as black, see the image
http://i.stack.imgur.com/toEfm.png
Can you please tell me where did I done mistake ?
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<include layout="#layout/action_bar_header" />
<android.support.v4.widget.DrawerLayout
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<!-- The main content view -->
<FrameLayout
android:id="#+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<!-- The navigation drawer -->
<LinearLayout
android:id="#+id/drawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="#android:color/darker_gray"
android:orientation="vertical" >
</LinearLayout>
</android.support.v4.widget.DrawerLayout>
</LinearLayout>
Change background color of your menu layout
You can try changing the theme for your Activity.
Under your res/values folder, go to the styles.xml file and change your theme.
This can be done like so:
How to set the holo dark theme in a Android app?

Navigation Drawer is not opening when sliding from left

As the page on Android Developers says
The user can bring the navigation drawer onto the screen by swiping from the left edge of the screen or by touching the application icon on the action bar.
But strangely the navigation drawer on my activity does not respond to sliding action. It toggles only on touching the icon on the action bar. Below is my implementation of the navigation drawer
mNavigationDrawerFragment = (NavigationDrawerFragment)
getSupportFragmentManager().findFragmentById(R.id.navigation_drawer);
// Set up the drawer.
drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mNavigationDrawerFragment.setUp(
R.id.navigation_drawer,
drawerLayout);
Is there any possible explanation for this? What I doubt is my activity by default has the layout of one of its fragments. So is that the reason?
Edit: The layout file of my activity
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainPage">
<!-- The main ocntent view -->
<FrameLayout android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent">
</FrameLayout>
<!-- The navigation drawer-->
<fragment android:id="#+id/navigation_drawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:divider="#android:color/transparent"
android:name="com.example.android.handsfree.NavigationDrawerFragment"
tools:layout="#layout/fragment_navigation_drawer" />
Your ´NavigationDrawerFragment´ already creates a drawer Toggle for you inside its `setUp´ method.
You should not create a new one inside your ´MainPage´ Activity.
Notes:
You can use the android.support.v7.app.ActionBarDrawerToggle instead of the v4 one inside the NavigationDrawerFragment.
Update:
The problem seems solved now. There were 2 Issues:
The OP created a second drawer toggle in it's MainPage - Activity but there was already one created inside the NavigationDrawerFragment's setUp method, which gets called by the MainPage in order to set up the drawer. (This is basicly outsourc[ecoding]ing some of the drawer stuff to the drawer fragment.)
The OP locked the drawer inside onCreateOptionsMenu by calling a method which sets the DrawerLockMode to LOCK_MODE_LOCKED_CLOSED. He never reverted this change.
You should use:
drawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED);
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/white" >
<FrameLayout
android:id="#+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<ListView
android:id="#+id/listview_drawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="#color/drawer"
android:choiceMode="singleChoice" />
on your activity
drawlayout = (DrawerLayout) findViewById(R.id.drawer_layout);
listData = (ListView) findViewById(R.id.listview_drawer);
drawlayout.setDrawerShadow(R.drawable.drawer_shadow,
GravityCompat.START);
Add these fields in your activity class:
private ActionBarDrawerToggle mDrawerToggle;
private DrawerLayout mDrawerLayout;
Then, inside your activity:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
...
// enabling action bar app icon and behaving it as toggle button
getActionBar().setDisplayHomeAsUpEnabled(true);
getActionBar().setHomeButtonEnabled(true)
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,
R.drawable.ic_drawer, //nav menu toggle icon
R.string.app_name, // nav drawer open - description for accessibility
R.string.app_name // nav drawer close - description for accessibility
){
public void onDrawerClosed(View view) {
// calling onPrepareOptionsMenu() to show action bar icons
invalidateOptionsMenu();
}
public void onDrawerOpened(View drawerView) {
// calling onPrepareOptionsMenu() to hide action bar icons
invalidateOptionsMenu();
}
};
mDrawerLayout.setDrawerListener(mDrawerToggle);
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// toggle nav drawer on selecting action bar app icon/title
if (mDrawerToggle.onOptionsItemSelected(item)) {
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* When using the ActionBarDrawerToggle, you must call it during
* onPostCreate() and onConfigurationChanged()...
*/
#Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
// Sync the toggle state after onRestoreInstanceState has occurred.
mDrawerToggle.syncState();
}
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
// Pass any configuration change to the drawer toggls
mDrawerToggle.onConfigurationChanged(newConfig);
}
ic_drawer is a .png image which located in drawable resources. You can search this image on Google.
API level to be used is 11+.
Hope it helps for you.
1- replace the tag by :
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainPage">
<!-- The main ocntent view -->
<FrameLayout android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent">
</FrameLayout>
<!-- The navigation drawer-->
<ListView android:id="#+id/navigation_drawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:divider="#android:color/transparent" />
</android.support.v4.widget.DrawerLayout>
2- use this code in the onCreate function u can use the v4 ActionBarDrawerToggle or the v7 her i use the v4:
private DrawerLayout mDrawerLayout;
private ActionBarDrawerToggle mDrawerToggle;
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,R.drawable.ic_drawer,
R.string.app_name,R.string.app_name) {
public void onDrawerClosed(View view) {
getActionBar().setTitle(mTitle);
invalidateOptionsMenu();
}
public void onDrawerOpened(View drawerView) {
getActionBar().setTitle(mDrawerTitle);
invalidateOptionsMenu();
}
};
mDrawerLayout.setDrawerListener(mDrawerToggle);

DrawerToggle is not set (or not visible)

I want to use a NavigationDrawer inside a ListActivity, so this is basically my Layout:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<ListView
android:id="#+id/left_drawer"
android:layout_width="265dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="#232833"
android:choiceMode="singleChoice" />
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#android:id/list"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:divider="#android:color/transparent"
android:dividerHeight="10dp"
android:choiceMode="singleChoice">
</ListView>
</android.support.v4.widget.DrawerLayout>
After initializing the Drawer no errors occur, however, there's no DrawerToggle in the ActionBar. What could be a possible reason? ActionBar and Drawer setup:
private void initDrawer(){
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,
R.drawable.ic_drawer, R.string.dr_open, R.string.dr_close) {
public void onDrawerClosed(View view) {
//TODO
}
public void onDrawerOpened(View drawerView) {
//TODO
}
};
mDrawerLayout.setDrawerListener(mDrawerToggle);
mDrawerToggle.syncState();
lvDrawerList = (ListView) findViewById(R.id.left_drawer);
lvDrawerList.setDivider(getResources().getDrawable(
R.drawable.drawer_list_divider));
lvDrawerList.setDividerHeight(4);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, DRAWER_ITEMS);
lvDrawerList.setAdapter(adapter);
}
private void initActionBar(){
ab = getActionBar();
ab.setTitle("News");
ab.setIcon(getResources().getDrawable(R.drawable.logo));
ab.setBackgroundDrawable(getResources().getDrawable(R.drawable.ab_main_bg));
}
UPDATE I figured out that I actually can open the Drawer with a swipe gesture, the Toggle is still missing from the ActionBar however
First, the mDrawerToggle.syncState() is called in the wrong method. You should call it from onPostCreate
#Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
// Sync the toggle state after onRestoreInstanceState has occurred.
mDrawerToggle.syncState();
}
You should definitively take a look at the official documentation.
I figured it out. I had to use ActionBar.setDisplayHomeAsUpEnabled(true) to get the DrawerToggle working. Maybe this answer will help someone who's experiencing the same issue.

DrawerLayout Double Drawer (Left and Right Drawers simultaneously)

I have an application, in which i want to implement a double drawer - one from the left and one from the right. Left drawer is for app navigation, right drawer is for result filtering.
So, the layout is like this:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/light_grey"
android:orientation="vertical">
<GridView
android:id="#+id/gridview"
style="#style/GridViewStyle"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:horizontalSpacing="7dp"
android:stretchMode="columnWidth"
android:verticalSpacing="7dp" />
</LinearLayout>
<ListView
android:id="#+id/left_drawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="#111"
android:choiceMode="singleChoice"
android:divider="#android:color/transparent"
android:dividerHeight="0dp" />
<ListView
android:id="#+id/right_drawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="end"
android:background="#111"
android:choiceMode="singleChoice"
android:divider="#android:color/transparent"
android:dividerHeight="0dp" />
</android.support.v4.widget.DrawerLayout>
You can clearly see here "left_drawer" and "right_drawer", and their respective gravity - "start" and "end"
And this actually works! You can pull them both out.
The problem is, when i implement the DrawerToggle - it only opens the left drawer, and does not close the right one, so if the right drawer is opened and i press the DrawerToggle button - the left drawers opens ALSO, and overlaps the right drawer.
There are a couple of solutions i'am trying to get:
Make the same DrawerToggle button on the right side, with the same behavior and animation as the left side.
Make the drawer on the opposite side of the drawer i am trying to open - automatically close (if the left drawer is open and i press the toggle of the right drawer and vise-versa).
And i haven't figured how to do that, because DrawerToggle accepts the DrawerLayout itself as a parameter, and not the individual drawers...
I am using the Support Library.
Anyone have any ideas?
Thank you in advance.
Here is the code for a Double Drawer Activity than can be extended by other activities to implement the double drawer, assuming they have a layout like the one propposed by OP.
public class DoubleDrawerActivity extends ActionBarActivity {
private DrawerLayout mDrawerLayout;
private ActionBarDrawerToggle mDrawerToggle;
private View mLeftDrawerView;
private View mRightDrawerView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
}
#Override
protected void onStart() {
super.onStart();
if(mDrawerLayout == null || mLeftDrawerView == null || mRightDrawerView == null || mDrawerToggle == null) {
// Configure navigation drawer
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mLeftDrawerView = findViewById(R.id.left_drawer);
mRightDrawerView = findViewById(R.id.right_drawer);
mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, R.drawable.ic_navigation_drawer, R.string.drawer_open, R.string.drawer_close) {
/** Called when a drawer has settled in a completely closed state. */
public void onDrawerClosed(View drawerView) {
if(drawerView.equals(mLeftDrawerView)) {
getSupportActionBar().setTitle(getTitle());
supportInvalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
mDrawerToggle.syncState();
}
}
/** Called when a drawer has settled in a completely open state. */
public void onDrawerOpened(View drawerView) {
if(drawerView.equals(mLeftDrawerView)) {
getSupportActionBar().setTitle(getString(R.string.app_name));
supportInvalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
mDrawerToggle.syncState();
}
}
#Override
public void onDrawerSlide(View drawerView, float slideOffset) {
// Avoid normal indicator glyph behaviour. This is to avoid glyph movement when opening the right drawer
//super.onDrawerSlide(drawerView, slideOffset);
}
};
mDrawerLayout.setDrawerListener(mDrawerToggle); // Set the drawer toggle as the DrawerListener
}
}
#Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
// Sync the toggle state after onRestoreInstanceState has occurred.
mDrawerToggle.syncState();
}
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
mDrawerToggle.onConfigurationChanged(newConfig);
}
#Override
public boolean onPrepareOptionsMenu(Menu menu) {
// If the nav drawer is open, hide action items related to the content view
for(int i = 0; i< menu.size(); i++)
menu.getItem(i).setVisible(!mDrawerLayout.isDrawerOpen(mLeftDrawerView));
return super.onPrepareOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId()) {
case android.R.id.home:
mDrawerToggle.onOptionsItemSelected(item);
if(mDrawerLayout.isDrawerOpen(mRightDrawerView))
mDrawerLayout.closeDrawer(mRightDrawerView);
return true;
}
return super.onOptionsItemSelected(item);
}
}
You can call it like this in a ToggleButton's handler for example :
mDrawerLayout.openDrawer(mDrawer);
mDrawerLayout.closeDrawer(mDrawer);
Where mDrawer is a reference to the specific drawer you need to open (be it a view or a layout), in your case, the actual ListView you wish to display.
You can use NavigationView from material design.
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start">
<include
layout="#layout/app_bar_main"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<android.support.design.widget.NavigationView
android:id="#+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:headerLayout="#layout/nav_header_main"
app:menu="#menu/activity_main_drawer" />
<android.support.design.widget.NavigationView
android:id="#+id/nav_view2"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="end"
android:fitsSystemWindows="true"
app:headerLayout="#layout/nav_header_main"
app:menu="#menu/activity_main_drawer1" />
</android.support.v4.widget.DrawerLayout>
Here is my short solution for all who want to prevent the animation of drawer indicator if they swipe the right view. Simply implement the onDrawerSlide Method like this.
mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, R.drawable.ic_drawer_white, 0, 0) {
#Override
public void onDrawerClosed(View view) {
invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
}
#Override
public void onDrawerOpened(View drawerView) {
invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
}
#Override
public void onDrawerSlide(View drawerView, float slideOffset) {
if (drawerView == mSlidingMenuNavigationList) {
super.onDrawerSlide(drawerView, slideOffset);
}
else {
// do nothing on all other views
}
}
};
Use the gravity constant (Gravity.LEFT or Gravity.RIGHT) of whatever drawer you want to close (as you open the other one) in onOptionsItemSelected() as shown below.
public boolean onOptionsItemSelected(MenuItem item) {
if (mDrawerToggle.onOptionsItemSelected(item)) {
// Close the right side drawer if visible
if(mDrawerLayout.isDrawerVisible(Gravity.RIGHT)) {
mDrawerLayout.closeDrawer(Gravity.RIGHT);
}
return true;
}
// Regular stuff
switch (item.getItemId()) {
case R.id.action_example:
Toast.makeText(getActivity(), "Example action.", Toast.LENGTH_SHORT).show();
return true;
}
return super.onOptionsItemSelected(item);
}
mDrawerToggle = Listener object implementing DrawerLayout.DrawerListener
See: http://developer.android.com/reference/android/support/v4/app/ActionBarDrawerToggle.html
I have solved adding this code in the onOptionsItemSelected method:
switch (item.getItemId()) {
case android.R.id.home:
if (mDrawerLayout.isDrawerOpen(mDrawerList_right)){
mDrawerLayout.closeDrawer(mDrawerList_right);
}
mDrawerLayout.openDrawer(mDrawerList_left);
}
break;
case R.id.action_drawer:
if (mDrawerLayout.isDrawerOpen(mDrawerList_left)){
mDrawerLayout.closeDrawer(mDrawerList_left);
}
mDrawerLayout.openDrawer(mDrawerList_right);
}
default:
break;
}
I have added an action button and overrided the home button of the actionbar
make a custom item and add it the right, pass to it the right drawer.
final ToggleButton ic_nav = (ToggleButton) customNav.findViewById(R.id.ic_nav);
ic_nav.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View arg0)
{
if ( mDrawerLayout.isDrawerOpen(mDrawerList) && arg0.isSelected()) {
mDrawerLayout.closeDrawer(mDrawerList);
arg0.setSelected(false);
}
else if (!mDrawerLayout.isDrawerOpen(mDrawerList) && !arg0.isSelected()){
mDrawerLayout.openDrawer(mDrawerList);
ic_nav.setSelected(false);
arg0.setSelected(true);
}
}
});
You can show two navigation views to the same drawer as:
<androidx.drawerlayout.widget.DrawerLayout
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="false">
<com.google.android.material.navigation.NavigationView
android:id="#+id/NAVIGATION_VIEW_LEFT"
android:layout_width="270dp"
android:layout_height="match_parent"
android:layout_gravity="start"/>
<com.google.android.material.navigation.NavigationView
android:id="#+id/NAVIGATION_VIEW_RIGHT"
android:layout_width="200dp"
android:layout_height="match_parent"
android:layout_gravity="right"/>
</androidx.drawerlayout.widget.DrawerLayout>
And for activity do this:
actionBarDrawerToggle = new ActionBarDrawerToggle(context, drawerLayout, R.string.nav_open, R.string.nav_close);
// pass the Open and Close toggle for the drawer layout listener
// to toggle the button
drawerLayout.addDrawerListener(actionBarDrawerToggle);
actionBarDrawerToggle.syncState();
nav_drawer_left_Iv.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
drawerLayout.openDrawer(GravityCompat.START);
}
});
nav_drawer_right_Iv.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
drawerLayout.openDrawer(GravityCompat.END);
}
});

Categories

Resources