How to Add Plurals or Different Texts for a Certain Quantity in Android Studio Using Java (Using Text Resources)

Tiempo de lectura: 2 minutos

First, we will define the plural messages in the strings.xml file, located in the res/values/ folder. This file contains the text resources used in the application.

In this case, as an example, we will display different text depending on the number of messages we have.

In the plural text item quantity=»other», to add the number, %d is used, indicating that this text value will be replaced by an integer number.

In Android’s plural resources, besides %d, there are other placeholder markers that can be used to format text strings based on the quantity of elements. Some of these placeholders are:

  • %d: Used to represent integer values.
  • %f: Represents floating-point values (decimal numbers).
  • %s: Represents text strings.
  • %c: Used for individual characters.
  • %b or %B: Represents boolean values.
  • %x: Displays numbers in hexadecimal format.
  • %o: Displays numbers in octal format.
<resources>
    <string name="app_name">MyApp</string>

    <!-- Set the text for the plural of messages-->
    <plurals name="devcodelight_message">
        <item quantity="one">You have one message</item>
        <item quantity="other">You have %d messages</item>
    </plurals>
</resources>

In the XML layout (activity_main.xml), we create the user interface, in this case, simply creating a TextView where the text with the number of messages will be displayed as shown below.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <!-- TextView to display the message of unread messages -->
    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="18sp"
        android:layout_centerInParent="true" />

</RelativeLayout>

In the activity code (MainActivity.java, for example), we get the quantity of unread messages and update the text of the TextView

import android.os.Bundle;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

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

        // Get the number of messages (here a value is simulated)
        int numMessages = 5;

        // Access the plural resource from the code
        String messagesString = getResources().getQuantityString(R.plurals.devcodelight_message, numMessages, numMessages);

        // Display the text in a TextView
        TextView textView = findViewById(R.id.textView);
        textView.setText(messagesString);
    }
}

Finally, I show the result of the previous example.

I hope this helps you. Have a great day!

Leave a Comment