Change font of a Textview (navigation bar)

JuliusSakataJuliusSakata ✭✭Member ✭✭

Hi there !
I wanted to have a navigation bar with a centered text and a custom font. After many hours I finally found a way to center the text in Android by adding a TextView in the toolbar.axml :

    <?xml version="1.0" encoding="utf-8"?>
    <android.support.v7.widget.Toolbar xmlns:android="hxxp://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">

    <TextView
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:text="Awesome Title"
         android:layout_gravity="center"
         android:id="@+id/toolbar_title"
         android:fontFamily="Volkhov-Italic.ttf"
         />

    </android.support.v7.widget.Toolbar>

But I can't achieve to get my custom font working. I added it on the Asset folder I can access it with Xaml and behind-code to custom some label and others stuffs but not with the TextView I'm using as a Toolbar. Any idea ?

Thanks a lot !

Best Answer

  • JuliusSakataJuliusSakata ✭✭ ✭✭
    edited April 2018 Accepted Answer

    Okay so I found out the solution :

    In the XML :

    TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:textSize="40sp"
    android:text="Title"
    android:id="@+id/toolbar_title" /

    And in the MainActivity.cs

        public override void OnViewAdded(Android.Views.View child)
        {
            base.OnViewAdded(child);
            if (child.GetType() == typeof(Android.Support.V7.Widget.Toolbar))
            {
                toolbar = (Android.Support.V7.Widget.Toolbar)child;
                toolbar.TextAlignment = Android.Views.TextAlignment.Center;
    
                var txtView = FindViewById<TextView>(Resource.Id.toolbar_title);
                Typeface tf = Typeface.CreateFromAsset(Context.ApplicationContext.Assets, "Volkhov-Italic.ttf");
                txtView.SetTypeface(tf, TypefaceStyle.Normal);
            }
        }
    

Answers

  • JuliusSakataJuliusSakata ✭✭ Member ✭✭

    Okay somehow I found a way. In the MainActivity.cs I add :

                    var txtView = FindViewById<TextView>(Resource.Id.toolbar_title);
                    txtView.Text = "Update Title";
                    txtView.TextSize = 40;
                    Typeface tf = Typeface.CreateFromAsset(Assets, "Volkhov-Italic.ttf");
                    txtView.SetTypeface(tf, TypefaceStyle.Normal);
    

    It's working, but when I change the orientation of my screen the title switch back with the default font and text :(

    Any idea someone ?

    Thank you !

  • DimChrisDimChris ✭✭✭ USMember ✭✭✭

    I cant understand why it doesnt work inside your xml. Did you tried to chane in other fonts example arial and it didnt also worked?

  • JuliusSakataJuliusSakata ✭✭ Member ✭✭
    edited April 2018 Accepted Answer

    Okay so I found out the solution :

    In the XML :

    TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:textSize="40sp"
    android:text="Title"
    android:id="@+id/toolbar_title" /

    And in the MainActivity.cs

        public override void OnViewAdded(Android.Views.View child)
        {
            base.OnViewAdded(child);
            if (child.GetType() == typeof(Android.Support.V7.Widget.Toolbar))
            {
                toolbar = (Android.Support.V7.Widget.Toolbar)child;
                toolbar.TextAlignment = Android.Views.TextAlignment.Center;
    
                var txtView = FindViewById<TextView>(Resource.Id.toolbar_title);
                Typeface tf = Typeface.CreateFromAsset(Context.ApplicationContext.Assets, "Volkhov-Italic.ttf");
                txtView.SetTypeface(tf, TypefaceStyle.Normal);
            }
        }
    
Sign In or Register to comment.