Text not used again if it used once in Random Method - android

I am using random text from the string.xml to my textview through Random method. Now I don't want to use the text again if it has been used once. Help will be appreciated.
I put the text in string.xml
<string name="one">1</string>
<string name="two">2</string>
<string name="three">3</string>
<string name="four">4</string>
<string name="five">5</string>
<string name="six">6</string>
<string name="seven">7</string>
<string name="eight">8</string>
<string name="nine">9</string>
<string name="ten">10</string>
<string name="one_explaination">This is number one</string>
<string name="two_explaination">This is number two</string>
<string name="three_explaination">This is number three</string>
<string name="four_explaination">This is number four</string>
<string name="five_explaination">This is number five</string>
<string name="six_explaination">This is number six</string>
<string name="seven_explaination">This is number seven</string>
<string name="eight_explaination">This is number eight</string>
<string name="nine_explaination">This is number nine</string>
<string name="ten_explaination">This is number ten</string>
My code in MainActivity:
Random number,number_explaination;
int [] array_number,array_number_explaination;
int textview_number,textview_number_explaination;
TextView textView1,textView2;
number = new Random();
array_number = new int[] {R.string.one,R.string.two,R.string.three,R.string.four,R.string.five,R.string.six,R.string.seven,
R.string.eight,R.string.nine,R.string.ten};
array_number_explaination = new int[] {R.string.one_explaination,R.string.two_explaination,R.string.three_explaination,
R.string.four_explaination,R.string.five_explaination,R.string.six_explaination,R.string.seven_explaination,
R.string.eight_explaination,R.string.nine_explaination,R.string.ten_explaination};
textview_number = number.nextInt(array_number.length - 1);
textView1.setText(getResources().getString(array_number[textview_number]));
textView2.setText(getResources().getString(array_number_explaination[textview_number]));

LinkedList<Integer> number=new LinkedList<Integer>();
LinkedList<Integer> number_exp=new LinkedList<Integer>();
//add data
int randomIndex=(int) (Math.random()*(strs.size()-1));
textview1.setText(number.get(randomIndex));
textview2.setText(number_exp.get(randomIndex));
number.remove(randomIndex);
number_exp.remove(randomIndex);

Related

Android getString issue

So I have many strings in strings.xml, they are recorded in a format of:
<string name="list_1">xxxxxxxxxxx</string>
<string name="list_2">xxxxxxxxxxx</string>
<string name="list_3">xxxxxxxxxxx</string>
......
Now I want to load them one by one without having to type all the string IDs. I want to load them in a fashion like:
for (int i = 1; i <= num; i++) {
// Just showing what I mean.
String xxx = getString(R.string.("list_" + i));
}
Is there a method to do so?
Try this:
int resourceID = getResources().getIdentifier("list_" + i, "string", getPackageName());
String xxx = getString(resourceID);
It would be worth to use resource arrays. For example
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="planets_array">
<item>Mercury</item>
<item>Venus</item>
<item>Earth</item>
<item>Mars</item>
</string-array>
</resources>
and you can access, iterate as follows:
Resources res = getResources();
String[] planets = res.getStringArray(R.array.planets_array);

Android show plurals in both integer and float

My target is to show text like "1 star" "2.5 stars" "3 stars".
which is remove .0 for float value like 1.0, 2.0 etc. But for the value with .5 show the float value 2.5 3.5 etc.
Is this possible to use android plurals ?
I used this way but doesn't work.
plurals.xml
<plurals name="hotel_card_rating_text">
<item quantity="zero">#string/text_zero</item>
<item quantity="one">#string/text_one</item>
<item quantity="two">#string/text_two</item>
<item quantity="few">#string/text_few</item>
<item quantity="many">#string/text_many</item>
<item quantity="other">#string/text_other</item>
</plurals>
strings.xml
<resources>
<string name="text_zero">%.1f stars</string>
<string name="text_one">1 star</string>
<string name="text_two">%.1f stars</string>
<string name="text_few">%.1f stars</string>
<string name="text_many">%.1f stars</string>
<string name="text_other">%.1f stars</string>
</resources>
test code
DecimalFormat format = new DecimalFormat();
float rating = getRating();
getStarText().setText(getContext().getResources().getQuantityString(R.plurals.hotel_card_rating_text, (int) rating, format.format(rating)));
}
strings.xml use %s instead of %.1f
<resources>
<string name="text_zero">%s stars</string>
<string name="text_one">1 star</string>
<string name="text_two">%s stars</string>
<string name="text_few">%s stars</string>
<string name="text_many">%s stars</string>
<string name="text_other">%s stars</string>
</resources>
test code
DecimalFormat format = new DecimalFormat("#.#"); // format the number
float rating = getRating();
getContext().getResources()
.getQuantityString(R.plurals.hotel_card_rating_text, (int) rating, format.format(rating)));
Be careful when you use plurals. It has its own issues, see below:
Plural definition is ignored for zero quantity
Issue 8287: PluralRules does not handle quantity "zero"
you can give it a try to below snippet which is very typical
String somePostFixText = "star";
String output;
double starCount;
if (starCount > (double)1)
somePostFixText = somePostFixText+"s";
if(starCount == (long) starCount){
output = String.format("%d",(long)starCount)+" "+somePostFixText;
} else {
output = String.format("%s",starCount)+" "+somePostFixText;
}
//do whatever you want to do with output variable
Happy Coding!

