I've been trying to achieve a simple task: Changing the font of my Xamarin Forms Android app's tab bar items' font. I have a custom font and I've so far been successful using it elsewhere in the app.
There doesn't seem to be a straight attribute to change the font so I used this thread's code (https://forums.xamarin.com/discussion/44321/xamarin-forms-tabbedpage-tabs-with-custom-font-working-in-android-4-x-but-no-longer-in-5-1 as a base to make a new textview inside the tabs to replace the text. That seemed to be the easiest solution for me since I only want to change the font.
The font did change and everything works otherwise beautifully (I haven't finalized the padding etc.), but for some reason I can't understand why the app renders two textviews inside one tab. I must be missing/doing wrong something here that I just don't understand.
Here's the code:
[assembly: ExportRenderer (typeof (ContentPage), typeof (TabsCustomRenderer))]
namespace TabPageFont.Android {
public class TabsCustomRenderer : PageRenderer {
protected override void DispatchDraw(Canvas canvas)
{
base.DispatchDraw(canvas);
SetTabsTypeface();
}
private void SetTabsTypeface()
{
var activity = this.Context as Activity;
if (activity == null) return;
var actionBar = activity.ActionBar;
if (actionBar == null) return;
Typeface font = Typeface.CreateFromAsset (Forms.Context.Assets, "Fonts/MyCustomFont.otf");
for (int i = 0; i < actionBar.TabCount; i++)
{
var tab = actionBar.GetTabAt(i);
if (tab == null) continue;
string tabText = tab.Text.ToUpper ();
TextView CustomTab = new TextView (this.Context);
CustomTab.Text = tabText;
CustomTab.SetTextColor (global::Android.Graphics.Color.White);
CustomTab.SetPadding (15,5,15,5);
CustomTab.Typeface = font;
tab.SetCustomView (CustomTab);
}
}
}
}
And here's a screenshot of the problem: