create a custom spinner adapter in android using kotlin
11,217
Your problem lies in customeSpinnerAdapter.kt
customeSpinnerAdapter.kt
import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.BaseAdapter
class customeSpinnerAdapter : BaseAdapter{
override fun getItem(p0: Int): Any {
return flag.length
}
override fun getItemId(p0: Int): Long {
return null
}
override fun getCount(): Int {
return 0
}
val country:String
val flag:String
//Add this
val inflater : LayoutInflater
fun customeSpinnerAdapter(context:Context,name:String,image:String){
this.country = name
this.flag = image
//val inflater = LayoutInflater.from(context)
inflater = LayoutInflater.from(context)
}
override fun getView(p0: Int, p1: View?, p2: ViewGroup?): View {
val view = inflater.inflate(R.layout.custome_spinner)
spinner_image.setImageResource(flag)
spinner_country.setText(country)
return view
}
}
Related videos on Youtube
Author by
Wael Fadl Ãllåh
Updated on June 04, 2022Comments
-
Wael Fadl Ãllåh almost 2 years
I'm trying make a spinner with a custom adapter to display images with text like the following
but I got lots of unresolved references so I think I'm doing something terribly wrong
I have two fragments the spinner is inside the second fragment custome_spinner.xml is the custom layout file
android studio 3.1.2
kotlin_version = '1.2.30'
gradle:3.1.2
customeSpinnerAdapter.kt import android.content.Context import android.view.LayoutInflater import android.view.View import android.view.ViewGroup import android.widget.BaseAdapter class customeSpinnerAdapter : BaseAdapter{ override fun getItem(p0: Int): Any { return flag.length } override fun getItemId(p0: Int): Long { return null } override fun getCount(): Int { return 0 } val country:String val flag:String fun customeSpinnerAdapter(context:Context,name:String,image:String){ this.country = name this.flag = image val inflater = LayoutInflater.from(context) } override fun getView(p0: Int, p1: View?, p2: ViewGroup?): View { val view = inflater.inflate(R.layout.custome_spinner) spinner_image.setImageResource(flag) spinner_country.setText(country) return view } }
custome_spinner.xml <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <ImageView android:id="@+id/spinner_image" android:layout_width="50dp" android:layout_height="50dp" android:padding="5dp" android:src="@drawable/us" /> <TextView android:id="@+id/spinner_country" android:layout_width="fill_parent" android:layout_height="wrap_content" android:paddingLeft="10dp" android:paddingRight="10dp" android:layout_gravity="center" android:text="Custom Text" android:textColor="#000" /> </LinearLayout>
the widget inside fragment_second.xml <Spinner android:id="@+id/spinner" android:layout_width="368dp" android:layout_height="wrap_content" android:layout_marginEnd="8dp" android:layout_marginLeft="8dp" android:layout_marginRight="8dp" android:layout_marginStart="8dp" android:layout_marginTop="12dp" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintHorizontal_bias="0.0" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/from_textView" />