How To Get Pluralized Values In Xamarin Android App Using Visual Studio 2015

Introduction

Xamarin is a platform to develop cross-platform and multi-platform apps (for example, Windows phone, Android, iOS). In the Xamarin platform, the code sharing concept is used. In Xamarin Studio, Visual Studio is also available.

Prerequisites
  • Visual Studio 2015 Update 3.
The steps, given below are required to be followed in order to get pluralized values in the Xamarin Android app, using Visual Studio 2015.

Step 1

Click File--> select New--> select Project. The project needs to be clicked after opening all the types of projects in Visual Studio or click (Ctrl+Shift+N).
 
 

Step 2

After opening the New Project, select Installed-->Templates-->Visual C#-->Android-->choose the Blank app (Android). Now, give your Android app a name (Ex:sample) and give the path of your project. Afterwards, click OK.

 
 
Step 3

Next go to the solution explorer. All  the files and sources in your project are in solution explorer.

Next Select Resource-->Layout-->double click to open main.axml page. You want to select source to write the xaml code.

You want to choose the designer window so you can design your app.
 
 

Step 4

After opening, the main.axml file will open the main page designer. In this page choose which type you want so you can design this page.
 
 

Next Delete the Default hello world button.

Go to the source panel and you can see the button coding. You will delete it.

After deleting the xaml code, next delete the C# button action code.

Go to the MainActivity.cs page. You will delete the button code.

Step 5

Now, go to the toolbox Window. In the toolbox Window, get all the types of the tools and controls.

You need to go to the toolbox Window. Now, scroll down and you will see all the tools and controls.

You need to drag and drop the Button.

 
 
Step 6

Now, go to the properties Window. You need to edit the Button Id Value and Text Value (EX: android:id="@+id/MyButton"android:text="Hello" ).
 
 

Step 7

In this step, go to the Main.axml page Source Panel. Note, the Button Id value.

 

Main.axml
  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.    android:orientation="vertical"  
  3.    android:layout_width="match_parent"  
  4.    android:layout_height="match_parent"  
  5.    android:minWidth="25px"  
  6.    android:minHeight="25px">  
  7. <Button  
  8.    android:id="@+id/MyButton"  
  9.    android:layout_width="fill_parent"  
  10.    android:layout_height="wrap_content"  
  11.    android:text="Hello" />  
  12. </LinearLayout>   
Step 8

In this step open the String.xml page. Go to the Solution Explorer-->Resource-->values-->String.xml.
 
 

Step 9

After open the String.xml file write the following xml code.

String.xml
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <resources>  
  3.     <plurals name="numberOfSongsAvailable">  
  4.         <item quantity="one">One song found.</item>  
  5.         <item quantity="other">%d songs found.</item>  
  6.     </plurals>  
  7. </resources>  


Step 10

In this step create one folder its name is values-pl

Go to Solution Explorer-->Resource-->Right Click--> Add-->New folder and give name values-pl.
 
 

Step 11

In this step create one file its name is strings.xml

Go to Solution Explorer-->Resource-->values-pl-->Right Click--> Add-->New items or (Ctrl+Shift+A).

 

Step 12

Now choose Xml file, and give the name strings.xml.
 
 

Step 13

After creating the strings.xml file write the Following xml code.

strings.xml
  1. <?xml version="1.0" encoding="utf-8" ?>  
  2. <resources>  
  3.     <plurals name="numberOfSongsAvailable">  
  4.         <item quantity="one">Selfi song.</item>  
  5.         <item quantity="few">Love song %d En idhayathai.</item>  
  6.         <item quantity="other">Kathala %d Oh baby.</item>  
  7.     </plurals>  
  8. </resources>   
 

Step 14

In this step go to the MainActivity.cs page. Write the following code.

MainActivity.cs
  1. private int count = 0;  
  2. protected override void OnCreate(Bundle bundle)   
  3. {  
  4.     base.OnCreate(bundle);  
  5.     // Set our view from the "main" layout resource  
  6.     SetContentView(Resource.Layout.Main);  
  7.     var button = FindViewById < Button > (Resource.Id.MyButton);  
  8.     button.Click += delegate {  
  9.         count++;  
  10.         var buttonText = Resources.GetQuantityString(Resource.Plurals.numberOfSongsAvailable, count, count);  
  11.         button.Text = buttonText;  
  12.     };  
  13. }   
 
 
Step 15

If you have Android Virtual device, run the app on it. Else, connect your Android phone and run the app in that.

Simply, connect your phone and go to Visual Studio. The connected phone will show up in the Run menu (Ex:LENOVO A6020a40(Android 5.1-API 22)). Click the Run option.

 
 
Output

After a few seconds, the app will start running on your phone. You will click the Hello Button. You will get the values successfully.
 
 

You will get the Plural values successfully.

 

Summary

So, this was the process of getting pluralized values in Xamarin Android app, using Visual Studio 2015.

Up Next
    Ebook Download
    View all
    Learn
    View all