0

In my xamarin android app I have a right navigation bar and a custom toolbar. The code for navigation bar is this

<android.support.design.widget.NavigationView
 android:layout_width="wrap_content"
 android:layout_height="match_parent"
 android:layout_gravity="right"
 android:id="@+id/right_nav_view"
 app:menu="@menu/menu_navigation_swipe" />

And the code for toolbar is this

<android.support.v7.widget.Toolbar
 android:id="@+id/toolbar"
 android:layout_width="match_parent"
 android:layout_height="45dp"
 android:minHeight="?attr/actionBarSize"
 android:background="@drawable/rectangle"
 android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
 app:popupTheme="@style/ThemeOverlay.AppCompat.Light">
  <RelativeLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">
    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/HomeButton"
        android:layout_gravity="center"
        android:src="@drawable/logo"
        android:clickable="true" />

  <ImageView
          android:padding="10dp"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:id="@+id/right_nav"
         android:layout_alignParentEnd="true"
         android:layout_alignParentRight="true"
         android:src="@drawable/icon_settings"
         android:clickable="true" />

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

I wanna open the navigation bar when the user press the ImageView with id = right_nav. How can i do this?

3 Answers3

4

Xamarin android. Open right navigation bar with button from custom toolbar

Place your NavigationView into a DrawerLayout, for example:

<?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"
    android:id="@+id/drawer_layout"
    android:layout_height="match_parent"
    android:layout_width="match_parent"
    android:fitsSystemWindows="true">

  <!-- I place your Toolbar in this include_list_viewpager layout -->
  <include layout="@layout/include_list_viewpager"/>

  <android.support.design.widget.NavigationView
        android:id="@+id/nav_view"
        android:layout_height="match_parent"
        android:layout_width="wrap_content"
        android:layout_gravity="right"
        android:fitsSystemWindows="true"
        app:headerLayout="@layout/nav_header"
        app:menu="@menu/drawer_view"/>

</android.support.v4.widget.DrawerLayout>

Then, as @Federico Navarrete has pointed out, define a click event for your ImageView:

DrawerLayout drawerLayout = FindViewById<DrawerLayout> (Resource.Id.drawer_layout);

ImageView right_nav = FindViewById<ImageView>(Resource.Id.right_nav);

right_nav.Click += (s, e) =>
{
     drawerLayout.OpenDrawer(Android.Support.V4.View.GravityCompat.End);
};
York Shen
  • 9,014
  • 1
  • 16
  • 40
0

Try This

ImageView right_nav = (ImageView)findViewById(R.id.right_nav);
        right_nav.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                drawer.openDrawer(Gravity.RIGHT);
            }
        });
Siddharth Patel
  • 205
  • 1
  • 13
0

You can use this code to open it from the right side:

//You get your navigation drawer
var drawer = FindViewById<DrawerLayout>(Resource.Id.right_nav_view);
//You get the image view
var right_nav = FindViewById<ImageView>(Resource.Id.right_nav);

//You define the click event
right_nav.Click += (s, e) =>
{
    //You open the navigation Drawer from the right
    drawer.OpenDrawer(GravityCompat.Right);
}
Federico Navarrete
  • 3,069
  • 5
  • 41
  • 76