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.
Related
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 need the expert help. I am struggling to create the ProgressBar with rounded corner in Xamarin Form. I have searched on google and found the couple of post by using the layer property ex. "Layer.CornerRadius = 5" but my code does not support it.
How to make a progress bar with rounded corners in Xamarin forms.
(The above example not supported)
How can I create the rounded Edge of the progress bar?
Sample Code (TestControls.xaml usnig XamarinForm):-
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local="clr-namespace:Test.Mobile"
xmlns:ffimageloadingsvg="clr-namespace:FFImageLoading.Svg.Forms;assembly=FFImageLoading.Svg.Forms"
x:Class="Test.Mobile.UI.TestControls">
<ContentPage.Content>
<Grid Grid.Column="1" Margin="0" Padding="0" BackgroundColor= "LightYellow" >
<ProgressBar x:Name="pb_ProgressBar" Margin="70,0,30,0" Progress="0.0" WidthRequest="300" HeightRequest="20" ProgressColor="DarkSlateBlue" VerticalOptions="Center" BackgroundColor="AntiqueWhite"></ProgressBar>
</Grid>
</ContentPage.Content>
</ContentPage>
and Code behind file as
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Xamarin.Forms;
using Xamarin.Forms.Xaml;
namespace Test.Mobile.UI
{
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class TestControls : ContentPage
{
public TestControls ()
{
InitializeComponent ();
Device.BeginInvokeOnMainThread(async () =>
{
await pb_ProgressBar.ProgressTo(1.0, 10000, Easing.Linear);
});
}
}
}
Please help me how to make a progress bar with rounded corners in Xamarin forms?
I can not use xmlns:android="schemas.android.com/apk/res/android";. The device android tablet will be use offline and no internet access.
This is not library, you could refer to the question:
Why this line xmlns:android=“http://schemas.android.com/apk/res/android” must be the first in the layout xml file?
As NitroG42 said, in XML, xmlns declares a Namespace. In fact, when you do:
<LinearLayout android:id>
</LinearLayout>
Instead of calling android:id, the xml will use http://schemas.android.com/apk/res/android:id to be unique. Generally this page doesn't exist (it's a URI, not a URL), but sometimes it is a URL that explains the used namespace.
The namespace has pretty much the same uses as the package name in a Java application.
You could refer to the documentation:
Uniform Resource Identifier (URI)
A Uniform Resource Identifier (URI) is a string of characters which identifies an Internet Resource.
The most common URI is the Uniform Resource Locator (URL) which identifies an Internet domain address. Another, not so common type of URI is the Universal Resource Name (URN).
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.
I've just started Windows phone 8 development with previous experience of Android Development.
I was just searching if there is any strings.xml like file in windows phone 8 where we can keep strings and specially color codes. Yes i want to keep my own color codes in windows phone 8 project so that i may refer these colors in my Pages XML and in code.
Suggestions are highly appreciated.
Special color codes can be placed in your App.xaml file in the resources part.
Use like the following:
<Application.Resources>
<SolidColorBrush x:Key="ColorWhite"
Color="#FFFFFFFF" />
<ImageBrush x:Key="BackGroundBrush"
ImageSource="SplashScreenImage.jpg" />
<Style x:Key="MyPageStyle"
TargetType="phone:PhoneApplicationPage">
<Setter Property="Background">
<Setter.Value>
<ImageBrush ImageSource="Background_WindowsPhone.png"
Stretch="UniformToFill" />
</Setter.Value>
</Setter>
</Style>
</Application.Resources>
In your page you might then use the following:
<phone:ApplicationPage Style="{StaticResource MyPageStyle">
<Grid Background="{StaticResource BackGroundBrush}">
<TextBlock Text="Hi"
Foreground="{StaticResource ColorWhite}" />
</Grid>
</phone:ApplicationPage>
Let's go to the point..
I make an Android application which target is Google APIs (Platform 2.2).
I want to parse the XML File which comes from the REST API.
The XML file likes this:
<title>EARTH WIND & FIRE Live in Concert</title>
I store that output in my String variable, which I can use later and since the output is in XML, I use XML Parser class of course.
And as you can see, since the rule of XML format doesn't allow the naked character, in this case my '&' character, it will show:
<title>EARTH WIND & FIRE Live in Concert</title>
So I change that character with this method: xml.replaceAll("&", "<![CDATA[&]]>");
xml is String and I replace my & with <![CDATA[&]]> tag.
And I follow this: Android how to parse CDATA TAG?
It says that I must use dbf.setCoalescing(true);
Everything goes fine and in my TextView shows the words EARTH WIND & FIRE Live in Concert. The ampersand character (&) is shown in my emulator (Platform 2.2).
But, when I try it in the device (Platform 2.1), it will show only EARTH WIND.
And I make the new emulator Google APIs (Platform 2.1), it shows EARTH WIND too.
Does anyone here face the same problem with me??
Is there any compatibility issue with the XML Parser or the <![CDATA[]]> tag in Android??
Thanks in advance...
In String.xml in resouces folder
<string name="my_string"><![CDATA[Please put Your long text here ]]></string>
if you want to access this string for textview:
<TextView
android:id="#+id/my_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="#string/my_string"
android:textColor="#android:color/white"
android:textSize="14sp"
android:textStyle="bold" />