Input Controls Spinners
Input Controls Spinners
Spinners
Spinner
• In Android, Spinner provides a quick way to select
one value from a set of values.
• Android spinners are nothing but the drop down-list
seen in other programming languages.
• In a default state, a spinner shows its currently
selected value.
• Spinner is
associated with
Adapter view so to
fill the data in
spinner we need to
use one of the
Adapter class.
Spinner - ArrayAdapter
</RelativeLayout>
Example of Spinner :open app-> java -> package -> MainActivity.java
package example.abhiandriod.spinnerexample; import android.support.v7.app.AppCompatActivity;import
android.os.Bundle;import android.view.View;import android.widget.AdapterView;
import android.widget.ArrayAdapter;import android.widget.Spinner; import android.widget.Toast;
public class MainActivity extends AppCompatActivity implements AdapterView.OnItemSelectedListener{
String[] bankNames={"BOI","SBI","HDFC","PNB","OBC"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Getting the instance of Spinner and applying OnItemSelectedListener on it
Spinner spin = (Spinner) findViewById(R.id.simpleSpinner);
spin.setOnItemSelectedListener(this);
//Creating the ArrayAdapter instance having the bank name list
ArrayAdapter aa = new ArrayAdapter(this,android.R.layout.simple_spinner_item,bankNames);
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) {
Toast.makeText(getApplicationContext(), bankNames[position], Toast.LENGTH_LONG).show();}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
}}
Menu in Android