Android Quantity Strings (Plurals)

Anik Dey
3 min readApr 7, 2019

We can get text strings for our Android Application with/without text styling and formatting. String resources can be classified in three types.

  1. String -> XML resources that provides a single string.
  2. String Array -> XML resources that provides an array of strings.
  3. Quantity Strings (Plurals) -> XML resource that carries different strings for pluralization.

In this article we will try to understand Quantity Strings (Plurals) & how to use them.

In English to represent a single item we write 1 item but for any other quantity we write “n items”. This distinction between singular and plural is very common, but other languages make finer distinctions. The full set supported by Android is zero, one, two, few, many, and other.

Quantity strings should only be used for plurals. It would be a mistake to use quantity strings to implement something like Gmail’s “Inbox” versus “Inbox (12)” when there are unread messages, for example. It might seem convenient to use quantity strings instead of an if statement, but it's important to note that some languages (such as Chinese) don't make these grammatical distinctions at all, so you'll always get the other string.

It would be a mistake to use quantity strings to implement something like Gmail’s “Inbox” versus “Inbox (12)” when there are unread messages, for example. It might seem convenient to use quantity strings instead of an if statement, but it's important to note that some languages (such as Chinese) don't make these grammatical distinctions at all, so you'll always get the other string.

The above discussion is collected from the documentation (i will provide the link at the end, so you can have in depth overview.)

Now let us get our hands dirty with some code. I am going to use a variable counter, Button and a TextView. On the click event of the button the counter will get increased and we will set the increased value as text for the TextView.

In strings.xml file add this piece of code

<resources>
<string name="app_name">AndroidPluralString</string>
<plurals name="numberOfItems">
<item quantity="one">%d item</item>
<item quantity="other">%d items</item>
</plurals>
</resources>

activity_main.xml looks like this

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:gravity="center"
android:orientation="vertical"
android:padding="16dp"
>

<Button
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Click Me"
android:textAllCaps="false"
/>

<TextView
android:id="@+id/text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="24sp"
android:textColor="@android:color/black"
android:textStyle="bold"
android:textAlignment="center"
android:layout_marginTop="16dp"
android:text="Hello"
/>

</LinearLayout>

MainActivity.java looks like this

public class MainActivity extends AppCompatActivity {

private Button button;
private TextView textView;
private int counter = 0;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

button = findViewById(R.id.button);
textView = findViewById(R.id.text_view);
setUpText(counter);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
counter++;
setUpText(counter);
}
});
}

private void setUpText(int counter) {
String itemsFound = getResources().getQuantityString(R.plurals.numberOfItems, counter, counter);
textView.setText(itemsFound);
}
}

When using the getQuantityString() method, you need to pass the count twice if your string includes string formatting with a number. For example, for the string %d songs found, the first count parameter selects the appropriate plural string and the second count parameter is inserted into the %d placeholder. If your plural strings do not include string formatting, you don't need to pass the third parameter to getQuantityString.

As a developer, you should always supply “one” and “other” strings.

Run the application and you will see O items. If you click on the button for the first time it is going to increase the value of variable counter by one and it will become 1 and you will see 1 item. Now if you click the button again it will increase the value of variable counter by one and it will become 2 and you will see 2 items

https://developer.android.com/guide/topics/resources/string-resource.html#java

--

--