I’m trying to make the progress bar interactive with the editText
. When the number written in editText
is greater than 10, the progress bar increases by 10%.
The code I wrote doesn’t work of course but I don’t see how to do it:
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.first_gamble)
val progressbar = findViewById<ProgressBar>(R.id.progressBar)
val converter = findViewById<EditText>(R.id.gamblesum)
val button = findViewById<Button>(R.id.button6)
button.setOnClickListener() {
if (converter > '2') {
with(progressbar) { incrementProgressBy(10) }
}
}
2
Answers
I went with this approach:
In the view model create a private and public val. Public val is to be read from/ observed and private val to set the value. Public val gets its value from the private val.
In view model
in the fragment / activity observe changes to progress. You can make the bar visible or not from here and set the value of the progress at the same time.
Back in your view model you will need some way to reset the progress/make it invisible. For example:
When you run some function or do something in the view model you can make the progress bar appear / disappear by changing the value of the private variable
You need to compare the value converted to an integer against an integer and not a string.
You can save the first conditional if the EditText only accepts numbers, otherwise you have to validate that I entered it