toolbar = FindViewById(Resource.Id.toolbar); toolbar is always null

LeoJHarrisLeoJHarris NZMember ✭✭✭
edited April 2018 in Xamarin.Android

Hi all,

I am trying to call this

Android.Support.V7.Widget.Toolbar toolbar = this.FindViewById<Android.Support.V7.Widget.Toolbar>(Resource.Id.toolbar);
SetSupportActionBar(toolbar);

but toolbar is always null??

My MainActivity.cs:

 public class MainActivity : FormsAppCompatActivity
    {
        protected override void OnCreate(Bundle savedInstanceState)
        {
            TabLayoutResource = Resource.Layout.Tabbar;
            ToolbarResource = Resource.Layout.Toolbar;

            base.SetTheme(Resource.Style.MainTheme);

            base.OnCreate(savedInstanceState);

            Forms.SetFlags("FastRenderers_Experimental");
            Forms.Init(this, savedInstanceState);

            LoadApplication(new App());

            Android.Support.V7.Widget.Toolbar toolbar
               = this.FindViewById<Android.Support.V7.Widget.Toolbar>(Resource.Id.toolbar);
            SetSupportActionBar(toolbar);
        }

I have the Toolbar.axml that xamarin convenietly generates for me upon project creation:

<android.support.v7.widget.Toolbar
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="?attr/colorPrimary"
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
    android:popupTheme="@style/ThemeOverlay.AppCompat.Light" />

Also in my styles.xml my base theme is using:

<style name="MainTheme.Base" parent="Theme.AppCompat.Light.DarkActionBar">
    <!--If you are using revision 22.1 please use just windowNoTitle. Without android:-->
    <item name="windowNoTitle">true</item>
    <!--We will be using the toolbar so no need to show ActionBar-->
    <item name="windowActionBar">false</item>
....
</style>

I need to override the back navigation for android. Anyone had issues getting the toolbar before?

I am using the latest Xamarin.Android support libraries 27.0.2 and XF's v2.5.1.444934.

Best Answer

  • LeoJHarrisLeoJHarris NZ ✭✭✭
    edited May 2018 Accepted Answer

    Got this working by setting the mainpage of the app as a NavigationPage or something extending this class. I did need to call below code as sometimes when I was getting the toolbar in the onCreate it was also returning null.

        protected override void OnResume()
                {
                    Android.Support.V7.Widget.Toolbar toolbar = this.FindViewById<Android.Support.V7.Widget.Toolbar>(Resource.Id.toolbar);
                    if (toolbar != null)
                        SetSupportActionBar(toolbar);
                }
    

Answers

  • munaalzeermunaalzeer JOMember ✭✭
    edited April 2018

    Before finding element by Id it need to be placed within your MainActivity layout ....you need to include the toolbar layout within your main layout .axml as follows :

    <include

    android:id="@+id/toolbar"

    layout="@layout/toolbr" />

  • Lang_tu_bi_dienLang_tu_bi_dien VNMember ✭✭

    Can you post code xml on manifest.

  • LeoJHarrisLeoJHarris NZMember ✭✭✭
    edited April 2018

    @Lang_tu_bi_dien

    manifest xml below:

    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
              android:versionCode="3"
              android:versionName="1.2"
              package="PackageName"
              android:installLocation="auto">
      <uses-sdk android:minSdkVersion="15" />
      <application android:label="App name" android:theme="@style/SplashTheme"></application>
    
      <activity android:name=".activities.MainActivity">
        <intent-filter>
          <action android:name="android.intent.action.MAIN" />
          <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
      </activity>
    </manifest>
    

    I have a Style in my styles for the splash:

     <style name="SplashTheme" parent="MainTheme.Base">
        <item name="android:windowBackground">@drawable/splash</item>
      </style>
    

    And also have a splash.xml

    <?xml version="1.0" encoding="utf-8"?>
    <layer-list xmlns:android="http://schemas.android.com/apk/res/android" android:opacity="opaque">
      <item>
        <shape android:shape="rectangle" >
          <solid android:color="#262626" />
        </shape>
      </item>
      <item>
        <bitmap
          android:gravity="center"
          android:src="@drawable/logo"/>
      </item>
    </layer-list>
    
  • LeoJHarrisLeoJHarris NZMember ✭✭✭
    edited April 2018

    @Lang_tu_bi_dien

    Attach my project and stripped out irrelevant files and code.

  • LeoJHarrisLeoJHarris NZMember ✭✭✭
    edited May 2018 Accepted Answer

    Got this working by setting the mainpage of the app as a NavigationPage or something extending this class. I did need to call below code as sometimes when I was getting the toolbar in the onCreate it was also returning null.

        protected override void OnResume()
                {
                    Android.Support.V7.Widget.Toolbar toolbar = this.FindViewById<Android.Support.V7.Widget.Toolbar>(Resource.Id.toolbar);
                    if (toolbar != null)
                        SetSupportActionBar(toolbar);
                }
    
Sign In or Register to comment.