I am having an issue while using observable collection in XAML. I have created tabs on my Xamarin.Forms application using observable collection in XAML and for accessing observable collection in XAML I have included this namespace
xmlns:col="clr-namespace:System.Collections.ObjectModel;assembly=System.ObjectModel"
when I am writing System.ObjectModel in assembly it is working fine in iOS and UWP but crashing on Android. If I am writing netstandard assembly instead of System.ObjectModel then it is working fine in Android but crashing in iOS and UWP.
I have tried installing System.ObjectModel package in my project but it didn't work.
My XAML looks something like this
<ResourceDictionary>
<!-- Default list -->
<xcoll:ObservableCollection x:Key="NewTabs" x:TypeArguments="model:ISequenceItem">
<test:FirstPage Order="1" Text="Page 1" />
<test:SecondPage Order="2" Text="Page2" />
<test:ThirdPage Order="3" Text="Page3" />
</xcoll:ObservableCollection>
</ResourceDictionary>
<test:Tabs
ItemSource="{Binding Path=NewTabs}"
SelectedItem="{Binding Path=SelectedTab}">
I am new to Xamarin (and Xamarin.Forms if they are different). I placed some Entries on page. As soon as I tap one on real Android 4.4 device or inside 4.4 emulator, the application crash with error:
Android.Content.Res.Resources+NotFoundException: 'File res/drawable/abc_ic_clear_material.xml from drawable resource ID #0x7f07001d'
No crash on Android 6.0 real device and 9.0 emulator. I think crash somehow related with ClearButtonVisibility="WhileEditing", because there is clear in missing XML.
<Entry Grid.Row="2" Grid.Column="1" ClearButtonVisibility="WhileEditing" Keyboard="Numeric" Text="15" TextChanged="PriceEntry_TextChanged" />
<Entry Grid.Row="2" Grid.Column="2" ClearButtonVisibility="WhileEditing" Keyboard="Numeric" Text="0" TextChanged="AmountEntry_TextChanged" />
This is how I define Entries in XAML. I want my app to specifically work under Android 4.4. What to do?
I was able to reproduce this issue.
To fix this, please try adding the code below into your MainActivity.cs OnCreate method:
Android.Support.V7.App.AppCompatDelegate.CompatVectorFromResourcesEnabled = true;
Let me know if you have any other questions or concerns.
I am using Xamarin.forms version 3.0.0.561731 for the Application.The issue is, when a button is clicked another button like object at top left corner is appearing for few seconds which is very annoying, I have been searching for couple of hours and tried what ever stuff found but no luck.
I tried upgrading to latest xamarin.forms version and also tried to downgrading it upto 2.4.0 but still the issue exists.
Please guide me to get rid of that visual effect issue while tapping/clicking on button.
<Button BorderRadius="5" Text="Continue" HorizontalOptions='FillAndExpand' BackgroundColor="#1976D2" TextColor="White" Command="{Binding ContinueFormCommand}">
<Button.FontSize>
<OnIdiom x:TypeArguments="x:Double">
<OnIdiom.Phone>
<OnPlatform x:TypeArguments="x:Double" iOS="13" Android="15" WinPhone="30"/>
</OnIdiom.Phone>
<OnIdiom.Tablet>
<OnPlatform x:TypeArguments="x:Double" iOS="13" Android="15" WinPhone="30"/>
</OnIdiom.Tablet>
</OnIdiom>
</Button.FontSize>
<Button.HeightRequest>
<OnIdiom x:TypeArguments="x:Double">
<OnIdiom.Phone>
<OnPlatform x:TypeArguments="x:Double" iOS="13" Android="45" WinPhone="30"/>
</OnIdiom.Phone>
<OnIdiom.Tablet>
<OnPlatform x:TypeArguments="x:Double" iOS="13" Android="15" WinPhone="30"/>
</OnIdiom.Tablet>
</OnIdiom>
</Button.HeightRequest>
</Button>
I got this issue for Samsung device.
This issue is open for way too long on GitHub here and here.
As a work around, you can using Xamarin.Forms v2.5.0.122203 which doesn't have the issue.
I'm pretty new to nativescript. I'm trying to figure out how to use the string values from the strings.xml so i can localize my app.
My layout looks like this.
<StackLayout class="page">
<label [text]="#string/a_value"></label>
<Label text="Hello world with tap!" class="yellow"></Label>
<button text="Tap me my friend" (tap)="tapTheButton()" class="red"></button>
<label [text]="text" class="blue"></label>
</StackLayout>
The #string/a_value should get the string from the res/values/strings.xml file.
Any help is welcome!
#string/string_name is not a valid notation when describing NativeScript layouts as it's a strictly Android way of interpolating strings.
You could probably access the resource manager (https://developer.android.com/reference/android/content/Context.html#getResources()) and get the string value for string with name a_value, and then data bind that to your label. That would only work on Android.
There is a NativeScript plugin done by the community that aims to make internationalization abstract, so that it works for both Android and iOS - nativescript-i18n.
I need to specify different FontFamily for different Labels in my app. I need to use the default fonts (e.g. Roboto for Android and Helvetica for iOS) with their modifications (e.g. Light, Medium, Bold). As far as I understand I should be using Roboto-Light and Helvetica-Light to get the Light version of the fonts (same for Medium and Bold).
In addition to this requirement I need to set the fonts in XAML (like described in documentation) so I end up with this code
<StackLayout BackgroundColor="#F8F8F8" Padding="0, 20, 0, 0">
<Label Text="Black" TextColor="#000000" >
<Label.FontSize>
<OnPlatform x:TypeArguments="x:Double"
iOS="17"
Android="16"
WinPhone="16" />
</Label.FontSize>
<Label.FontFamily>
<OnPlatform x:TypeArguments="x:String">
<OnPlatform.iOS>Helvetica-Black</OnPlatform.iOS>
<OnPlatform.Android>Roboto-Black</OnPlatform.Android>
<OnPlatform.WinPhone></OnPlatform.WinPhone>
</OnPlatform>
</Label.FontFamily>
</Label>
<Label Text="Light" TextColor="#000000">
<Label.FontSize>
<OnPlatform x:TypeArguments="x:Double"
iOS="17"
Android="16"
WinPhone="16" />
</Label.FontSize>
<Label.FontFamily>
<OnPlatform x:TypeArguments="x:String">
<OnPlatform.iOS>Helvetica-Light</OnPlatform.iOS>
<OnPlatform.Android>Roboto-Light</OnPlatform.Android>
<OnPlatform.WinPhone></OnPlatform.WinPhone>
</OnPlatform>
</Label.FontFamily>
</Label>
<Label Text="Medium" TextColor="#000000" >
<Label.FontSize>
<OnPlatform x:TypeArguments="x:Double"
iOS="17"
Android="16"
WinPhone="16" />
</Label.FontSize>
<Label.FontFamily>
<OnPlatform x:TypeArguments="x:String">
<OnPlatform.iOS>Helvetica-Medium</OnPlatform.iOS>
<OnPlatform.Android>Roboto-Medium</OnPlatform.Android>
<OnPlatform.WinPhone></OnPlatform.WinPhone>
</OnPlatform>
</Label.FontFamily>
</Label>
<Label Text="Bold" TextColor="#000000">
<Label.FontSize>
<OnPlatform x:TypeArguments="x:Double"
iOS="17"
Android="16"
WinPhone="16" />
</Label.FontSize>
<Label.FontFamily>
<OnPlatform x:TypeArguments="x:String">
<OnPlatform.iOS>Helvetica-Bold</OnPlatform.iOS>
<OnPlatform.Android>Roboto-Bold</OnPlatform.Android>
<OnPlatform.WinPhone></OnPlatform.WinPhone>
</OnPlatform>
</Label.FontFamily>
</Label>
However, the result in Android is unexpected. The FontFamily of the different Labels is not changed. They all look the same.
The same code in iOS works as expected
My question is: How to get the Roboto-Light, Roboto-Medium and Roboto-Bold fonts in my Android app if following XAMARIN documentation does not work?
Update:
I did not see that you were using API 18 / 4.3 the first time (in the title bar of your emulator), thought you were loading them as custom assets for older android versions. Since roboto is the default font since 4.1, you can use them as:
sans-serif
sans-serif-light
sans-serif-condensed
sans-serif-thin (4.2+)
Original:
https://developer.xamarin.com/guides/cross-platform/xamarin-forms/working-with/fonts/
Xamarin.Forms for Android does not currently expose the ability to set
the font to a custom font file, so custom renderers are required. When
writing the renderer for the Android platform, create a Typeface
instance that references a custom font file that has been added to the
Assets directory of the application (with Build Action: AndroidAsset).
[assembly: ExportRenderer (typeof (MyLabel), typeof (MyLabelRenderer))]
namespace WorkingWithFonts.Android {
public class MyLabelRenderer : LabelRenderer {
protected override void OnElementChanged (ElementChangedEventArgs<Label> e) {
base.OnElementChanged (e);
var label = (TextView)Control; // for example
Typeface font = Typeface.CreateFromAsset (Forms.Context.Assets, "SF Hollywood Hills.ttf");
label.Typeface = font;
}
}
}
I did overwrite the default Renderer for all views I needed it for.
That way you can use XAML code like you intended to:
<Label FontFamily="Arial" Text="Hi there" />
Here is an example for Label.
[assembly: ExportRenderer (typeof (Label), typeof (MyLabelRenderer))]
namespace MyApp.Droid
{
public class MyLabelRenderer : LabelRenderer
{
protected override void OnElementChanged(ElementChangedEventArgs<Label> e)
{
base.OnElementChanged(e);
if ( !String.IsNullOrEmpty(Element.FontFamily) )
Control.Typeface = Typeface.CreateFromAsset(Forms.Context.Assets, "Fonts/" + Element.FontFamily + ".otf");
}
}
}
Of course you need to map that to the location where you embedded the custom font. In my case Assets/Fonts/Arial.otf.
Please use below render, to load custom font in label, please make sure that you will put your font file in fonts folder in assets folder of android project to define similar path for all platform
[assembly: ExportRenderer(typeof(Label), typeof(CustomLabelRenderer))]
namespace MyApp.Droid.Renderer
{
public class CustomLabelRenderer : LabelRenderer
{
protected override void OnElementChanged(ElementChangedEventArgs<Label> e)
{
base.OnElementChanged(e);
if (e.NewElement != null)
if (!String.IsNullOrEmpty(e.NewElement.FontFamily))
Control.Typeface = Typeface.CreateFromAsset(Forms.Context.Assets, "Fonts/" + Element.FontFamily);
}
}
}
xaml of label control, please make sure that font file is available in font folder
<Label FontFamily="Roboto-Light.ttf" Text="Hi there" />
2020 Update, Xamarin.Forms 4.5 now supports custom fonts embedded in one place (shared UI project) and exported via ExportFont attribute: https://learn.microsoft.com/en-us/xamarin/xamarin-forms/user-interface/text/fonts#use-a-custom-font - iOS, Android and UWP.
For macOS and WPF you still have to go longer path as described below.
As of 2018, in Xamairn.Forms, there's no need for platform renderers. The steps are:
Download TTF files and bundle them as resources with each platform project.
Reference the fonts from within Xamarin.Forms shared project, taking into account the peculiarities of each platform (naming conventions for addressing resources and font families).
Apply the fonts to Xamarin.Forms controls.
Here's detailed description on how to add and apply custom fonts in macOS, WPF and Android.