Displaying Country and its calling code, but returning its Abbreviated code using Android XML

StackOverflow https://stackoverflow.com/questions/16161732

  •  11-04-2022
  •  | 
  •  

Question

I am trying to develop a Registration screen from Android XML. As in every Registration form, I would need to display the list of countries. I am able to do this using string-array in strings.xml file.

The greater part of the problem is, when a user selects a country, the phone number field just below it should be initially filled with its respective country code, which may or may not be editable. Here, my problem is how do I get the country code when the country is selected. Do I need to use a database or is it achievable using xml itself?

Besides that, when user submits the Register form, I would have to send the abbreviated code of the country, not the full country name. Now, again this would require either a database or xml?

My app doesn't use database till now, it would not be so good to use database for this purpose. Moreover, almost any application that uses a registration needs this thing to be done, but I cannot find any resources on how to do this in Android.

Also, I would like to tell you that my minimum sdk is version 7.

Please help.

Was it helpful?

Solution

I was finally able to do it without using database. I'm writing down the steps so that it may help anyone else who needs the same thing to be done.

First I downloaded the CSV available at: https://github.com/mledoze/countries/blob/master/countries.csv

I removed all other fields, except those I needed. It left me with 3 fields: name, abbreviation and calling code.

Next, I downloaded the CSVReader from: http://code.google.com/p/secrets-for-android/source/browse/trunk/src/au/com/bytecode/opencsv/CSVReader.java

Got the items from the CSV as mentioned in How to parse the CSV file in android application? by "Kopfgeldjaeger" as:

String next[] = {};
List<String[]> list = new ArrayList<String[]>();

 try {
      CSVReader reader = new CSVReader(new InputStreamReader(getAssets().open("countries.csv")));
            for(;;) {
                next = reader.readNext();
                if(next != null) {
                    list.add(next);
                } else {
                    break;
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

Next I added an ArrayList for each of the values like:

ArrayList<String> countryNames = new ArrayList<String>();
ArrayList<String> countryAbber = new ArrayList<String>();
ArrayList<String> countryCodes = new ArrayList<String>();

for(int i=0; i < list.size(); i++)
{
    countryNames.add(list.get(i)[0]); // gets name
    countryAbber.add(list.get(i)[1]); // gets abbreviation
    countryCodes.add(list.get(i)[2]); // gets calling code
}

Then added it to the spinner in the XML layout as:

ArrayAdapter<String> countryAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item, countryNames);
        spinner.setAdapter(countryAdapter);

// adding event to display codes when country is selected


spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

            @Override
            public void onItemSelected(AdapterView<?> arg0, View arg1,
                    int pos, long arg3) {
                // display the corresponding country code
                TextView tvCountryCode = (TextView) findViewById(R.id.country_code);
                tvCountryCode.setText("+"+list.get(pos)[2]);
                countryPosition = pos;
            }

            @Override
            public void onNothingSelected(AdapterView<?> arg0) {
                // TODO Auto-generated method stub

            }
        });

This way, I was able to display country code in the xml file, when a country was selected from the dropdown list.

Remember to add the setOnItemSelectedListener() to achive that.

I hope this helps somebody in future.

OTHER TIPS

I would advise you to use a database. API level 7 supports SQLite databases (I used them in android 2.1 myself). Create one table that has all the required info:

create table countries (
    _id integer primary key autoincrement,
    country_code    char(2),
    country_name    varchar,
    phone_code      char(4),
    editable        integer
);

Then store your country information into this table. When populating your list of countries, use this table instead of XML; display country names and associate country codes with each corresponding list item. Then on selection, use the country code to get the phone code and the 'editable' flag - and act upon this info.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top