Showing posts with label plurals. Show all posts
Showing posts with label plurals. Show all posts

15 September 2010

More plurals: decimal values

In my previous post, I showed you how to set string plurals. If you noticed, the methods to get the plurals strings only accept ints. What if (like me) you want to display a decimal value? I am getting my raw value from a progress bar with a range of 1-10, with 0.1 increments.

First, to display decimal values, I set my plurals string to display a float value.
<item quantity="other">Progress is at %.1f units.</item>

And then I devised a way to set the quantity based on the value of the progress bar (ProgressBar.getProgress() returns an int).

public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {

// get quantity for plurals string
int quantity = setQuantity(progress);

// convert the actual progress to float
float floatProgress = convertProgress(progress);

// get the actual string and replace formatting with the float value
String currentProgress = getResources()
.getQuantityString(R.plurals.seekBarProgress, // get the plurals
quantity, // set the quantity
floatProgress); // format arguments

// set text to display
TextView displayProgress = (TextView)findViewById(R.id.prog_text);
displayProgress.setText(currentProgress);

}

/**
* Use this method to see if we will use the singular or plural string.
*
* @param progress
* @return the value to set in getQuantityString()
*/
private int setQuantity(int progress){
int quantity;

if (((progress%10) == 0) && ((progress/10) == 1)){
quantity = 1;
} else {
quantity = 2;
}

return quantity;
}

/**
* Use this value to get the *actual* value to display.
*
* @param progress actual progress value from 0 to 100
* @return the float value from 0.0 to 10.0
*/
private Float convertProgress(int progress){
return ((Float.valueOf(String.valueOf(progress)))/(float)10);
}
So you see, it's quite long-winded. Here are some screen shots of the results:
Different values for the unit value

String Pluralization

Last week, I discovered Android's support for plural strings by accident. And a good accident it was since I am working on an app that will display a float to the user. I used to display:
You set XX mile(s).
which is kinda lame.

Plurals lets you specify the string to display for different quantities. So how do we use this Plurals thing?

In your string resources XML, which is usually strings.xml, define the plurals element like so:

<?xml version="1.0" encoding="utf-8"?>
<resources>
<plurals name="pluralsTest">
<item quantity="one">You have one friend.</item>
<item quantity="other">You have %d friends.</item>
</plurals>
</resources>
Remember, your parent node must be <resources>!

Android provides several methods to use these in your code. Let's see what each of them results to when used:

// set one as the quantity
String one = getResources().getQuantityString(R.plurals.pluralsTest, 1);

// set two as the quantity
String more = getResources().getQuantityString(R.plurals.pluralsTest, 2, 2);

// set one as the quantity
CharSequence quantity = getResources().getQuantityText(R.plurals.pluralsTest, 1);

// set two as the quantity
CharSequence quantityMore = getResources().getQuantityText(R.plurals.pluralsTest, 2);

I created a basic layout with TextViews to display what each of these strings look like. Take note though that getQuantityText() returns a CharSequence and not a String! Also, from what I have noticed, and as the name mildly suggests, getQuantityText() gets the actual value of the text you defined in your xml.