Tuesday, 25 August 2015

Android get list of Countries in Spinner



        Here I am going to explain how to get the list  of Countries in Spinner


                        



  Step 1).group.xml

    
 <?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >

    <solid android:color="#E6E6E6" />

    <padding
        android:bottom="2dp"
        android:left="2dp"
        android:right="2dp"
        android:top="2dp" />

</shape>


Step 2).activity.xml


<RelativeLayout 
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:background="#7faaff" >

<RelativeLayout 

  android:layout_width="match_parent"
  android:layout_height="124dp"
  android:layout_centerInParent="true"
  android:layout_margin="20dp"
  android:layout_marginTop="30dp"
  android:background="@drawable/group" >

<EditText
  android:id="@+id/editText1"
  android:layout_width="wrap_content"
  android:layout_height="40dp"
  android:layout_alignParentLeft="true"
  android:layout_alignParentRight="true"
  android:background="@null"
  android:ems="10"
  android:hint="Name"
  android:imeOptions="actionNext"
  android:padding="10dp"
  android:singleLine="true" >
</EditText>

<View

  android:id="@+id/view1"
  android:layout_width="fill_parent"
  android:layout_height="1dp"
  android:layout_below="@+id/editText1"
  android:background="#c0c0c0" />

<EditText

  android:id="@+id/editText2"
  android:layout_width="match_parent"
  android:layout_height="40dp"
  android:layout_below="@+id/view1"
  android:background="@null"
  android:ems="10"
  android:hint="State"
  android:imeOptions="actionNext"
  android:padding="10dp"
  android:singleLine="true" >
</EditText>

<View
  android:id="@+id/view2"
  android:layout_width="fill_parent"
  android:layout_height="1dp"
  android:layout_below="@+id/editText2"
  android:background="#c0c0c0" />

<EditText

  android:id="@+id/editText3"
  android:layout_width="match_parent"
  android:layout_height="50dp"
  android:layout_alignRight="@+id/view2"
  android:layout_below="@+id/view2"
  android:background="@null">
   
</EditText>
</RelativeLayout >

</RelativeLayout >

  Step 3).MainActivity.java

 import java.util.ArrayList;
 import java.util.Locale;
 import android.app.Activity;
 import android.os.Bundle;
 import android.widget.ArrayAdapter;
 import android.widget.Spinner;

public class MainActivity extends Activity {
Spinner country;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
country= (Spinner) findViewById(R.id.editText3);
ArrayList<String> list = new ArrayList<String>();

String[] locales = Locale.getISOCountries();

for (String countryCode : locales) {

Locale obj = new Locale("", countryCode);
                        list .add(obj.getDisplayCountry());

}

ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, list);
        dataAdapter .setDropDownViewResource
                                    (android.R.layout.simple_spinner_dropdown_item);
       country.setAdapter(dataAdapter );
}

}

Happy Coding:)