Show text according to Random Textview

I am showing Random text in the two textview's on button click. Now I have to show the explaination of the text showed in textview1 in textview2.But I am not able to get it. Help will be appreciated.
I put the text in string.xml
<string name="one">1</string>
<string name="two">2</string>
<string name="three">3</string>
<string name="four">4</string>
<string name="five">5</string>
<string name="six">6</string>
<string name="seven">7</string>
<string name="eight">8</string>
<string name="nine">9</string>
<string name="ten">10</string>
<string name="one_explaination">This is number one</string>
<string name="two_explaination">This is number two</string>
<string name="three_explaination">This is number three</string>
<string name="four_explaination">This is number four</string>
<string name="five_explaination">This is number five</string>
<string name="six_explaination">This is number six</string>
<string name="seven_explaination">This is number seven</string>
<string name="eight_explaination">This is number eight</string>
<string name="nine_explaination">This is number nine</string>
<string name="ten_explaination">This is number ten</string>
My code in MainActivity:
Random number,number_explaination;
int [] array_number,array_number_explaination;
int textview_number,textview_number_explaination;
TextView textView1,textView2;
number = new Random();
array_number = new int[] {R.string.one,R.string.two,R.string.three,R.string.four,R.string.five,R.string.six,R.string.seven,
R.string.eight,R.string.nine,R.string.ten};
textview_number = number.nextInt(array_number.length - 1);
textView1.setText(array_number[textview_number]);
number_explaination = new Random();
array_number_explaination = new int[] {R.string.one_explaination,R.string.two_explaination,R.string.three_explaination,
R.string.four_explaination,R.string.five_explaination,R.string.six_explaination,R.string.seven_explaination,
R.string.eight_explaination,R.string.nine_explaination,R.string.ten_explaination};
textview_number_explaination = number_explaination.nextInt(array_number_explaination.length - 1);
textView2.setText(array_number_explaination[textview_number_explaination]);
What I want here if I get Random R.string.two in my textview1 and then I will get R.string.two_explaination and so on.How can I achive this. Sorry for my bad english.
try this
Random number,number_explaination;
int [] array_number,array_number_explaination;
int textview_number,textview_number_explaination;
TextView textView1,textView2;
number = new Random();
array_number = new int[] {R.string.one,R.string.two,R.string.three,R.string.four,R.string.five,R.string.six,R.string.seven,
R.string.eight,R.string.nine,R.string.ten};
array_number_explaination = new int[] {R.string.one_explaination,R.string.two_explaination,R.string.three_explaination,
R.string.four_explaination,R.string.five_explaination,R.string.six_explaination,R.string.seven_explaination,
R.string.eight_explaination,R.string.nine_explaination,R.string.ten_explaination};
textview_number = number.nextInt(array_number.length - 1);
textView1.setText(getResources().getString(array_number[textview_number]));
textView2.setText(getResources().getString(array_number_explaination[textview_number]));
You do not need to generate another random number for explanation text
Try this:
textView2.setText(getString(array_number_explaination[textview_number_explaination]));
getString(int) should be used to get a string in your string resource file.
you need to use string array in the strings file :
<string-array
name="string_array_name">
<item
>text_string</item>
</string-array>
and then make an array in your code with reference of :
R.array.string_array_name
see http://developer.android.com/guide/topics/resources/string-resource.html for reference.
after you got the two arrays in your code you can run loops and do your desired work

Android get random string by the xml string

