package com.example.spinnercountries;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Locale;
import android.os.Bundle;
import android.app.Activity;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
public class MainActivity extends Activity {
private Spinner country;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
country = (Spinner) findViewById(R.id.spinner1);
Locale[] locales = Locale.getAvailableLocales();
ArrayList<String> countries = new ArrayList<String>();
for (Locale locale : locales) {
String country = locale.getDisplayCountry();
if (country.trim().length() > 0 && !countries.contains(country)) {
countries.add(country);
}
}
Collections.sort(countries);
for (String country : countries) {
System.out.println(country);
}
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, countries);
// set the view for the Drop down list
dataAdapter
.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// set the ArrayAdapter to the spinner
country.setAdapter(dataAdapter);
country.setSelection(37);
System.out.println("# countries found: " + countries.size());
}
}
Activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
<Spinner
android:id="@+id/spinner1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView1"
android:layout_below="@+id/textView1"
android:layout_marginTop="33dp" />
</RelativeLayout>
Sample Output
-----------------------------------
import java.util.ArrayList;
import java.util.Collections;
import java.util.Locale;
import android.os.Bundle;
import android.app.Activity;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
public class MainActivity extends Activity {
private Spinner country;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
country = (Spinner) findViewById(R.id.spinner1);
Locale[] locales = Locale.getAvailableLocales();
ArrayList<String> countries = new ArrayList<String>();
for (Locale locale : locales) {
String country = locale.getDisplayCountry();
if (country.trim().length() > 0 && !countries.contains(country)) {
countries.add(country);
}
}
Collections.sort(countries);
for (String country : countries) {
System.out.println(country);
}
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, countries);
// set the view for the Drop down list
dataAdapter
.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// set the ArrayAdapter to the spinner
country.setAdapter(dataAdapter);
country.setSelection(37);
System.out.println("# countries found: " + countries.size());
}
}
Activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
<Spinner
android:id="@+id/spinner1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView1"
android:layout_below="@+id/textView1"
android:layout_marginTop="33dp" />
</RelativeLayout>
Sample Output
-----------------------------------
AWESOME! Thank you!
ReplyDeleteMore helpful tutorial !
ReplyDeletethanks
how can use this code to get the countries in arabic
ReplyDeletehow can use this code to get the countries in arabic
ReplyDeleteI liked your blog.Thanks for your interest in sharing the information.keep updating.
ReplyDeleteBest Spoken English Institute in Adyar
Spoken English Classes in Shastri Nagar
Spoken English Classes in Kottivakkam
Spoken English Classes near Porur
Spoken English Class Maduravoyal Chennai
Spoken English in Ramapuram
Spoken English Training near me
OctaFx Review helps identify trading opportunities using actions of Market Participants through charts, patterns, and indicators. It seeks to answer the question: "How Will Market Participants React To Current And Future Events?"
ReplyDelete