kotlin custom spinner example in android
In this Article today learn kotlin custom spinner example. follow the full source code kotlin custom spinner example
1.MainActivity.kt:
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import com.google.gson.Gson
import kotlinx.android.synthetic.main.activity_main.*
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val modelList: List<Model> = readFromAsset()
val customDropDownAdapter = CustomDropDownAdapter(this, modelList)
spinner04.adapter = customDropDownAdapter
}
private fun readFromAsset(): List<Model> {
val file_name = "android_version.json"
val bufferReader = application.assets.open(file_name).bufferedReader()
val json_string = bufferReader.use {
it.readText()
}
val gson = Gson()
val modelList: List<Model> = gson.fromJson(json_string, Array<Model>::class.java).toList()
return modelList
}
}
2.activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:orientation="vertical">
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Custom Spinner With Text and Image"
android:layout_marginTop="20dp"
android:textSize="20sp"
android:textStyle="bold"/>
<androidx.appcompat.widget.AppCompatSpinner
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:id="@+id/spinner04"/>
</LinearLayout>
3.Model.kt:
data class Model
(val name: String, val url: String)
{
}
4.CustomDropDownAdapter.kt:
import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.BaseAdapter
import android.widget.ImageView
import android.widget.TextView
class CustomDropDownAdapter(val context: Context, var dataSource: List<Model>) : BaseAdapter() {
private val inflater: LayoutInflater = context.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
override fun getView(position: Int, convertView: View?, parent: ViewGroup?): View {
val view: View
val vh: ItemHolder
if (convertView == null) {
view = inflater.inflate(R.layout.custom_spinner_item, parent, false)
vh = ItemHolder(view)
view?.tag = vh
} else {
view = convertView
vh = view.tag as ItemHolder
}
vh.label.text = dataSource.get(position).name
val id = context.resources.getIdentifier(dataSource.get(position).url, "drawable", context.packageName)
vh.img.setBackgroundResource(id)
return view
}
override fun getItem(position: Int): Any? {
return dataSource[position];
}
override fun getCount(): Int {
return dataSource.size;
}
override fun getItemId(position: Int): Long {
return position.toLong();
}
private class ItemHolder(row: View?) {
val label: TextView
val img: ImageView
init {
label = row?.findViewById(R.id.text) as TextView
img = row?.findViewById(R.id.img) as ImageView
}
}
}
5.custom_spinner_item.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="8dp">
<ImageView android:layout_width="40dp"
android:layout_height="40dp"
android:id="@+id/img"
android:contentDescription="@string/app_name"/>
<TextView android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="20sp"
android:paddingStart="10dp"
android:layout_gravity="center_vertical"
android:id="@+id/text"/>
</LinearLayout>
6.AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest
package="com.spinner.example"
xmlns:android="http://schemas.android.com/apk/res/android">
<application
android:name=".ui.App"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:networkSecurityConfig="@xml/network_security_config"
android:label="@string/app_name"
android:theme="@style/AppTheme">
<activity
android:name=".ui.activities.MainActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
</application>
</manifest>
A Quadratic Formula Calculator is a tool used to find the roots of a quadratic equation in the form ax^2 + bx + c = 0. Users input the coefficients (a, b, and c), and the calculator computes the solutions using the quadratic formula (-b ± √(b^2 - 4ac)) / (2a). It helps solve quadratic equations efficiently in mathematics and engineering.…read more
ReplyDelete