본문 바로가기

안드로이드

안드로이드_스피너(Spinner)

- 스피너

    - 여러 개의 데이터 중에서 하나를 선택하는 방법을 제공하는 것

    - 스피너 터치 시 선택 가능한 모든 값을 포함하는 드롭다운 메뉴 표시

- 사용

    [activity_main.xml]에 Spinner 생성 및 이미지 뷰 생성

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
<LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
 
        <TextView
            android:id="@+id/title"
            android:layout_width="match_parent"
            android:layout_height="100dp"
            android:layout_marginBottom="15dp"
            android:background="#3FBBA4"
            android:gravity="center"
            android:text="Spinner 적용해보기"
            android:textSize="30sp"
            android:textStyle="bold" />
 
        <Spinner
            android:id="@+id/spinner"
            android:layout_width="300dp"
            android:layout_height="70dp"
            android:layout_gravity="center"
            android:background="#EAD9D9" />
 
        <ImageView
            android:id="@+id/snackImg"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            android:layout_marginBottom="10dp"
            android:layout_gravity="center"/>
 
        <TextView
            android:id="@+id/snackName"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="좋아하는 과자는?"
            android:textSize="20sp" />
 
    </LinearLayout>
cs

    [MainActivity.java]에 Adapter 설정, 배열에 데이터 입력

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
public class MainActivity extends AppCompatActivity {
    //--- dataBinding
    ActivityMainBinding binding;
 
    //--- 과자 목록
    ArrayList<String> snackList = new ArrayList<>(Arrays.asList(
            new String[] {"선택하기""바나나킥""배배""눈을감자""고래밥""꼬북칩"}));
 
    //--- Adapter
    ArrayAdapter<String> arrayAdapter;
 
    //--- 과자 이미지
    ArrayList<Integer> Imgs =
            new ArrayList<>(Arrays.asList(new Integer[] {0, R.drawable.bananakick, R.drawable.baebae, R.drawable.nungam, R.drawable.goraebap, R.drawable.kkobuk}));
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this, R.layout.activity_main);
 
        //--- Adapter 설정, snackList 인자로 전달
        arrayAdapter = new ArrayAdapter<>(getApplicationContext(), android.R.layout.simple_spinner_dropdown_item, snackList);
        binding.spinner.setAdapter(arrayAdapter);
        
//        XML 파일로 배열 생성한 경우
//        //--- Adapter 설정, snack_array 인자로 전달
//        arrayAdapter = ArrayAdapter.createFromResource(this, R.array.snack_array,
//                android.R.layout.simple_spinner_item);
//        arrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
//        binding.spinner.setAdapter(arrayAdapter);
 
        //--- Spinner에서 Item 선택 시
        binding.spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                setView(position);
            }
            @Override
            public void onNothingSelected(AdapterView<?> parent) { }
        });
    }
 
    private void setView(int position) {
        binding.snackImg.setImageResource(Imgs.get(position));
        binding.snackName.setText(snackList.get(position) + " 선택!!");
    }
}
cs

 

- 결과