Hi, guys this is small but very useful concept provided by android. Suppose i have dynamic list of books and i want to show string "n book" means if book list have 1 book then want to print "Found 1 Book" else show "Found n Books". For This functionality we are simply using condition like this.
if(bookList.size()>1){ print("Found 10 Books"); } else{ print("Found 1 Book"); }
We have better approach to achieve this feature rather than doing this ordinary code. So let's see step by step.
STEP : 1 in string.xml declare your resource under
<resources> <plurals name="lbl_found_books"> <item quantity="one">Found %1$d Book</item> <item quantity="other">Found %1$d Books</item> </plurals> </resources>
Here quantity="one" means when quantity is 1 so use single resource and quantity="other" means more than 1 quantity so use plural string resource.
STEP : 2 Now in your java code access this resource like this.
textView.setText(getResources().getQuantityString(R.plurals.lbl_found_books, bookList.size(), bookList.size()));
So above line will get resource according to bookList size and no need any conditions.
Comments
Post a Comment
Thanks, I'll respond you soon!