0

My question is similar to this question Android - Switch ActionBar Back Button to Navigation Button, but I have problems using the Java code that I changed to c#.

import android.support.v7.app.ActionBarDrawerToggle
import android.support.v4.widget.DrawerLayout

ActionBarDrawerToggle mDrawerToggle;
DrawerLayout drawerLayout;
private boolean mToolBarNavigationListenerIsRegistered = false;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setSupportActionBar(mToolbar);
    getSupportActionBar().setDisplayShowTitleEnabled(false);
    // Get DrawerLayout ref from layout
    drawerLayout = (DrawerLayout)findViewById(R.id.drawer);
    // Initialize ActionBarDrawerToggle, which will control toggle of hamburger.
    // You set the values of R.string.open and R.string.close accordingly.
    // Also, you can implement drawer toggle listener if you want.
    mDrawerToggle = new ActionBarDrawerToggle (this, drawerLayout, mToolbar, R.string.open, R.string.close);
    // Setting the actionbarToggle to drawer layout
    drawerLayout.addDrawerListener(mDrawerToggle);
    // Calling sync state is necessary to show your hamburger icon...
    // or so I hear. Doesn't hurt including it even if you find it works
    // without it on your test device(s)
    mDrawerToggle.syncState();
}

/**
 * To be semantically or contextually correct, maybe change the name
 * and signature of this function to something like:
 *
 * private void showBackButton(boolean show)
 * Just a suggestion.
 */
 private void enableViews(boolean enable) {

    // To keep states of ActionBar and ActionBarDrawerToggle synchronized,
    // when you enable on one, you disable on the other.
    // And as you may notice, the order for this operation is disable first, then enable - VERY VERY IMPORTANT.
    if(enable) {
        //You may not want to open the drawer on swipe from the left in this case  
        drawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);
        // Remove hamburger
        mDrawerToggle.setDrawerIndicatorEnabled(false);
        // Show back button
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        // when DrawerToggle is disabled i.e. setDrawerIndicatorEnabled(false), navigation icon
        // clicks are disabled i.e. the UP button will not work.
        // We need to add a listener, as in below, so DrawerToggle will forward
        // click events to this listener.
        if(!mToolBarNavigationListenerIsRegistered) {
            mDrawerToggle.setToolbarNavigationClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    // Doesn't have to be onBackPressed
                    onBackPressed();
                }
            });

            mToolBarNavigationListenerIsRegistered = true;
        }

    } else {
        //You must regain the power of swipe for the drawer. 
        drawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED);

        // Remove back button
        getSupportActionBar().setDisplayHomeAsUpEnabled(false);
        // Show hamburger 
        mDrawerToggle.setDrawerIndicatorEnabled(true);
        // Remove the/any drawer toggle listener
        mDrawerToggle.setToolbarNavigationClickListener(null);
        mToolBarNavigationListenerIsRegistered = false;
    }

    // So, one may think "Hmm why not simplify to:
    // .....
    // getSupportActionBar().setDisplayHomeAsUpEnabled(enable);
    // mDrawer.setDrawerIndicatorEnabled(!enable);
    // ......
    // To re-iterate, the order in which you enable and disable views IS important #dontSimplify.
}

So my question is how to change the button navigation click listener, from the hambuger to open the navigation drawer to the back button which functions to close the activity. is it possible to use ToolbarNavigationClickListener like the java code, if so, how?

Edwin Sulaiman
  • 639
  • 9
  • 14
  • Instead of trying to convert code from Java to c#, what are you trying to accomplish? Maybe an easier approch exists. What do you mean by the code is wrong? – Cfun Nov 01 '20 at 12:01
  • @Cfun in the error list there is a message ````There is no argument given that corresponds to the required formal parameter 'v' of 'View.IOnClickListener.OnClick(View?)'```` i was try to add ````this```` for the parameter like as i used it on ````navigationView.SetNavigationItemSelectedListener(this);````. what I want to achieve is how to change the click listener on the navigation button which initially displays the drawerlayout to replace the fragment. The question is how to use the method to change the ````onClickListener````? – Edwin Sulaiman Nov 01 '20 at 12:24

1 Answers1

0

With the link and the description you provided, it seems you want to change the back button to the hamburger icon and have the click event for this hamburger.

In Xamarin.Android, you could use ActionBarDrawerToggle. Create your own MyActionBarDrawerToggle.

    internal class MyActionBarDrawerToggle : ActionBarDrawerToggle
    {
        NavigationDrawerActivity owner;

        public MyActionBarDrawerToggle (NavigationDrawerActivity activity, DrawerLayout layout, int imgRes, int openRes, int closeRes)
            : base (activity, layout, imgRes, openRes, closeRes)
        {
            owner = activity;
        }

        public override void OnDrawerClosed (View drawerView)
        {
            owner.ActionBar.Title = owner.Title;
            owner.InvalidateOptionsMenu ();
        }

        public override void OnDrawerOpened (View drawerView)
        {
            owner.ActionBar.Title = owner.mDrawerTitle;
            owner.InvalidateOptionsMenu ();
        }
    }

And then set the mDrawerToggle for this layout.

    mDrawerToggle = new MyActionBarDrawerToggle (this, mDrawerLayout,
            Resource.Drawable.ic_drawer, 
            Resource.String.drawer_open, 
            Resource.String.drawer_close);

        mDrawerLayout.SetDrawerListener (mDrawerToggle);
        if (savedInstanceState == null) //first launch
            selectItem (0);

Create the click listener for RecyclerView.

public interface OnItemClickListener{
        void OnClick(View view, int position);
    }

And then implement it in layout.

    public void OnClick (View view, int position)
    {
        selectItem (position);
    }

Screenshot:

enter image description here

You could download the source file from the link for reference.

https://learn.microsoft.com/en-us/samples/xamarin/monodroid-samples/android50-navigationdrawer/

Wendy Zang - MSFT
  • 10,509
  • 1
  • 7
  • 17