I have looked at EVERY thread on here but cant seem to figure out why my Spinner isn't populating. I have tried all of these setups and cant seem to get my Spinners to populate. The app runs fine but nothing appears in the dropdowns.
In .kt file
//Setting Spinner Info
val weight_units = resources.getStringArray(R.array.weight_units)
val nut_spinner = binding.nutSpinner
val units = arrayOf("One", "Two", "Three")
if (nut_spinner != null) {
/*
val t = inflater.inflate(com.example.cc_tabbed.R.layout.fragment_main, container, false)
val spinner = t.findViewById<Spinner>(R.id.nutSpinner) as Spinner
val adapter = ArrayAdapter.createFromResource(
this,
R.array.weight_units, android.R.layout.simple_spinner_item
)/*
//val nut_adapter = ArrayAdapter<String>(requireContext(), android.R.layout.simple_spinner_item, weight_units.toList())
//val nut_adapter = ArrayAdapter<String>(requireActivity(), android.R.layout.simple_spinner_item, weight_units)
//val nut_adapter = activity?.let { ArrayAdapter<String>(it, android.R.layout.simple_spinner_item, weight_units) }
//val nut_adapter = ArrayAdapter(this, android.R.layout.simple_spinner_item, weight_units)
//nut_spinner.adapter = nut_adapter
strings.xml
<resources>
<string name="app_name">CC-Tabbed</string>
<string name="tab_text_1">Converter</string>
<string name="tab_text_2">Local Database</string>
<string-array name="weight_units">
<item>lb</item>
<item>oz</item>
<item>gram</item>
</string-array>
</resources>
The relevant Spinner in the .xml file
<Spinner
android:id="#+id/desSpinner"
android:layout_width="97dp"
android:layout_height="36dp"
android:layout_marginTop="76dp"
android:layout_marginEnd="16dp"
android:layout_marginRight="16dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="#+id/textView2" />
Can anyone provide me any direction here? I am VERY new to programming in general and have sortof just stumbled my way to a working version of my Android App but Im learning as I go and have a hiccup here.
Thanks!
To populate data in Spinner component via xml you can use the attribute android:entries and define an array of strings into the strings.xml file like below:
<Spinner
android:id="#+id/spinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:entries="#array/spinnerData" />
where #array/spinnerData is define in your strings.xml like below:
<resources>
<string-array name="spinnerData">
<item>Spinner Item 1</item>
<item>Spinner Item 2</item>
<item>Spinner Item 3</item>
</string-array>
</resources>
or Programmatically in Fragment:
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
val spinner = view.findViewById<Spinner>(R.id.spinner)
val spinnerData = arrayOf("Spinner Item 1", "Spinner Item 2", "Spinner Item 3", "Spinner Item 4")
spinner.adapter = ArrayAdapter<String>(requireContext(), android.R.layout.simple_spinner_dropdown_item, spinnerData)
}
Result:
Related
I have 6 repetitive codes and I just want to use for-loop logic.
I tried to use "getIdentifier" and "findViewById" But I failed.
val sb_spinner_secret_p1: Spinner = findViewById(R.id.sb_secret1)
ArrayAdapter.createFromResource(this, R.array.secret, android.R.layout.simple_spinner_item
).also { adapter ->
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)
sb_spinner_secret_p1.adapter = adapter
}
I tried this but it never works
for(i in 1..7){
val sb_spinner = "sb_spinner_secret_p$i"
val sbsecret = "sb_secret$i"
val sb_spinner_resId = resources.getIdentifier(sb_spinner,"id", packageName)
val sbsecret_resId = resources.getIdentifier(sbsecret,"id", packageName)
}
I am trying to discriminate the selected item of a Spinner by its (multilanguage) text.
Here is my default strings.xml content:
<string-array name="spinner_items">
<item>length</item>
<item>weight</item>
<item>temperature</item>
</string-array>
And this is another strings.xml (Italian language) content:
<string-array name="spinner_items">
<item>lunghezza</item>
<item>peso</item>
<item>temperatura</item>
</string-array>
I set up my Spinner items in this way:
val items = resources.getStringArray(R.array.spinner_items)
spinner.adapter = ArrayAdapter(requireContext(), android.R.layout.simple_spinner_item, items)
And then I add the item selected listener:
spinner.onItemSelectedListener = object : AdapterView.OnItemSelectedListener {
override fun onItemSelected(parent: AdapterView<*>, view: View, position: Int, id: Long) {
when(spinner.getItemAtPosition(position).toString()) {
"length" -> actionLength()
"lunghezza" -> actionLength()
"weight" -> actionWeight()
"peso" -> actionWeight()
"temperature" -> actionTemperature()
"temperatura" -> actionTemperature()
}
}
override fun onNothingSelected(parent: AdapterView<*>) {}
}
Everything works fine but the problem is that everytime I add a new language locale, I have to remember to add the specific string translation inside the when block.
Is there a more "dynamic" way to do this?
I had the same problem in the past and here is how I solved it.
Edit your strings.xml files by adding a string resource name for each items in your array, for example:
Default strings.xml
<string name="length">length</string>
<string name="weight">weight</string>
<string name="temperature">temperature</string>
<string-array name="spinner_items">
<item>#string/length</item>
<item>#string/weight</item>
<item>#string/temperature</item>
</string-array>
Italian strings.xml
<string name="length">lunghezza</string>
<string name="weight">peso</string>
<string name="temperature">temperatura</string>
<string-array name="spinner_items">
<item>#string/length</item>
<item>#string/weight</item>
<item>#string/temperature</item>
</string-array>
So in your code, you'll have:
when(spinner.getItemAtPosition(position).toString()) {
getString(R.string.length) -> actionLength()
getString(R.string.weight) -> actionWeight()
getString(R.string.temperature) -> actionTemperature()
}
I hope I was helpful!
Just use the position:
spinner.onItemSelectedListener = object : AdapterView.OnItemSelectedListener {
override fun onItemSelected(parent: AdapterView<*>, view: View, position: Int, id: Long) {
when(position) {
0 -> actionLength()
1 -> actionWeight()
2 -> actionTemperature()
}
}
override fun onNothingSelected(parent: AdapterView<*>) {}
}
In your array use:
<string-array name="spinner_items">
<item>#string/length</item>
<item>#string/weight</item>
<item>#string/temperature</item>
</string-array>
How do I change the value of a variable using the spinner widget.
strings.xml
<string name="selected_item">Selected item:</string>
<string-array name="Languages">
<item>English</item>
<item>Latin</item>
<item>French</item>
</string-array>
mainactivity
val spinner = findViewById<Spinner>(R.id.spinner)
if (spinner != null) {
val adapter = ArrayAdapter(
this,
android.R.layout.simple_spinner_item, languages
)
spinner.adapter = adapter
spinner.onItemSelectedListener = object :
AdapterView.OnItemSelectedListener {
override fun onItemSelected(
parent: AdapterView<*>,
view: View, position: Int, id: Long
) {
//what to do here?
}
}
}
suppose I want to change the value of this variable
strokeManager.lang = "en"
So you will want to use the position to access it from the list that you used to populate the spinner.
strokeManager.lang = languageMapToLangCode[languages.get(position)]
This should be defined in your class somewhere the spinner can access it
val languageMapToLangCode: HashMap<String, String> = hashMapOf("French" to "fr", "Latin" to "ln", "English to "en")
Android Spinner is not working, the API is working and the Spinner's list of items is working. However, the item selection is not working.
class PlayerSignup2Activity : AppCompatActivity() {
private lateinit var positions : List<Position>
val positionSpinner = positionsSpinner
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(com.example.app.R.layout.activity_user_signup2)
//Positions from API
positions = APIService.getPositions(this)
val spinnerAdapter = ArrayAdapter(this, R.layout.spinner_item, positions)
spinnerAdapter.setDropDownViewResource(R.layout.spinner_item)
positionSpinner.adapter = spinnerAdapter
}
Spinner list
After selecting any item
Any idea on how to fix this?!
Try with this
<Spinner
android:id="#+id/spinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
In Kotlin
class MainActivity : /** Other Classes, */AdapterView.OnItemSelectedListener {
var list_of_items = arrayOf("Item 1", "Item 2", "Item 3")
override fun onCreate(savedInstanceState: Bundle?) {
spinner!!.setOnItemSelectedListener(this)
// Create an ArrayAdapter using a simple spinner layout and languages array
val aa = ArrayAdapter(this, android.R.layout.simple_spinner_item, list_of_items)
// Set layout to use when the list of choices appear
aa.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)
// Set Adapter to Spinner
spinner!!.setAdapter(aa)
}
override fun onItemSelected(arg0: AdapterView<*>, arg1: View, position: Int, id:Long{
// use position to know the selected item
//here you will get the answwer
}
override fun onNothingSelected(arg0: AdapterView<*>) {
}
}
try using prompt for adding title in .XML file
<Spinner
android:id="#+id/spinner"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:prompt="#string/spinner_title"/>
or try adding 1 more item that contains whatever title you want like "select position" on index 0.
positions.add("select position");
positions.add("value 1");
after adding values to list set spinner value to index 0 by default using below code.
mSpinner.setSelection(0) .
and on Item Selected check if index 0 or value is "select position" then ignore selection else perform action you want.
private lateinit var positions : List<Position>
//add dummy data first
positions.add(Position("select position"));
//Positions from API
positions.addAll(APIService.getPositions(this));
I'm trying to add a spinner inside an alert using anko. My code so far looks like this:
alert(getString(R.string.alert)) {
positiveButton("Cool") { toast("Yess!!!") }
customView {
linearLayout {
textView("I'm a text")
padding = dip(16)
orientation = LinearLayout.VERTICAL
spinner(R.style.Widget_AppCompat_Spinner) {
id = R.id.spinner_todo_category
prompt = "Select a Category"
}
}
}
}.show()
but I get compilation errors because apparently that's not how to call a spinner. I've been looking at the docs (Anko GitHub Wiki) but it says nothing about spinners.
Thanks in advance
One solution :
class AddActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val items = listOf(Friend("bla","bla",50),Friend("bla","bla",50));
val adapterFriends = ArrayAdapter(this,R.layout.mon_spinner,items)
verticalLayout {
val friends = spinner { adapter = adapterFriends }
val wine = editText()
button("Say Hello") {
onClick { toast("Hello, ${wine.text}!") }
}
}
}
}
with this layout (mon_spinner.xml) :
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:textSize="14sp"
android:textColor="#color/colorPrimary"
android:spinnerMode="dialog"
android:text="XXX"
/>
It's all right !!
Try this in your AnkoComponent:
spinner {
adapter = ArrayAdapter.createFromResource(
ctx,
R.array.your_string_array,
android.R.layout.simple_spinner_dropdown_item)
}