2

I created a NavigationDrawerActivity in android studio . But Now i dont need to toolbar. so i delete toolbar from appa-bar_main.xml :

<?xml version="1.0" encoding="utf-8"?>

<android.support.design.widget.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/AppTheme.AppBarOverlay">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:background="?attr/colorPrimary"
        app:popupTheme="@style/AppTheme.PopupOverlay"
        android:layout_width="match_parent"
        android:layout_height="56dp" />

</android.support.design.widget.AppBarLayout>

<include layout="@layout/content_main" />

But I need a toggle Button on the top_left of Screen to show NavigationDrawer. Because i deleted toolbar,i cant use below code:

  ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
            this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
    drawer.setDrawerListener(toggle);
    toggle.syncState();

Now, how i can add a toggle Button to my activity?

Paeez
  • 21
  • 1
  • 2

2 Answers2

1

You can use almost any clickable component to control the Drawer, a button for example. You can create a custom component that descents from Button and that implements DrawerLayout.DrawerListener interface, for example:

package com.test.view;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.support.v4.widget.DrawerLayout;
import com.test.MainActivity;

public class CustomDrawerButton extends Button implements DrawerLayout.DrawerListener {

    private DrawerLayout mDrawerLayout;
    private int side = Gravity.LEFT;

    public CustomDrawerButton(Context context) {
        super(context);
    }
    public CustomDrawerButton(Context context, AttributeSet attrs) {
        super(context, attrs);
    }
    public CustomDrawerButton(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public void changeState(){
        if ( mDrawerLayout.isDrawerOpen( side ) ){
            mDrawerLayout.closeDrawer( side );
        }else{
            mDrawerLayout.openDrawer( side );
        }
    }

    @Override
    public void onDrawerSlide(View drawerView, float slideOffset) {
        Log.e("BUTTOM DRAWER: ", "onDrawerSlide");
    }
    @Override
    public void onDrawerOpened(View drawerView) {
        Log.e("BUTTOM DRAWER: ", "onDrawerOpened");
        setText("Close\ndrawer");
    }
    @Override
     public void onDrawerClosed(View drawerView) {
        Log.e("BUTTOM DRAWER: ", "onDrawerClosed");
        setText("Open\ndrawer");
    }
    @Override
    public void onDrawerStateChanged(int newState) {
         Log.e("BUTTOM DRAWER: ", "onDrawerStateChanged");
    }

    public DrawerLayout getDrawerLayout() {
        return mDrawerLayout;
    }
     public CustomDrawerButton setDrawerLayout(DrawerLayout mDrawerLayout) {
        this.mDrawerLayout = mDrawerLayout;
        return this;
    }
}

You can include this component in the layout of your fragment:

<com.test.view.CustomDrawerButton
    android:id="@+id/btnOpenDrawer"
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_alignParentRight="true"
    android:text="Open\ndrawer"/>

And in the Fragment or Activity that contains your CustomDrawerButton:

customDrawerButton = (CustomDrawerButton)view.findViewById(R.id.btnOpenDrawer);
customDrawerButton.setDrawerLayout( mDrawer );
customDrawerButton.getDrawerLayout().addDrawerListener( customDrawerButton );
    customDrawerButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            customDrawerButton.changeState();
        }
    });
Rodia
  • 1,407
  • 8
  • 22
  • 29
Jesús Barrera
  • 400
  • 1
  • 5
  • 15
0

Try to set the height and the width of the action bar =0dp from layout app_bar_.. so it will not effect on the code but will remove the action bar like this

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:background="?attr/colorPrimary"
            app:popupTheme="@style/AppTheme.PopupOverlay" />
Omar Sakr
  • 11
  • 1
  • 1