"implement spinner in android kotlin" Code Answer's
You're definitely familiar with the best coding language Kotlin that developers use to develop their projects and they get all their queries like "implement spinner in android kotlin" answered properly. Developers are finding an appropriate answer about implement spinner in android kotlin related to the Kotlin coding language. By visiting this online portal developers get answers concerning Kotlin codes question like implement spinner in android kotlin. Enter your desired code related query in the search bar and get every piece of information about Kotlin code related question on implement spinner in android kotlin.
implement spinner in android kotlin
package net.androidly.androidspinnerkotlin
import android.content.Context
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.view.Gravity
import android.view.View
import android.widget.*
import kotlinx.android.synthetic.main.activity_main.*
class MainActivity : AppCompatActivity(), AdapterView.OnItemSelectedListener {
var languages = arrayOf("Java", "PHP", "Kotlin", "Javascript", "Python", "Swift")
val NEW_SPINNER_ID = 1
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
var aa = ArrayAdapter(this, android.R.layout.simple_spinner_item, languages)
aa.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)
with(mySpinner)
{
adapter = aa
setSelection(0, false)
onItemSelectedListener = this@MainActivity
prompt = "Select your favourite language"
gravity = Gravity.CENTER
}
val spinner = Spinner(this)
spinner.id = NEW_SPINNER_ID
val ll = LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT)
ll.setMargins(10, 40, 10, 10)
linearLayout.addView(spinner)
aa = ArrayAdapter(this, R.layout.spinner_right_aligned, languages)
aa.setDropDownViewResource(R.layout.spinner_right_aligned)
with(spinner)
{
adapter = aa
setSelection(0, false)
onItemSelectedListener = this@MainActivity
layoutParams = ll
prompt = "Select your favourite language"
setPopupBackgroundResource(R.color.material_grey_600)
}
}
override fun onNothingSelected(parent: AdapterView<*>?) {
showToast(message = "Nothing selected")
}
override fun onItemSelected(parent: AdapterView<*>?, view: View?, position: Int, id: Long) {
when (view?.id) {
1 -> showToast(message = "Spinner 2 Position:${position} and language: ${languages[position]}")
else -> {
showToast(message = "Spinner 1 Position:${position} and language: ${languages[position]}")
}
}
}
private fun showToast(context: Context = applicationContext, message: String, duration: Int = Toast.LENGTH_LONG) {
Toast.makeText(context, message, duration).show()
}
}
Source: www.journaldev.com
All those coders who are working on the Kotlin based application and are stuck on implement spinner in android kotlin can get a collection of related answers to their query. Programmers need to enter their query on implement spinner in android kotlin related to Kotlin code and they'll get their ambiguities clear immediately. On our webpage, there are tutorials about implement spinner in android kotlin for the programmers working on Kotlin code while coding their module. Coders are also allowed to rectify already present answers of implement spinner in android kotlin while working on the Kotlin language code. Developers can add up suggestions if they deem fit any other answer relating to "implement spinner in android kotlin". Visit this developer's friendly online web community, CodeProZone, and get your queries like implement spinner in android kotlin resolved professionally and stay updated to the latest Kotlin updates.