[Android] Spinner in Android

spinner allows us to have 1 available picker like this:
spinner in android

To be able to set the values ​​for spinner I have 2 how to perform the preset value in the file res / values ​​/ strings.xml or placed directly in your * .java.

First we do 1 the simple interface.
file *.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
    <LinearLayout 
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
	    <TextView 
	        android:layout_width="match_parent"
	        android:layout_height="wrap_content"
	        android:gravity="center_horizontal"
	        android:text="My Spinner 1"/>
    </LinearLayout>
    
    <LinearLayout 
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
	    <Spinner 
	        android:id="@+id/spin"
	        android:layout_width="wrap_content"
	        android:layout_height="wrap_content"
	        android:gravity="center_horizontal"/>
    </LinearLayout>
    
</LinearLayout>

Then we can look in the Graphics tab cal following form (bad breath but after run will look like the image above)
spinner in android

Next we install the value in the spinner:

How to 1: Install Now in Java.

package android.spinner;

import android.os.Bundle;
import android.app.Activity;
import android.widget.ArrayAdapter;
import android.widget.Spinner;

public class MainActivity extends Activity {
	Spinner sp;
	ArrayAdapter<String> adapter; //tạo vector adapter để truyền vào spinner
	String numbers[] = { "ONE", "TWO", "THREE", "FOUR", "FIVE", "SIX", "SEVEN", "EIGHT", "NINE", "TEN" };
	// number[] là các giá trị của spinner khi hiện ra
	public void onCreate(Bundle savedInstanceState){
		super.onCreate(savedInstanceState);
	   	setContentView(R.layout.activity_main);
	   	
	   	sp = (Spinner) findViewById(R.id.spin);
	   	//set các giá trị của number vào adapter
	   	adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, numbers);
	   	//set adapter vào sp (spinner)
	 	sp.setAdapter(adapter);
	 }
}

The results of our:
spinner in android

How to 2: Install the values ​​from the file res / values ​​/ strings.xml

As well as interface installed on. We add the following code strings.xml file:

<string-array name = "day"> <!--Tên của mảng-->
	    <item>Monday</item>
	    <item>Tuesday</item>
	    <item>Wednesday</item>
	    <item>Thursday</item>
	    <item>Firday</item>
	    <item>Saturday</item>
	    <item>Sunday</item>
	</string-array>

Next, we work with the * .java

package android.spinner1;

import android.os.Bundle;
import android.app.Activity;
import android.widget.ArrayAdapter;
import android.widget.Spinner;

public class MainActivity extends Activity {
	ArrayAdapter<CharSequence> adapter;
	Spinner sp;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		sp = (Spinner) findViewById(R.id.spin);
		//Lấy giá trị của mảng "day" trong file string.xml bằng lệnh sau ("R.array.day" là tên mảng giá trị trong file string)
		adapter = ArrayAdapter.createFromResource(this, R.array.day, android.R.layout.simple_spinner_item); 
		//set các giá trị vào spinner
		sp.setAdapter(adapter);
	}
}

And our results:
spinner in android