I'm trying to make NumberPicker show a custom set of selections and to start with the second item in the list (value=1). The selections are: C♭, C, C#, D♭, and D, and the picker should show "C" for the second item, but instead it's showing "C♭" with two "C♭" items in the list:
Using touch input to scroll the NumberPicker "fixes" this, but that's not really a fix because users won't want to have to interact with the view to make it show the correct value. It should display correctly from the start. And it only seems to happen when the "C♭" comes first, "C" comes second, and I set the value to 1 (so, the "C"). The bug doesn't happen if I set the value to 0 or 2, or if the "C#" comes first, or if the C comes first.
Here's my layout XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<NumberPicker
android:id="@+id/npExample"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</LinearLayout>
Here's my Kotlin:
package com.example.myapplication
import android.os.Bundle
import android.widget.NumberPicker
import androidx.activity.ComponentActivity
class MainActivity : ComponentActivity() {
var npExample : NumberPicker? = null
val exampleValues = arrayOf(
"C♭",
"C",
"C#",
"D♭",
"D"
)
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
npExample = findViewById(R.id.npExample)
npExample!!.minValue = 0
npExample!!.maxValue = exampleValues.size - 1
npExample!!.displayedValues = exampleValues
}
override fun onResume() {
super.onResume()
// Read the value we need from preferences here.
// For this example, we just hard-code the value into Kotlin to demo the behavior.
val exampleValue = 1
npExample?.value = exampleValue
}
}
Any idea? Thank you.
