본문 바로가기

안드로이드

[Android Studio] Drop down 사용하기

728x90

 

xml 

 

<com.google.android.material.textfield.TextInputLayout
    android:id="@+id/til_region_code"
    style="@style/RegionTextInputLayoutStyle"
    android:layout_width="@dimen/size128"
    android:layout_height="@dimen/size50"
    android:layout_marginTop="@dimen/size20"
    app:layout_constraintStart_toStartOf="@id/guide_start_20"
    app:layout_constraintTop_toBottomOf="@+id/tv_license_num_text">

   <AutoCompleteTextView
        android:id="@+id/autoCompleteTextView"
        style="@style/fs_body_1_md"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="top"
        android:hint="지역코드"
        android:inputType="none"
        android:textColor="@color/grayscale_09"
        android:textColorHint="@color/grayscale_06" />
        
</com.google.android.material.textfield.TextInputLayout>

 

 

style 

<style name="RegionTextInputLayoutStyle" parent="Widget.MaterialComponents.TextInputLayout.OutlinedBox.ExposedDropdownMenu">
    <!-- 테두리 -->
    <item name="boxStrokeColor">@color/selector_text_input_color</item>
    <item name ="strokeColor">@color/grayscale_04</item>
    <item name="boxStrokeWidth">@dimen/size01</item>
    <item name="boxStrokeWidthFocused">@dimen/size01</item>
    <item name="boxCornerRadiusTopStart">@dimen/size08</item>
    <item name="boxCornerRadiusTopEnd">@dimen/size08</item>
    <item name="boxCornerRadiusBottomStart">@dimen/size08</item>
    <item name="boxCornerRadiusBottomEnd">@dimen/size08</item>

    <item name="endIconDrawable">@drawable/svg_icon_arrow_down_24_dp</item>

    <!-- 에러 -->
    <item name="errorEnabled">true</item>
    <item name="colorError">@color/red</item>
    <item name="errorIconDrawable">@null</item>
    <item name="errorTextAppearance">@style/fs_body_2</item>

    <!-- 힌트 -->
    <item name="hintEnabled">false</item>
</style>

 

 

activity 

 

private fun regionCodeInit() {
    val regionArray = resources.getStringArray(R.array.license_drop_down_list)
    val arrayAdapter = ArrayAdapter(this, R.layout.item_license_drop_down, regionArray)
    binding.autoCompleteTextView.setAdapter(arrayAdapter)

    binding.autoCompleteTextView.setOnItemClickListener { parent, view, position, id ->
        when (position) {
            0 -> Toast.makeText(this, "ExposedDropDown 1 Clicked", Toast.LENGTH_SHORT).show()
            1 -> Toast.makeText(this, "ExposedDropDown 2 Clicked", Toast.LENGTH_SHORT).show()
            2 -> Toast.makeText(this, "ExposedDropDown 3 Clicked", Toast.LENGTH_SHORT).show()
            3 -> Toast.makeText(this, "ExposedDropDown 4 Clicked", Toast.LENGTH_SHORT).show()
        }
    }

    binding.autoCompleteTextView.setOnClickListener {
        binding.tilRegionCode.setEndIconDrawable(R.drawable.svg_icon_arrow_up_bold_24_dp)

    }

    binding.autoCompleteTextView.setOnDismissListener {
        binding.tilRegionCode.setEndIconDrawable(R.drawable.svg_icon_arrow_down_24_dp)
    }
}

 

 

string 

 

<string-array name="license_drop_down_list">
    <item>서울특별시</item>
    <item>인천광역시</item>
    <item>대전광역시</item>
    <item>광주광역시</item>
    <item>대구광역시</item>
    <item>울산광역시</item>
    <item>부산광역시</item>
    <item>세종특별자치시</item>
    <item>광주광역시</item>
    <item>강원도</item>
    <item>충청북도</item>
    <item>충청남도</item>
    <item>전라북도</item>
    <item>전라남도</item>
    <item>경상북도</item>
    <item>경상남도</item>
    <item>제주특별자치도</item>
</string-array>

 

 

 

dropdown 아이템 

 

<?xml version="1.0" encoding="utf-8"?>
<TextView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/textView"
    android:layout_width="match_parent"
    android:maxLines="1"
    android:ellipsize="end"
    android:textColor="@color/black"
    android:textStyle="bold"
    android:padding="14dp"
    android:layout_height="wrap_content"
    android:text="TextView" />

 

상위 레이아웃을 안넣는것이 포인트