Ashwin
Ashwin

Reputation: 13547

Navigation Drawer Icon not visible with custom action bar?

I am trying to use navigation drawer with custom action bar. My action bar does not contain any menu.
I see that the Navigation Drawer icon, is not visible on the action bar when the drawer is closed. Even though I have given the drawer icon in the ActionbarToggle implementation. The drawer icon appears when the navigation drawer is open, but disappears when the navigation drawer closes.

Activty

 public void setCustomActionBar() {
        ActionBar actionBar = getActionBar();
        actionBar.setCustomView(R.layout.action_bar);
}

NavigationDrawer Fragment

getActivity(),                    /* host Activity */
        mDrawerLayout,                    /* DrawerLayout object */
        R.drawable.drawer,             /* nav drawer image to replace 'Up' caret */
        R.string.navigation_drawer_open,  /* "open drawer" description for accessibility */
        R.string.navigation_drawer_close  /* "close drawer" description for accessibility */
) {...

As you can see I have provided the drawer icon above. Why is this happening and what is the solution to this?

Upvotes: 0

Views: 1254

Answers (1)

Khushal Chouhan
Khushal Chouhan

Reputation: 581

I am assuming that you are creating your own custom layout.Suppose I have this xml layout given below:

<?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="horizontal" >

<FrameLayout 
    android:layout_width="match_parent"
    android:layout_height="45dp"
    android:background="#cccccc">
    <Button 
        android:layout_width="10dp"
        android:id="@+id/drawer_toggle"
        android:layout_height="40dp"
        android:background="#000000"
        android:layout_margin="10dp"
        android:layout_gravity="center_vertical|start"/>
    </FrameLayout>
</LinearLayout>

Then you can do this:

Button toggle = (Button) yourDrawerView.findViewById(R.id.drawer_toggle);   
toggle.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                boolean isDrawerOpen = yourDrawerLayoutRefference.isDrawerOpen(yourDrawerLayout);
                if(isDrawerOpen){
                         toggle.closeDrawer(yourDrawerLayout);
                       }
                else{
                       toggle.openDrawer(yourDrawerLayout);
                    }


            }
        }); 

Upvotes: 1

Related Questions