Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set NavigationDrawer icon right side using AppCompat-V-21 Toolbar Widget

I've been working in material design using the latest Toolbar Widget provided in AppCompact-V21, with the navigation drawer.

My concern is How can I set navigation drawer icon to right side of the toolbar. Although I can open navigation drawer from right by setting the android:layout_gravity="right" but navigation icon still appears to be on left side in toolbar.

When Using old Action Bar, I can crate custom action bar with button to right side with custom drawer icon and on click of custom navigation drawer icon open/close the drawer.

  1. What is way to set navigation drawer icon on right side using toolbar ?
  2. How to create custom view for it ?
like image 263
Dory Avatar asked Dec 10 '14 06:12

Dory


People also ask

How to add navigation drawer icon in toolbar android?

The drawer icon is displayed on all top-level destinations that use a DrawerLayout . To add a navigation drawer, first declare a DrawerLayout as the root view. Inside the DrawerLayout , add a layout for the main UI content and another view that contains the contents of the navigation drawer.

How to use navigation drawer in android app?

The user can view the navigation drawer when the user swipes a finger from the left edge of the activity. They can also find it from the home activity by tapping the app icon in the action bar. The drawer icon is displayed on all top-level destinations that use a DrawerLayout.


2 Answers

Well, to customize toolbar and setting your own navigation button, you can add your own imagebutton to the toolbar :

<android.support.v7.widget.Toolbar
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/toolbar"
    android:minHeight="?attr/actionBarSize"
    android:layout_height="?attr/actionBarSize"
    android:layout_width="match_parent" >

    <ImageButton
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:layout_gravity="end"
       android:onClick="backButtonHandler"
       android:background="@drawable/ic_launcher" />

</android.support.v7.widget.Toolbar>

Then, you can define your backButtonHandler(View v) in your activity class, and add functionality as desired.

like image 134
Abhinav Puri Avatar answered Sep 28 '22 07:09

Abhinav Puri


I want to move ActionBarDrawerToggle from left to right and I am using appcompat 21 support library. I haven't found a solution for this. It seems that we need to create a custom item in actionbar/toolbar as Abhinav Puri suggested.

You said you don't know how to add a custom view to your toolbar, hope this helps you or anybody who is fighting with this problem:

		toolbar = (Toolbar) findViewById(R.id.toolbar);

		if (toolbar != null) {
			setSupportActionBar(toolbar);
		}

		actionBar = getSupportActionBar();
		actionBar.setDisplayHomeAsUpEnabled(true);
		actionBar.setHomeButtonEnabled(true);
		actionBar.setDisplayShowHomeEnabled(true);
		actionBar.setDisplayShowCustomEnabled(true);

		ActionBar.LayoutParams layoutParams = new ActionBar.LayoutParams(
				LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT);
		layoutParams.gravity = Gravity.RIGHT;
		actionBar.setCustomView(
				getLayoutInflater().inflate(R.layout.custom_top_bar, null),
				layoutParams);
		
		ImageButton rightToggle =(ImageButton) toolbar.findViewById(R.id.rightToggle);
		
		rightToggle.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				if (mDrawerLayout.isDrawerOpen(Gravity.RIGHT)) {
					mDrawerLayout.closeDrawer(Gravity.RIGHT);
				} else {
					mDrawerLayout.openDrawer(Gravity.RIGHT);
				}
			}
		});

custom_top_bar.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/rightLayout"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:orientation="horizontal" >

    <ImageButton
        android:id="@+id/rightToggle"
        android:layout_width="?attr/actionBarSize"
        android:layout_height="?attr/actionBarSize"
        android:src="@drawable/ic_launcher" />

</LinearLayout>
like image 36
Kinga l Avatar answered Sep 28 '22 08:09

Kinga l