4
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

【Android Studio】画像スピナー(プルダウン)作成

Posted at

スピナーに画像をセットする処理。
作成するのに少々時間がかかったのでメモ。

スピナーアイテムのレイアウト作成

各々の画像表示用のアイテム

item.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
	xmlns:android="http://schemas.android.com/apk/res/android"
	xmlns:app="http://schemas.android.com/apk/res-auto"
	android:layout_width="60dp"
	android:background="#FFFFFF"
	android:layout_height="40dp">

	<ImageView
		android:id="@+id/image_view"
		android:layout_width="wrap_content"
		android:layout_height="wrap_content"
		android:contentDescription="@null"
		app:layout_constraintBottom_toBottomOf="parent"
		app:layout_constraintEnd_toEndOf="parent"
		app:layout_constraintStart_toStartOf="parent"
		app:layout_constraintTop_toTopOf="parent"/>
</androidx.constraintlayout.widget.ConstraintLayout>

Adapterの作成

getDropDownViewのコメントアウトを外すと、ドロップダウンのビューと表示ビューを切り分けることができるようになる。

SpinnerAdapter.kt
class SpinnerAdapter(private val imageResIdList: List<Int>) : BaseAdapter() {

	override fun getView(position: Int, convertView: View?, parent: ViewGroup?): View {
		val view = convertView ?: LayoutInflater.from(parent?.context)
			.inflate(R.layout.item, parent, false)
		view.findViewById<ImageView>(R.id.image_view).setImageResource(imageResIdList[position])
		return view
	}

	override fun getItem(position: Int): Any {
		return imageResIdList[position]
	}

	override fun getCount(): Int {
		return imageResIdList.size
	}

	override fun getItemId(position: Int): Long {
		return position.toLong()
	}

	/*
	override fun getDropDownView(position: Int, convertView: View?, parent: ViewGroup?): View {
		val view = convertView ?: LayoutInflater.from(parent?.context)
			.inflate(R.layout.dropdown_item, parent, false)
		view.findViewById<ImageView>(R.id.image_view).setImageResource(imageResIdList[position])
		return view
	}
	 */
}

Activityレイアウト作成

メインビュー内にスピナーを配置。

activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
	xmlns:android="http://schemas.android.com/apk/res/android"
	xmlns:app="http://schemas.android.com/apk/res-auto"
	xmlns:tools="http://schemas.android.com/tools"
	android:layout_width="match_parent"
	android:layout_height="match_parent"
	tools:context=".MainActivity">

	<Spinner
		android:id="@+id/spinner"
		android:layout_width="wrap_content"
		android:layout_height="wrap_content"
		app:layout_constraintBottom_toBottomOf="parent"
		app:layout_constraintEnd_toEndOf="parent"
		app:layout_constraintStart_toStartOf="parent"
		app:layout_constraintTop_toTopOf="parent"/>
</androidx.constraintlayout.widget.ConstraintLayout>

Acitiviyの処理作成

スピナーにアダプターをセット。

MainActivity.kt
binding.spinner.adapter = SpinnerAdapter(listOf(R.drawable.check_icon, R.drawable.clear_icon, R.drawable.square_icon))
4
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?