I am creating a custom spinner with a custom background as that will hide the difficult dropdown, that's why I am setting a custom layout to show the dropdown. but when I am adding the adapter to the spinner the app is crashing? why? This is the spinner code
<Spinner
android:id="@+id/spinner"
android:layout_width="300dp"
android:layout_height="40dp"
android:layout_marginTop="70dp"
android:padding="10dp"
android:background="@drawable/border"
android:spinnerMode="dropdown"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"/>
This is drawable border
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<stroke android:color="@color/text" android:width="1dp"/>
<corners android:radius="15dp"/>
</shape>
this is the custom layout ---list_item.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto">
<ImageView
android:id="@+id/imageViewDropdown"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/dropdown"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"/>
<TextView
android:id="@+id/textViewItem"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:textSize="20sp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toStartOf="@id/imageViewDropdown"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"/>
</androidx.constraintlayout.widget.ConstraintLayout>
This is my fragment code
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
val spinner=binding.spinner
val spinnerAdapter = ArrayAdapter(
requireContext(),
android.R.layout.simple_dropdown_item_1line,
this.listitem
)//this is working fine
// val spinnerAdapter = ArrayAdapter(
// requireContext(),
// R.layout.list_item,
// this.listitem
// ) This is I wanted to do
spinner.adapter = spinnerAdapter
}
The error I am getting
Caused by: java.lang.ClassCastException: androidx.constraintlayout.widget.ConstraintLayout cannot be cast to android.widget.TextView
Process: com.example.assignmentbackcoffer, PID: 32542
java.lang.IllegalStateException: ArrayAdapter requires the resource ID to be a TextView
I don't wanted to change the border effect. This is very important to me , I will be very happy if someone help me, Thanks in advance.
Please pay attention to
ArrayAdapterdata type. It has onlyString, but you give him layout which contains text and image when it expects only string. DefaultArrayADapterdoes not know how to process it. This code from this SO answer should work:Please check out this answer for details on how to implement use case you asked.