1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
| <?xml version="1.0" encoding="utf-8"?> android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" android:padding="10dp"> <Spinner android:id="@+id/spinnerWeekDays" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:prompt="@string/weekday_prompt"/> <EditText android:id="@+id/editText" android:layout_width="match_parent" android:layout_height="wrap_content" android:inputType="text" android:layout_marginTop="20dp"> <requestFocus /> </EditText></LinearLayout> |
1
2
3
4
5
6
7
8
9
10
11
12
| <!-- weekday_prompt string is the string that initially show in spinner --> <string name="weekday_prompt">Choose a Week Day</string> <!-- weekday_array is the item list we going to show for selection in spinner --> <string-array name="weekday_array"> <item>Saturday</item> <item>Sunday</item> <item>Monday</item> <item>Tuesday</item> <item>Wednesday</item> <item>Thursday</item> <item>Friday</item> </string-array> |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
| <?xml version="1.0" encoding="utf-8"?><resources> <string name="hello">Hello World, AndroidSpinnerExampleActivity!</string> <string name="app_name">AndroidSpinnerExample</string> <!-- weekday_prompt string is the string that initially show in spinner --> <string name="weekday_prompt">Choose a Week Day</string> <!-- weekday_array is the item list we going to show for selection in spinner --> <string-array name="weekday_array"> <item>Saturday</item> <item>Sunday</item> <item>Monday</item> <item>Tuesday</item> <item>Wednesday</item> <item>Thursday</item> <item>Friday</item> </string-array> </resources> |
1
2
3
4
5
6
| <Spinner android:id="@+id/spinnerWeekDays" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:prompt="@string/weekday_prompt"/> |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
| package android.guide.spinner;import android.app.Activity;import android.os.Bundle;import android.view.View;import android.widget.AdapterView;import android.widget.AdapterView.OnItemSelectedListener;import android.widget.ArrayAdapter;import android.widget.EditText;import android.widget.Spinner;public class AndroidSpinnerExampleActivity extends Activity { /** Called when the activity is first created. */ //define EditText EditText selectedWeekDay; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); //get the EditText form resource selectedWeekDay = (EditText) findViewById(R.id.editText); //get the spinner from resource Spinner weekDays = (Spinner) findViewById(R.id.spinnerWeekDays); //then defining ArrayAdapter using weekday_array ArrayAdapter<CharSequence> adapter =ArrayAdapter.createFromResource(this, R.array.weekday_array, android.R.layout.simple_spinner_item); //set the appearance of widget items that show when open the widget adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); //set the adapter to spinner weekDays.setAdapter(adapter); //set the action listener weekDays.setOnItemSelectedListener(listener); } // implement action listener type of OnItemSelectedListener private OnItemSelectedListener listener =new OnItemSelectedListener() { //do what ever you want to do when item selected public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) { //i get the item using selected item position and set it into selectedWeekDay selectedWeekDay.setText(parent.getItemAtPosition(pos).toString()); } public void onNothingSelected(AdapterView<?> parent) { } };} |
Labels: All softwares, Android, How To Work With Android Spinner Widget