Wednesday, 24 July 2019

Android spinner


import android.view.View;
import android.widget.*;
import android.widget.AdapterView.OnItemSelectedListener;
public class MainActivity extends Activity implements OnItemSelectedListener{
Spinner s1;
String[] country = { "India", "USA", "China", "Japan", "Other",  }; 
            @Override
            protected void onCreate(Bundle savedInstanceState) {
                        super.onCreate(savedInstanceState);
                        setContentView(R.layout.activity_main);
                        s1=(Spinner)findViewById(R.id.spinner1);
                        s1.setOnItemSelectedListener(this);
//Creating the ArrayAdapter instance having the country list 
ArrayAdapter<String> aa = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,country); 
        aa.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 
   //Setting the ArrayAdapter data on the Spinner 
        s1.setAdapter(aa); 
}
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
        String item = parent.getItemAtPosition(position).toString();
        Toast.makeText(parent.getContext(), "Selected: " + item, Toast.LENGTH_LONG).show();
    }
public void onNothingSelected(AdapterView<?> arg0)
   {
   }
}

No comments:

Post a Comment