Solution 1 :
how if you move if statement
inside the listener
buttonred.setOnClickListener {
if(count >= 0){
count--
textCount.text = count.toString()
}
}
buttoning.setOnClickListener {
if(count <= 10){
count += 3
textCount.text = count.toString()
}
}
Solution 2 :
I believe your logic is wrong.
The most primary thing to understand is the IF condition
count <= 10 && count >= 0
In Basic English this means: count can only be less than and equal to 10 AND greater than and equal to 0. This would make sure that both buttonRed
and buttonIng
will not surpass values 0 to 10 when they are clicked.
buttonRed.setOnClickListener {
if (count >= 0 && count <= 10) {
count++
textCount.text = count.toString()
}
}
buttonIng.setOnClickListener {
if (count <= 10 && count >= 0) {
count--
textCount.text = count.toString()
}
}
Solution 3 :
Just remove “=” i.e
if (count > 0) {
println(count)
buttonred.setOnClickListener {
count–
textCount.text = count.toString()
}
if (count < 10)
{buttoning.setOnClickListener {
count += 3
textCount.text = count.toString()
}}
Problem :
I am running an app that has 2 buttons, one for increasing value and another for decreasing value. In this, I need to run the values between 10 and 0 but I couldn’t do it(whenever I run the app, it goes below 0 and over 10).
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
var count = 10
val textCount = findViewById<View>(R.id.textView) as TextView
val buttonred = findViewById<View>(R.id.injury) as Button
val buttoning = findViewById<View>(R.id.vial) as Button
if (count >= 0) {
println(count)
buttonred.setOnClickListener {
count--
textCount.text = count.toString()
}
if (count <= 10)
{buttoning.setOnClickListener {
count += 3
textCount.text = count.toString()
}}
}
}
XML
LinearLayout xmlns_android="http://schemas.android.com/apk/res/android"
xmlns_app="http://schemas.android.com/apk/res-auto"
xmlns_tools="http://schemas.android.com/tools"
android_layout_width="match_parent"
android_layout_height="match_parent"
android_orientation="vertical"
tools_context=".MainActivity">
<TextView
android_id="@+id/textView"
android_layout_width="60dp"
android_layout_height="78dp"
android_layout_marginStart="145dp"
android_layout_marginTop="68dp"
android_layout_marginEnd="145dp"
android_textSize="50dp"
app_layout_constraintEnd_toEndOf="parent"
app_layout_constraintStart_toStartOf="parent"
app_layout_constraintTop_toTopOf="parent" />
<Button
android_id="@+id/injury"
android_layout_width="106dp"
android_layout_height="wrap_content"
android_layout_marginHorizontal="150dp"
android_text="injury"
/>
<Button
android_id="@+id/vial"
android_layout_width="197dp"
android_layout_height="wrap_content"
android_layout_marginHorizontal="109dp"
android_text="vial" />
</LinearLayout>
I need help to clear this.
Comments
Comment posted by Alexander Dadukin
what are you trying to achieve? its not clear at all from the description ;D
Comment posted by A.R.B.N
Just for your information, In Kotlin, You don’t to bind views like java. Just give them an ID then call them in the code.
Comment posted by Hayi Nukman
you put the logic in wrong way, you need to check the count value inside setOnClickListener, for example: