Forum Xamarin.Android

Dynamically add toolbar

genifygenify Member ✭✭

Does anyone have any code to create an Android.Widget.Toolbar dynamically?

I tried the following (Resource.Id.main_linearlayout is the id of the only LinearLayout tag):

        var root_layout = FindViewById<LinearLayout>(Resource.Id.main_linearlayout);
   Toolbar _toolbar = new Toolbar(this);
        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MatchParent, LinearLayout.LayoutParams.WrapContent);
        _toolbar.SetMinimumHeight(100);
        _toolbar.LayoutParameters = layoutParams;
        _toolbar.SetTitleTextColor(Android.Graphics.Color.DarkCyan);
        _toolbar.TooltipText = "test";

        root_layout.AddView(_toolbar);

Nothing shows up.

I am trying to replicate the following tag in code:

        < Toolbar
        android: id = "@+id/toolbar"
        android: layout_width = "match_parent"
        android: layout_height = "wrap_content"
        android: minHeight = "?android:attr/actionBarSize"
        android: background = "?android:attr/colorPrimary"
        android: theme = "@android:style/ThemeOverlay.Material.Dark.ActionBar"
        android: popupTheme = "@android:style/ThemeOverlay.Material.Light" />

Thank you

Sign In or Register to comment.