Android spinner like that drop down Option menu and user select only single item at a time. the android spinner view classs is sub class of AsbSpinner class.
In this tutorial explain Spinner two ways
- Android spinner with String Array get from <string-array name="beginnner">
<string-array name="beginnner">
<item>Red</item>
<item>Black</item>
<item>Yellow</item>
<item>Green</item>
<item>White</item>
</string-array>
.activity_main.xml
.................................................................................................................................................................
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical" >
<TextView
android:id="@+id/text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:gravity="center"
android:text="Android Beginner tutorials"
android:textSize="20dp" />
<Spinner
android:id="@+id/spinner1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:entries="@array/beginnner" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="40dp"
android:text="set your color Name" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge" />
</LinearLayout>
MainActivity.java
...................................................................................................................................................................
package com.androidbeginnerpoint.testapplication;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
@SuppressLint("NewApi")
public class MainActivity extends Activity {
TextView text;
@SuppressLint("NewApi")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getActionBar().setTitle("Horizontal Listview");
setContentView(R.layout.activity_main);
text=(TextView)findViewById(R.id.textView1);
final Spinner spin=(Spinner)findViewById(R.id.spinner1);
spin.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
// TODO Auto-generated method stub
int str=spin.getSelectedItemPosition();
String str2=spin.getSelectedItem().toString();
Toast.makeText(getApplication(), String.valueOf(str)+"\n"+str2, Toast.LENGTH_LONG).show();
text.setText(str2);
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
}
}
- Android spinner Associated with AdapterView
MainActivity.java
.................................................................................................................................................................
package com.androidbeginnerpoint.testapplication;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity implements
AdapterView.OnItemSelectedListener {
TextView text;
Spinner spin;
String[] country = { "RED", "BLACK", "YELLOW", "WHITE", "BLUE","PINK","GREEN" };
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
text=(TextView)findViewById(R.id.textView1);
spin=(Spinner)findViewById(R.id.spinner1);
spin.setOnItemSelectedListener(this);
//Creating the ArrayAdapter instance having the country list
ArrayAdapter aa = new ArrayAdapter(this,android.R.layout.simple_spinner_item,country);
aa.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
//Setting the ArrayAdapter data on the Spinner
spin.setAdapter(aa);
}
//Performing action onItemSelected and onNothing selected
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int position,long id) {
int str=spin.getSelectedItemPosition();
String str2=spin.getSelectedItem().toString();
Toast.makeText(getApplication(), String.valueOf(str)+"\n"+str2, Toast.LENGTH_LONG).show();
text.setText(str2);
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
}
0 comments