package com.example.androidspinner;
import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.Toast;
public class MainActivity extends Activity {
Spinner mySpinner;
String[] spinnerArray ={ "One", "Two", "Three", "Four", "Five"};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mySpinner = (Spinner)findViewById(R.id.myspinner);
ArrayAdapter<String> myArrayAdapter = new ArrayAdapter<String>(
this,
android.R.layout.simple_spinner_item,
spinnerArray);
myArrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item );
mySpinner.setAdapter(myArrayAdapter);
mySpinner.setOnItemSelectedListener(myOnItemSelectedListener);
}
OnItemSelectedListener myOnItemSelectedListener
= new OnItemSelectedListener(){
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position,
long id) {
String selectedItem = (String) parent.getItemAtPosition(position);
Toast.makeText(
getApplicationContext(),
selectedItem,
Toast.LENGTH_LONG)
.show();
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}};
}
Create Spinner from String array
To create Spinner from String array:
0 Response to "Create Spinner from String array"
Posting Komentar