Hopeless Hopeless - 2 months ago 35
Android Question

AppCompatActivity with the new Toolbar having Home button not shown correctly?

This question involves Xamarin Android and using C#, but I think Java developer using Android Studio ... can still understand the problem and may help.

As I have researched on the net, everything should be simple. The problem here is the

Toolbar
(in v7 support library) shows at the top expectedly but the Home button does not show as what I want.
I have a
DrawerLayout
in the main layout, clicking on the Home button should expand the drawer layout and together with that the Home icon should switch to the left arrow icon (back icon).
Clicking the back button should collapse the drawer layout and switch the icon back to the Home's initial icon.

What I had now is the Home button always shows the left arrow icon. I think that's because I use the call
SupportActionBar.SetDisplayHomeAsUpEnabled(true);

But without setting like that there is not anything shown, so there is nothing to click on.

Here is the code resulting in a working Home button but its Icon always shows the left arrow:

The Toolbar layout:

<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:id="@+id/mainToolbar"
android:background="?attr/colorPrimary"
android:layout_height="@dimen/ToolbarHeight">
</android.support.v7.widget.Toolbar>


The code behind:

//here is in OnCreate callback of the main Activity
var toolBar = FindViewById<Toolbar>(Resource.Id.mainToolbar);
SetSupportActionBar(toolBar);
SupportActionBar.SetDisplayShowHomeEnabled(true);
SupportActionBar.SetDisplayHomeAsUpEnabled(true);
SupportActionBar.SetHomeButtonEnabled(true);


With the above code, if I commment out the line
SupportActionBar.SetDisplayHomeAsUpEnabled(true);
then there is nothing shown (while I want some Icon such as the
app icon to be shown).

I hope someone here could figure out what I miss here.
BTW I run the code on Android 4.4.4, although I think this is not the problem of Android version (because the support library should work in this case).

UPDATE:
The main layout:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/rootFrame"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<android.support.v4.widget.DrawerLayout
android:id="@+id/main"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<!-- main content -->
<LinearLayout
android:id="@+id/mainContent"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<include layout="@layout/MainToolBar"/>
<FrameLayout android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:id="@+id/contentFrame"></FrameLayout>
</LinearLayout>

<!-- left menu -->
<android.support.design.widget.NavigationView
android:layout_height="fill_parent"
android:layout_width="wrap_content"
android:minWidth="@dimen/LeftMenuMinWidth"
android:layout_gravity="start"
android:fitsSystemWindows="false"
android:background="@color/LeftMenuBackgroundColor"
android:id="@+id/leftMenu">
<!-- layout for the menu here ... -->
</android.support.design.widget.NavigationView>
</android.support.v4.widget.DrawerLayout>
</LinearLayout>


Code to setup the drawer listener, this is placed after the code setting the toolBar I posted above:

var dl = FindViewById<DrawerLayout>(Resource.Id.main);
//the ActionBarDrawerToggle here is from Android.Support.V7.app
dl.AddDrawerListener(new ActionBarDrawerToggle(this, dl, toolBar, 0, 0));


I hope now I have provided enough code.

Answer

When using an ActionBarDrawerToggle, you need to call SyncState() on it after initialization to cause it to actually set its own icon on the navigation button.

It's also recommended to do this in the Activity's OnPostCreate() method, so it can sync correctly if, for example, the drawer is open during a device rotation.