Who i can get a random string from the xml string folder?
My xml loks like:
<string name="pr1">Question 1</string>
<string name="rs1.1">Aswer 1</string>
<string name="rs1.2">Aswer 2</string>
<string name="rs1.3">Aswer 3</string>
<string name="rs1.4">Aswer 4</string>
<string name="pr2">Question 2</string>
<string name="rs2.1">Aswer 1</string>
<string name="rs2.2">Aswer 2</string>
<string name="rs2.3">Aswer 3</string>
<string name="rs2.4">Aswer 4</string>
And i want do something like this:
Random r = new Random();
int num=r.nextInt(2);
TextView aswer= (TextView) findViewById(R.id.textView);
Button botao1 = (Button) findViewById(R.id.button3);
botao1.setText("#string/rs"+num+".1");
aswer.setText("#string/pr"+num);
But the input of the TextView is "#string/pr1", but i want the string of the xml who have the name "pr1". Please help. Thanks.
What you want is to get the id of the resource by name and luckily there is method for this:
getIdentifier
So, you should modify your code like this:
botao1.setText(getResources().getIdentifier("rs" + num + ".1", "string", getPackageName());

How can I get the fullname of a country from its code?

I can get the country code now but I need the full name of the country.
How can I do it?
public static String getCountry(Context c) {
TelephonyManager manager = (TelephonyManager) c
.getSystemService(Context.TELEPHONY_SERVICE);
return manager.getNetworkCountryIso().toUpperCase();
}
First write this code and putting here country code and their corresponding country name.
HashMap countryLookupMap = null;
countryLookupMap = new HashMap();
countryLookupMap.put("AD","Andorra");
countryLookupMap.put("AE","United Arab Emirates");
countryLookupMap.put("AF","Afghanistan");
countryLookupMap.put("AG","Antigua and Barbuda");
countryLookupMap.put("AI","Anguilla");
countryLookupMap.put("AL","Albania");
countryLookupMap.put("AM","Armenia");
countryLookupMap.put("AN","Netherlands Antilles");
countryLookupMap.put("AO","Angola");
countryLookupMap.put("AQ","Antarctica");
countryLookupMap.put("AR","Argentina");
countryLookupMap.put("AS","American Samoa");
countryLookupMap.put("AT","Austria");
countryLookupMap.put("AU","Australia");
countryLookupMap.put("AW","Aruba");
countryLookupMap.put("AZ","Azerbaijan");
countryLookupMap.put("BA","Bosnia and Herzegovina");
countryLookupMap.put("BB","Barbados");
countryLookupMap.put("BD","Bangladesh");
countryLookupMap.put("BE","Belgium");
countryLookupMap.put("BF","Burkina Faso");
countryLookupMap.put("BG","Bulgaria");
countryLookupMap.put("BH","Bahrain");
countryLookupMap.put("BI","Burundi");
countryLookupMap.put("BJ","Benin");
countryLookupMap.put("BM","Bermuda");
countryLookupMap.put("BN","Brunei");
countryLookupMap.put("BO","Bolivia");
countryLookupMap.put("BR","Brazil");
countryLookupMap.put("BS","Bahamas");
countryLookupMap.put("BT","Bhutan");
countryLookupMap.put("BV","Bouvet Island");
countryLookupMap.put("BW","Botswana");
countryLookupMap.put("BY","Belarus");
countryLookupMap.put("BZ","Belize");
countryLookupMap.put("CA","Canada");
countryLookupMap.put("CC","Cocos (Keeling) Islands");
countryLookupMap.put("CD","Congo, The Democratic Republic of the");
countryLookupMap.put("CF","Central African Republic");
countryLookupMap.put("CG","Congo");
countryLookupMap.put("CH","Switzerland");
countryLookupMap.put("CI","Côte d?Ivoire");
countryLookupMap.put("CK","Cook Islands");
countryLookupMap.put("CL","Chile");
countryLookupMap.put("CM","Cameroon");
countryLookupMap.put("CN","China");
countryLookupMap.put("CO","Colombia");
countryLookupMap.put("CR","Costa Rica");
countryLookupMap.put("CU","Cuba");
countryLookupMap.put("CV","Cape Verde");
countryLookupMap.put("CX","Christmas Island");
countryLookupMap.put("CY","Cyprus");
countryLookupMap.put("CZ","Czech Republic");
countryLookupMap.put("DE","Germany");
countryLookupMap.put("DJ","Djibouti");
countryLookupMap.put("DK","Denmark");
countryLookupMap.put("DM","Dominica");
countryLookupMap.put("DO","Dominican Republic");
countryLookupMap.put("DZ","Algeria");
countryLookupMap.put("EC","Ecuador");
countryLookupMap.put("EE","Estonia");
countryLookupMap.put("EG","Egypt");
countryLookupMap.put("EH","Western Sahara");
countryLookupMap.put("ER","Eritrea");
countryLookupMap.put("ES","Spain");
countryLookupMap.put("ET","Ethiopia");
countryLookupMap.put("FI","Finland");
countryLookupMap.put("FJ","Fiji Islands");
countryLookupMap.put("FK","Falkland Islands");
countryLookupMap.put("FM","Micronesia, Federated States of");
countryLookupMap.put("FO","Faroe Islands");
countryLookupMap.put("FR","France");
countryLookupMap.put("GA","Gabon");
countryLookupMap.put("GB","United Kingdom");
countryLookupMap.put("GD","Grenada");
countryLookupMap.put("GE","Georgia");
countryLookupMap.put("GF","French Guiana");
countryLookupMap.put("GH","Ghana");
countryLookupMap.put("GI","Gibraltar");
countryLookupMap.put("GL","Greenland");
countryLookupMap.put("GM","Gambia");
countryLookupMap.put("GN","Guinea");
countryLookupMap.put("GP","Guadeloupe");
countryLookupMap.put("GQ","Equatorial Guinea");
countryLookupMap.put("GR","Greece");
countryLookupMap.put("GS","South Georgia and the South Sandwich Islands");
countryLookupMap.put("GT","Guatemala");
countryLookupMap.put("GU","Guam");
countryLookupMap.put("GW","Guinea-Bissau");
countryLookupMap.put("GY","Guyana");
countryLookupMap.put("HK","Hong Kong");
countryLookupMap.put("HM","Heard Island and McDonald Islands");
countryLookupMap.put("HN","Honduras");
countryLookupMap.put("HR","Croatia");
countryLookupMap.put("HT","Haiti");
countryLookupMap.put("HU","Hungary");
countryLookupMap.put("ID","Indonesia");
countryLookupMap.put("IE","Ireland");
countryLookupMap.put("IL","Israel");
countryLookupMap.put("IN","India");
countryLookupMap.put("IO","British Indian Ocean Territory");
countryLookupMap.put("IQ","Iraq");
countryLookupMap.put("IR","Iran");
countryLookupMap.put("IS","Iceland");
countryLookupMap.put("IT","Italy");
countryLookupMap.put("JM","Jamaica");
countryLookupMap.put("JO","Jordan");
countryLookupMap.put("JP","Japan");
countryLookupMap.put("KE","Kenya");
countryLookupMap.put("KG","Kyrgyzstan");
countryLookupMap.put("KH","Cambodia");
countryLookupMap.put("KI","Kiribati");
countryLookupMap.put("KM","Comoros");
countryLookupMap.put("KN","Saint Kitts and Nevis");
countryLookupMap.put("KP","North Korea");
countryLookupMap.put("KR","South Korea");
countryLookupMap.put("KW","Kuwait");
countryLookupMap.put("KY","Cayman Islands");
countryLookupMap.put("KZ","Kazakstan");
countryLookupMap.put("LA","Laos");
countryLookupMap.put("LB","Lebanon");
countryLookupMap.put("LC","Saint Lucia");
countryLookupMap.put("LI","Liechtenstein");
countryLookupMap.put("LK","Sri Lanka");
countryLookupMap.put("LR","Liberia");
countryLookupMap.put("LS","Lesotho");
countryLookupMap.put("LT","Lithuania");
countryLookupMap.put("LU","Luxembourg");
countryLookupMap.put("LV","Latvia");
countryLookupMap.put("LY","Libyan Arab Jamahiriya");
countryLookupMap.put("MA","Morocco");
countryLookupMap.put("MC","Monaco");
countryLookupMap.put("MD","Moldova");
countryLookupMap.put("MG","Madagascar");
countryLookupMap.put("MH","Marshall Islands");
countryLookupMap.put("MK","Macedonia");
countryLookupMap.put("ML","Mali");
countryLookupMap.put("MM","Myanmar");
countryLookupMap.put("MN","Mongolia");
countryLookupMap.put("MO","Macao");
countryLookupMap.put("MP","Northern Mariana Islands");
countryLookupMap.put("MQ","Martinique");
countryLookupMap.put("MR","Mauritania");
countryLookupMap.put("MS","Montserrat");
countryLookupMap.put("MT","Malta");
countryLookupMap.put("MU","Mauritius");
countryLookupMap.put("MV","Maldives");
countryLookupMap.put("MW","Malawi");
countryLookupMap.put("MX","Mexico");
countryLookupMap.put("MY","Malaysia");
countryLookupMap.put("MZ","Mozambique");
countryLookupMap.put("NA","Namibia");
countryLookupMap.put("NC","New Caledonia");
countryLookupMap.put("NE","Niger");
countryLookupMap.put("NF","Norfolk Island");
countryLookupMap.put("NG","Nigeria");
countryLookupMap.put("NI","Nicaragua");
countryLookupMap.put("NL","Netherlands");
countryLookupMap.put("NO","Norway");
countryLookupMap.put("NP","Nepal");
countryLookupMap.put("NR","Nauru");
countryLookupMap.put("NU","Niue");
countryLookupMap.put("NZ","New Zealand");
countryLookupMap.put("OM","Oman");
countryLookupMap.put("PA","Panama");
countryLookupMap.put("PE","Peru");
countryLookupMap.put("PF","French Polynesia");
countryLookupMap.put("PG","Papua New Guinea");
countryLookupMap.put("PH","Philippines");
countryLookupMap.put("PK","Pakistan");
countryLookupMap.put("PL","Poland");
countryLookupMap.put("PM","Saint Pierre and Miquelon");
countryLookupMap.put("PN","Pitcairn");
countryLookupMap.put("PR","Puerto Rico");
countryLookupMap.put("PS","Palestine");
countryLookupMap.put("PT","Portugal");
countryLookupMap.put("PW","Palau");
countryLookupMap.put("PY","Paraguay");
countryLookupMap.put("QA","Qatar");
countryLookupMap.put("RE","Réunion");
countryLookupMap.put("RO","Romania");
countryLookupMap.put("RU","Russian Federation");
countryLookupMap.put("RW","Rwanda");
countryLookupMap.put("SA","Saudi Arabia");
countryLookupMap.put("SB","Solomon Islands");
countryLookupMap.put("SC","Seychelles");
countryLookupMap.put("SD","Sudan");
countryLookupMap.put("SE","Sweden");
countryLookupMap.put("SG","Singapore");
countryLookupMap.put("SH","Saint Helena");
countryLookupMap.put("SI","Slovenia");
countryLookupMap.put("SJ","Svalbard and Jan Mayen");
countryLookupMap.put("SK","Slovakia");
countryLookupMap.put("SL","Sierra Leone");
countryLookupMap.put("SM","San Marino");
countryLookupMap.put("SN","Senegal");
countryLookupMap.put("SO","Somalia");
countryLookupMap.put("SR","Suriname");
countryLookupMap.put("ST","Sao Tome and Principe");
countryLookupMap.put("SV","El Salvador");
countryLookupMap.put("SY","Syria");
countryLookupMap.put("SZ","Swaziland");
countryLookupMap.put("TC","Turks and Caicos Islands");
countryLookupMap.put("TD","Chad");
countryLookupMap.put("TF","French Southern territories");
countryLookupMap.put("TG","Togo");
countryLookupMap.put("TH","Thailand");
countryLookupMap.put("TJ","Tajikistan");
countryLookupMap.put("TK","Tokelau");
countryLookupMap.put("TM","Turkmenistan");
countryLookupMap.put("TN","Tunisia");
countryLookupMap.put("TO","Tonga");
countryLookupMap.put("TP","East Timor");
countryLookupMap.put("TR","Turkey");
countryLookupMap.put("TT","Trinidad and Tobago");
countryLookupMap.put("TV","Tuvalu");
countryLookupMap.put("TW","Taiwan");
countryLookupMap.put("TZ","Tanzania");
countryLookupMap.put("UA","Ukraine");
countryLookupMap.put("UG","Uganda");
countryLookupMap.put("UM","United States Minor Outlying Islands");
countryLookupMap.put("US","United States");
countryLookupMap.put("UY","Uruguay");
countryLookupMap.put("UZ","Uzbekistan");
countryLookupMap.put("VA","Holy See (Vatican City State)");
countryLookupMap.put("VC","Saint Vincent and the Grenadines");
countryLookupMap.put("VE","Venezuela");
countryLookupMap.put("VG","Virgin Islands, British");
countryLookupMap.put("VI","Virgin Islands, U.S.");
countryLookupMap.put("VN","Vietnam");
countryLookupMap.put("VU","Vanuatu");
countryLookupMap.put("WF","Wallis and Futuna");
countryLookupMap.put("WS","Samoa");
countryLookupMap.put("YE","Yemen");
countryLookupMap.put("YT","Mayotte");
countryLookupMap.put("YU","Yugoslavia");
countryLookupMap.put("ZA","South Africa");
countryLookupMap.put("ZM","Zambia");
countryLookupMap.put("ZW","Zimbabwe");
After getting country code then iterate the hasmap and search on basis of country code then you will get country code.
Might this will help to you.
Instead of having a huge hashmap of countries and their codes (which would also work), you could try one of the following:
Locale Approach
String locale = context.getResources().getConfiguration().locale.getDisplayCountry();
However, this one won't work in countries for which Android does not have a Locale. For example, in Switzerland, the language is likely to be set to German or French. This method will give you Germany or France, not Switzerland. Better to use the LocationManager or TelephonyManager approach.
Telephone Manager Approach
TelephonyManager tm = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
String countryCode = tm.getSimCountryIso();
This will work by getting the country from the SIM card. It will require the READ_PHONE_STATE permission. It will not work when there is no SIM card, or be inaccurate when the phone is roaming internationally.
Location Manager Approach (most reliable)
First, get the LocationManager. Then, call LocationManager.getLastKnownPosition(). Then, create a GeoCoder and call GeoCoder.getFromLocation(). Do this is a separate thread!! This will give you a list of Address objects. Call Address.getCountryName() and you got it.
Keep in mind that the last known position can be a bit stale, so if the user just crossed the border, you may not know about it for a while. However, it is more reliable than any of the others.
Similar to Ajay's answer, you could also use an XML file.
country_name_map.xml
<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:tools="http://schemas.android.com/tools" tools:ignore="MissingTranslation">
<string name="ad_country">Andorra</string>
<string name="ae_country">United Arab Emirates</string>
<string name="af_country">Afghanistan</string>
<string name="ag_country">Antigua and Barbuda</string>
<string name="ai_country">Anguilla</string>
<string name="al_country">Albania</string>
<string name="am_country">Armenia</string>
<string name="an_country">Netherlands Antilles</string>
<string name="ao_country">Angola</string>
<string name="aq_country">Antarctica</string>
<string name="ar_country">Argentina</string>
<string name="as_country">American Samoa</string>
<string name="at_country">Austria</string>
<string name="au_country">Australia</string>
<string name="aw_country">Aruba</string>
<string name="ax_country">Åland Islands</string>
<string name="az_country">Azerbaijan</string>
<string name="ba_country">Bosnia and Herzegovina</string>
<string name="bb_country">Barbados</string>
<string name="bd_country">Bangladesh</string>
<string name="be_country">Belgium</string>
<string name="bf_country">Burkina Faso</string>
<string name="bg_country">Bulgaria</string>
<string name="bh_country">Bahrain</string>
<string name="bi_country">Burundi</string>
<string name="bj_country">Benin</string>
<string name="bl_country">Saint Barthélemy</string>
<string name="bm_country">Bermuda</string>
<string name="bn_country">Brunei Darussalam</string>
<string name="bo_country">Bolivia (Plurinational State of)</string>
<string name="bq_country">Bonaire, Sint Eustatius and Saba</string>
<string name="br_country">Brazil</string>
<string name="bs_country">Bahamas</string>
<string name="bt_country">Bhutan</string>
<string name="bv_country">Bouvet Island</string>
<string name="bw_country">Botswana</string>
<string name="by_country">Belarus</string>
<string name="bz_country">Belize</string>
<string name="ca_country">Canada</string>
<string name="cc_country">Cocos (Keeling) Islands</string>
<string name="cd_country">Congo (Democratic Republic of the)</string>
<string name="cf_country">Central African Republic</string>
<string name="cg_country">Congo</string>
<string name="ch_country">Switzerland</string>
<string name="ci_country">Côte d\'Ivoire</string>
<string name="ck_country">Cook Islands</string>
<string name="cl_country">Chile</string>
<string name="cm_country">Cameroon</string>
<string name="cn_country">China</string>
<string name="co_country">Colombia</string>
<string name="cr_country">Costa Rica</string>
<string name="cu_country">Cuba</string>
<string name="cv_country">Cabo Verde</string>
<string name="cw_country">Curaçao</string>
<string name="cx_country">Christmas Island</string>
<string name="cy_country">Cyprus</string>
<string name="cz_country">Czechia</string>
<string name="de_country">Germany</string>
<string name="dj_country">Djibouti</string>
<string name="dk_country">Denmark</string>
<string name="dm_country">Dominica</string>
<string name="do_country">Dominican Republic</string>
<string name="dz_country">Algeria</string>
<string name="ec_country">Ecuador</string>
<string name="ee_country">Estonia</string>
<string name="eg_country">Egypt</string>
<string name="eh_country">Western Sahara</string>
<string name="er_country">Eritrea</string>
<string name="es_country">Spain</string>
<string name="et_country">Ethiopia</string>
<string name="fi_country">Finland</string>
<string name="fj_country">Fiji</string>
<string name="fk_country">Falkland Islands (Malvinas)</string>
<string name="fm_country">Micronesia (Federated States of)</string>
<string name="fo_country">Faroe Islands</string>
<string name="fr_country">France</string>
<string name="ga_country">Gabon</string>
<string name="gb_country">United Kingdom of Great Britain and Northern Ireland</string>
<string name="gd_country">Grenada</string>
<string name="ge_country">Georgia</string>
<string name="gf_country">French Guiana</string>
<string name="gg_country">Guernsey</string>
<string name="gh_country">Ghana</string>
<string name="gi_country">Gibraltar</string>
<string name="gl_country">Greenland</string>
<string name="gm_country">Gambia</string>
<string name="gn_country">Guinea</string>
<string name="gp_country">Guadeloupe</string>
<string name="gq_country">Equatorial Guinea</string>
<string name="gr_country">Greece</string>
<string name="gs_country">South Georgia and the South Sandwich Islands</string>
<string name="gt_country">Guatemala</string>
<string name="gu_country">Guam</string>
<string name="gw_country">Guinea-Bissau</string>
<string name="gy_country">Guyana</string>
<string name="hk_country">Hong Kong</string>
<string name="hm_country">Heard Island and McDonald Islands</string>
<string name="hn_country">Honduras</string>
<string name="hr_country">Croatia</string>
<string name="ht_country">Haiti</string>
<string name="hu_country">Hungary</string>
<string name="id_country">Indonesia</string>
<string name="ie_country">Ireland</string>
<string name="il_country">Israel</string>
<string name="im_country">Isle of Man</string>
<string name="in_country">India</string>
<string name="io_country">British Indian Ocean Territory</string>
<string name="iq_country">Iraq</string>
<string name="ir_country">Iran (Islamic Republic of)</string>
<string name="is_country">Iceland</string>
<string name="it_country">Italy</string>
<string name="je_country">Jersey</string>
<string name="jm_country">Jamaica</string>
<string name="jo_country">Jordan</string>
<string name="jp_country">Japan</string>
<string name="ke_country">Kenya</string>
<string name="kg_country">Kyrgyzstan</string>
<string name="kh_country">Cambodia</string>
<string name="ki_country">Kiribati</string>
<string name="km_country">Comoros</string>
<string name="kn_country">Saint Kitts and Nevis</string>
<string name="kp_country">Korea (Democratic People\'s Republic of)</string>
<string name="kr_country">Korea (Republic of)</string>
<string name="kw_country">Kuwait</string>
<string name="ky_country">Cayman Islands</string>
<string name="kz_country">Kazakhstan</string>
<string name="la_country">Lao People\'s Democratic Republic</string>
<string name="lb_country">Lebanon</string>
<string name="lc_country">Saint Lucia</string>
<string name="li_country">Liechtenstein</string>
<string name="lk_country">Sri Lanka</string>
<string name="lr_country">Liberia</string>
<string name="ls_country">Lesotho</string>
<string name="lt_country">Lithuania</string>
<string name="lu_country">Luxembourg</string>
<string name="lv_country">Latvia</string>
<string name="ly_country">Libya</string>
<string name="ma_country">Morocco</string>
<string name="mc_country">Monaco</string>
<string name="md_country">Moldova (Republic of)</string>
<string name="me_country">Montenegro</string>
<string name="mf_country">Saint Martin (French part)</string>
<string name="mg_country">Madagascar</string>
<string name="mh_country">Marshall Islands</string>
<string name="mk_country">Macedonia (the former Yugoslav Republic of)</string>
<string name="ml_country">Mali</string>
<string name="mm_country">Myanmar</string>
<string name="mn_country">Mongolia</string>
<string name="mo_country">Macao</string>
<string name="mp_country">Northern Mariana Islands</string>
<string name="mq_country">Martinique</string>
<string name="mr_country">Mauritania</string>
<string name="ms_country">Montserrat</string>
<string name="mt_country">Malta</string>
<string name="mu_country">Mauritius</string>
<string name="mv_country">Maldives</string>
<string name="mw_country">Malawi</string>
<string name="mx_country">Mexico</string>
<string name="my_country">Malaysia</string>
<string name="mz_country">Mozambique</string>
<string name="na_country">Namibia</string>
<string name="nc_country">New Caledonia</string>
<string name="ne_country">Niger</string>
<string name="nf_country">Norfolk Island</string>
<string name="ng_country">Nigeria</string>
<string name="ni_country">Nicaragua</string>
<string name="nl_country">Netherlands</string>
<string name="no_country">Norway</string>
<string name="np_country">Nepal</string>
<string name="nr_country">Nauru</string>
<string name="nu_country">Niue</string>
<string name="nz_country">New Zealand</string>
<string name="om_country">Oman</string>
<string name="pa_country">Panama</string>
<string name="pe_country">Peru</string>
<string name="pf_country">French Polynesia</string>
<string name="pg_country">Papua New Guinea</string>
<string name="ph_country">Philippines</string>
<string name="pk_country">Pakistan</string>
<string name="pl_country">Poland</string>
<string name="pm_country">Saint Pierre and Miquelon</string>
<string name="pn_country">Pitcairn</string>
<string name="pr_country">Puerto Rico</string>
<string name="ps_country">Palestine, State of</string>
<string name="pt_country">Portugal</string>
<string name="pw_country">Palau</string>
<string name="py_country">Paraguay</string>
<string name="qa_country">Qatar</string>
<string name="re_country">Réunion</string>
<string name="ro_country">Romania</string>
<string name="rs_country">Serbia</string>
<string name="ru_country">Russian Federation</string>
<string name="rw_country">Rwanda</string>
<string name="sa_country">Saudi Arabia</string>
<string name="sb_country">Solomon Islands</string>
<string name="sc_country">Seychelles</string>
<string name="sd_country">Sudan</string>
<string name="se_country">Sweden</string>
<string name="sg_country">Singapore</string>
<string name="sh_country">Saint Helena, Ascension and Tristan da Cunha</string>
<string name="si_country">Slovenia</string>
<string name="sj_country">Svalbard and Jan Mayen</string>
<string name="sk_country">Slovakia</string>
<string name="sl_country">Sierra Leone</string>
<string name="sm_country">San Marino</string>
<string name="sn_country">Senegal</string>
<string name="so_country">Somalia</string>
<string name="sr_country">Suriname</string>
<string name="ss_country">South Sudan</string>
<string name="st_country">Sao Tome and Principe</string>
<string name="sv_country">El Salvador</string>
<string name="sx_country">Sint Maarten (Dutch part)</string>
<string name="sy_country">Syrian Arab Republic</string>
<string name="sz_country">Swaziland</string>
<string name="tc_country">Turks and Caicos Islands</string>
<string name="td_country">Chad</string>
<string name="tf_country">French Southern Territories</string>
<string name="tg_country">Togo</string>
<string name="th_country">Thailand</string>
<string name="tj_country">Tajikistan</string>
<string name="tk_country">Tokelau</string>
<string name="tl_country">Timor-Leste</string>
<string name="tm_country">Turkmenistan</string>
<string name="tn_country">Tunisia</string>
<string name="to_country">Tonga</string>
<string name="tr_country">Turkey</string>
<string name="tt_country">Trinidad and Tobago</string>
<string name="tv_country">Tuvalu</string>
<string name="tw_country">Taiwan, Province of China[a]</string>
<string name="tz_country">Tanzania, United Republic of</string>
<string name="ua_country">Ukraine</string>
<string name="ug_country">Uganda</string>
<string name="um_country">United States Minor Outlying Islands</string>
<string name="us_country">United States of America</string>
<string name="uy_country">Uruguay</string>
<string name="uz_country">Uzbekistan</string>
<string name="va_country">Holy See</string>
<string name="vc_country">Saint Vincent and the Grenadines</string>
<string name="ve_country">Venezuela (Bolivarian Republic of)</string>
<string name="vg_country">Virgin Islands (British)</string>
<string name="vi_country">Virgin Islands (U.S.)</string>
<string name="vn_country">Viet Nam</string>
<string name="vu_country">Vanuatu</string>
<string name="wf_country">Wallis and Futuna</string>
<string name="ws_country">Samoa</string>
<string name="ye_country">Yemen</string>
<string name="yt_country">Mayotte</string>
<string name="za_country">South Africa</string>
<string name="zm_country">Zambia</string>
<string name="zw_country">Zimbabwe</string>
</resources>
Note that I appended _country because do is a reserved keyword.
Then you can define a method to get the country name.
public static String getCountryNameFromIso2Code(Context context, String iso2Code) {
if(context == null || TextUtils.isEmpty(iso2Code)) return "";
String key = String.format(Locale.US, "%s%s", iso2Code.toLowerCase(Locale.US), "_country");
int resId = context.getResources().getIdentifier(key, "string", context.getPackageName());
return context.getString(resId);
}
Sample usage:
String sampleInputCountryCode = "an";
String fullCountryName = MyUtilityClass.getCountryNameFromIso2Code(getApplicationContext(), sampleInputCountryCode);

Categories

Resources