Android has spinner structure like comboboxes in html.
I will explain to create default spinner in 2 ways.
First one is declaring resources as static in xml
Second one is declaring resources as programmatically in Java
First
You should declare your spinner resource in xml layout file such as
<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">
<Spinner
android:id="@+id/declaredSpinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_marginTop="133dp"
android:entries="@array/resources" />
</RelativeLayout>
And then you should create a resources in strings.xml file
<string-array name="country_arrays">
<item>Malaysia</item>
<item>India</item>
<item>Indonesia</item>
<item>France</item>
<item>Italy</item>
<item>Singapore</item>
<item>New Zealand</item>
</string-array>
And then you should handle your spinner functions in you activity
Spinner declaredSpinner= (Spinner) findViewById(R.id.declaredSpinner);
declaredSpinner.setOnItemSelectedListener(new yourListener());
you should create your own class as inner in your activity
class YourListener(){
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "Selected", Toast.LENGTH_LONG).show();
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "Not Selected", Toast.LENGTH_LONG).show();
}
}
If you do not want to write inner class you can use class in function
declaredSpinner.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
// TODO Auto-generated method stub
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
Second one needs to use adapter because you are creating your resources programmatically and you should
bind them through the spinner resources
So firstly you will declare your layout file such as below
<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">
<Spinner
android:id="@+id/declaredSpinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_marginTop="133dp"
/>
</RelativeLayout>
If you notice it is different according to the first.
Secondly you will open your activity class
1 ) Create your string arrays
String[] ustspinner = new String[5];
for(int i = 0 ; i < 5 ; i++){
ustspinner = ""+i;
}
2 ) create your arrayadapter with your string arrays
ArrayAdapter<String> defaultAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item, ustsolspiner);
defaultAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
3 ) then set to your spinner
spinnerReference.setAdapter(defaultAdapter);
4 ) You can define your functions as the previous one.
The best one is creating your own customized spinner.
For example i want an image and then a text in my spinner how can we do?
We must create CustomizedAdapter.
Steps :
1 ) Create your own layout file in layout folder. Such as customizedspinner.xml
2 ) Get a reference to spinner
3 ) Create your own Customized spinners adapter that is extended from ArrayAdapter
4 ) set your adapter object to your spinner reference
-->
<?xml version="1.0" encoding="utf-8"?>
<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="0dip"
android:layout_weight="0.74"
android:background="@drawable/textdrawable">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent" >
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ido"
android:contentDescription="@string/img" />
</LinearLayout>
<TextView
android:id="@+id/spinerdatetext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/blankForDate"
android:textAppearance="?android:attr/textAppearanceMedium" />
</LinearLayout>
</LinearLayout>
class SpAD extends ArrayAdapter<String>{
@Override
public int getPosition(String item) {
// TODO Auto-generated method stub
return 4;
}
public SpAD(Context context, int textViewResourceId, String[] objects) {
super(context, textViewResourceId, objects);
}
@Override
public View getDropDownView(int position, View convertView,ViewGroup parent) {
return getCustomView(position, convertView, parent);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater=getLayoutInflater();
View row=inflater.inflate(R.layout.spinerdate, parent, false);
TextView label=(TextView)row.findViewById(R.id.spinerdatetext);
label.setText(tenDays[position]);
ImageView icon=(ImageView)row.findViewById(R.id.imageView1);
icon.setImageResource(R.drawable.ido);
return row;
}
}
//Come to your own activity file
String[] days = tenDays();
SpAD dataAdapter1 = new SpAD(this,R.layout.customizedspinner, days);
spinerdate.setAdapter(dataAdapter1);