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.
![](https://i0.wp.com/devcodelight.com/wp-content/uploads/2023/12/image-1.png?resize=246%2C152&ssl=1)
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.
![](https://i0.wp.com/devcodelight.com/wp-content/uploads/2023/12/image-2.png?resize=450%2C762&ssl=1)
![](https://i0.wp.com/devcodelight.com/wp-content/uploads/2023/12/image-3.png?resize=450%2C762&ssl=1)
I hope this helps you. Have a great day!
![](https://i0.wp.com/devcodelight.com/wp-content/uploads/2023/07/cropped-light_logo-5.png?resize=100%2C100&ssl=1)