[Android] Spinner Android 2011. 12. 20. 16:11
스피너는 선택하면 마치 팝업 처럼 나타난다.

리스트 같은 경우는 한번에 다 나타나는 케이스지만 이것은 클릭을 하면 팝업 형태로 주르륵 나타난다.

모바일 같이 협소한 공간에선 유용한 것이다.

xml

<TextView
        android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:layout_alignParentLeft="true"
     android:id="@+id/text"
     android:text="select"
    />
    <Spinner
        android:layout_width="fill_parent"
     android:layout_height="wrap_content"
     android:id="@+id/spinner"
    />  

java

ArrayAdapter<CharSequence> arr;
    /** Called when the activity is first created. */
 @Override   
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
       
        Spinner spinner = (Spinner)findViewById(R.id.spinner);
        spinner.setPrompt("Select");
        arr = ArrayAdapter.createFromResource(this, R.array.array, android.R.layout.simple_spinner_item);
        arr.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinner.setAdapter(arr);
       
        spinner.setOnItemSelectedListener(new OnItemSelectedListener(){
         public void onItemSelected(AdapterView<?> parent, View view, int index, long id){
          Toast.makeText(ImageViewActivity.this, arr.getItem(index), Toast.LENGTH_SHORT).show();
         }
         public void onNothingSelected(AdapterView<?> parent){
          
         }
        });
 }

실행을 하기 전에 하나 짚고 가자면 array xml을 꼭 생성하고 어댑터에 리소스를 연결 하는 것이 중요하다.

물론 사후 관리를 위해서이다.