Xamarin Forms custom toolbar (Android)

EmilMller.4175EmilMller.4175 NOMember ✭✭
edited June 2017 in Xamarin.Forms

Hello

I have to implement a custom toolbar with some dynamic text (only for Android). I have to set this text when the Page first initializes.
My first idea was to edit Toolbar.axml. I added this:

<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">
  <LinearLayout
    android:id="@+id/alternateTitle"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" android:orientation="vertical">
    <TextView
      android:id="@+id/alternateTitle1"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:textAppearance="?android:attr/textAppearanceLarge" />
    <TextView
      android:id="@+id/alternateTitle2"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content" />
  </LinearLayout>
</android.support.v7.widget.Toolbar>

So I thought I would hide it for all the pages that wasn't using it (i.e. hide alternateTitle), and to show it for the pages that uses it.

I tried doing this in the constructor, like this:

#if __ANDROID__
            Droid.MainActivity._singleton.ShowAlternateTitle("foo1", "foo2");
#endif

And in MainActivity.cs:

        public void ShowAlternateTitle(string title1, string title2)
        {
            FindViewById<LinearLayout>(Resource.Id.alternateTitle).Visibility = ViewStates.Visible;

            FindViewById<TextView>(Resource.Id.alternateTitle1).Text = title1;
            FindViewById<TextView>(Resource.Id.alternateTitle2).Text = title1;
        }

But that didn't work because the element hadn't been added yet. So I also tried doing it by overriding OnAppearing in Page. Same error :(

Anybody have any idea on how I can achieve this? :) Thanks!

Edit: I guess it's the same problem as here: https://forums.xamarin.com/discussion/22561/page-loaded-event

Answers

Sign In or Register to comment.