Android Simple Spinner


SUBMITTED BY: indoincome

DATE: March 18, 2017, 4:40 a.m.

UPDATED: Nov. 5, 2017, 5:08 p.m.

FORMAT: Text only

SIZE: 4.8 kB

HITS: 2722

  1. 1. List of Items in Spinner
  2. Open “res/values/strings.xml” file, define the list of items that will display in Spinner (dropdown list).
  3. ======= File : res/values/strings.xml =============
  4. [tpcode]
  5. <?xml version="1.0" encoding="utf-8"?>
  6. <resources>
  7. <string name="app_name">MyAndroidApp</string>
  8. <string name="country_prompt">Choose a country</string>
  9. <string-array name="country_arrays">
  10. <item>Malaysia</item>
  11. <item>United States</item>
  12. <item>Indonesia</item>
  13. <item>France</item>
  14. <item>Italy</item>
  15. <item>Singapore</item>
  16. <item>New Zealand</item>
  17. <item>India</item>
  18. </string-array>
  19. </resources>
  20. [/tpcode]
  21. 2. Spinner (DropDown List)
  22. Open “res/layout/main.xml” file, add two spinner components and a button.
  23. In “spinner1”, the “android:entries” represents the selection items in spinner.
  24. In “spinner2”, the selection items will be defined in code later.
  25. ============== File : res/layout/main.xml ==============
  26. [tpcode] <?xml version="1.0" encoding="utf-8"?>
  27. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  28. android:layout_width="fill_parent"
  29. android:layout_height="fill_parent"
  30. android:orientation="vertical" >
  31. <Spinner
  32. android:id="@+id/spinner1"
  33. android:layout_width="match_parent"
  34. android:layout_height="wrap_content"
  35. android:entries="@array/country_arrays"
  36. android:prompt="@string/country_prompt" />
  37. <Spinner
  38. android:id="@+id/spinner2"
  39. android:layout_width="match_parent"
  40. android:layout_height="wrap_content" />
  41. <Button
  42. android:id="@+id/btnSubmit"
  43. android:layout_width="wrap_content"
  44. android:layout_height="wrap_content"
  45. android:text="Submit" />
  46. </LinearLayout>
  47. [/tpcode]
  48. 3. Code Code
  49. Read the code and also code’s comment, it should be self-explanatory.
  50. ============== File : MyAndroidAppActivity.java ========
  51. [tpcode] package com.mkyong.android;
  52. import java.util.ArrayList;
  53. import java.util.List;
  54. import android.app.Activity;
  55. import android.os.Bundle;
  56. import android.view.View;
  57. import android.view.View.OnClickListener;
  58. import android.widget.ArrayAdapter;
  59. import android.widget.Button;
  60. import android.widget.Spinner;
  61. import android.widget.Toast;
  62. public class MyAndroidAppActivity extends Activity {
  63. private Spinner spinner1, spinner2;
  64. private Button btnSubmit;
  65. @Override
  66. public void onCreate(Bundle savedInstanceState) {
  67. super.onCreate(savedInstanceState);
  68. setContentView(R.layout.main);
  69. addItemsOnSpinner2();
  70. addListenerOnButton();
  71. addListenerOnSpinnerItemSelection();
  72. }
  73. // add items into spinner dynamically
  74. public void addItemsOnSpinner2() {
  75. spinner2 = (Spinner) findViewById(R.id.spinner2);
  76. List<String> list = new ArrayList<String>();
  77. list.add("list 1");
  78. list.add("list 2");
  79. list.add("list 3");
  80. ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,
  81. android.R.layout.simple_spinner_item, list);
  82. dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
  83. spinner2.setAdapter(dataAdapter);
  84. }
  85. public void addListenerOnSpinnerItemSelection() {
  86. spinner1 = (Spinner) findViewById(R.id.spinner1);
  87. spinner1.setOnItemSelectedListener(new CustomOnItemSelectedListener());
  88. }
  89. // get the selected dropdown list value
  90. public void addListenerOnButton() {
  91. spinner1 = (Spinner) findViewById(R.id.spinner1);
  92. spinner2 = (Spinner) findViewById(R.id.spinner2);
  93. btnSubmit = (Button) findViewById(R.id.btnSubmit);
  94. btnSubmit.setOnClickListener(new OnClickListener() {
  95. @Override
  96. public void onClick(View v) {
  97. Toast.makeText(MyAndroidAppActivity.this,
  98. "OnClickListener : " +
  99. "\nSpinner 1 : "+ String.valueOf(spinner1.getSelectedItem()) +
  100. "\nSpinner 2 : "+ String.valueOf(spinner2.getSelectedItem()),
  101. Toast.LENGTH_SHORT).show();
  102. }
  103. });
  104. }
  105. }
  106. [/tpcode]
  107. ========= File : CustomOnItemSelectedListener.java ============
  108. [tpcode] package com.mkyong.android;
  109. import android.view.View;
  110. import android.widget.AdapterView;
  111. import android.widget.AdapterView.OnItemSelectedListener;
  112. import android.widget.Toast;
  113. public class CustomOnItemSelectedListener implements OnItemSelectedListener {
  114. public void onItemSelected(AdapterView<?> parent, View view, int pos,long id) {
  115. Toast.makeText(parent.getContext(),
  116. "OnItemSelectedListener : " + parent.getItemAtPosition(pos).toString(),
  117. Toast.LENGTH_SHORT).show();
  118. }
  119. @Override
  120. public void onNothingSelected(AdapterView<?> arg0) {
  121. // TODO Auto-generated method stub
  122. }
  123. }[/tpcode]

comments powered by Disqus