Search code examples
androidkotlinincrementdecrement

Set limits for increments and decrements in Android Studio (Kotlin)


So I have 2 sets of increment and decrement buttons on a single page app. I want to set a limit of 0 for decrement and 10 for increment. I tried a number of different "if" statements, one that's combined with decrease_1.setOnClickListener and another combined with fun increaseInteger1() and nothing worked. What am I doing wrong?

class MainActivity : AppCompatActivity() {

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)

    increase_1.setOnClickListener { increaseInteger1() }
    decrease_1.setOnClickListener { decreaseInteger1()
        if(integer_number_1.text.length < 0) {
            integer_number_1.text.length === 0
        }
    }



    increase_2.setOnClickListener { increaseInteger2() }
    decrease_2.setOnClickListener { decreaseInteger2() }


}

fun increaseInteger1() {
    display_number_1(integer_number_1.text.toString().toInt() + 1)

    /*val stringNumber = integer_number_1.text.toString()
    val intNumber = stringNumber.toInt()
    val increasedNumber = intNumber + 1

    if(increasedNumber < 0) { display_number_1(0) }
    else if (increasedNumber > 10) {display_number_1(10)} }*/
}

fun decreaseInteger1() {
    display_number_1(integer_number_1.text.toString().toInt() - 1)



}


fun increaseInteger2() {
    display_number_2(integer_number_2.text.toString().toInt() + 1)
}

fun decreaseInteger2() {
    display_number_2(integer_number_2.text.toString().toInt() - 1)
}



private fun display_number_1(number: Int) {
    integer_number_1.setText("$number")
}

private fun display_number_2(number: Int) {
    integer_number_2.setText("$number")
}

} 

XML:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 
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"
tools:context=".MainActivity">

<LinearLayout
    android:id="@+id/linearLayout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="56dp"
    android:layout_marginBottom="549dp"
    android:orientation="horizontal"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent">

    <Button
        android:id="@+id/decrease_1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="-" />

    <TextView
        android:id="@+id/integer_number_1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="40dp"
        android:layout_marginTop="16dp"
        android:layout_marginRight="40dp"
        android:layout_marginBottom="16dp"
        android:inputType="number"
        android:text="0"
        android:textSize="70sp"
        android:textStyle="bold" />

    <Button
        android:id="@+id/increase_1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="+" />


</LinearLayout>

<Button
    android:id="@+id/decrease_2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="-"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toStartOf="@+id/integer_number_2"
    app:layout_constraintHorizontal_bias="0.513"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/linearLayout"
    app:layout_constraintVertical_bias="0.317" />

<Button
    android:id="@+id/increase_2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="+"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.579"
    app:layout_constraintStart_toEndOf="@+id/integer_number_2"
    app:layout_constraintTop_toBottomOf="@+id/linearLayout"
    app:layout_constraintVertical_bias="0.313" />

<TextView
    android:id="@+id/integer_number_2"
    android:layout_width="46dp"
    android:layout_height="99dp"
    android:layout_marginStart="46dp"
    android:layout_marginLeft="46dp"
    android:inputType="number"
    android:text="0"
    android:textSize="70sp"
    android:textStyle="bold"

    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.426"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/linearLayout"
    app:layout_constraintVertical_bias="0.297" />


    </androidx.constraintlayout.widget.ConstraintLayout>

Solution

  • === is the identity equality operator. You should use =, the assignment operator, to change values.

    It's very clumsy to be using the Strings from a TextView as your method of storing an integer value. It would be much cleaner to use a property for the value. (In a more complicated situation, you would put the value in a ViewModel's LiveData.)

    You can use coerce functions to limit your values.

    Wrapping that together:

    private var integer1 = 0
    private var integer2 = 0
    
    fun increaseInteger1() {
        integer1 = (integer1 + 1).coerceAtMost(10)
        display_number_1(integer1)
    }
    
    fun decreaseInteger1() {
        integer1 = (integer1 - 1).coerceAtLeast(0)
        display_number_1(integer1)
    }
    
    //...
    

    You can alternatively use min/max instead of coerce functions:

    integer1 = min(10, integer1 + 1) // increment
    integer1 = max(0, integer1 - 1) // decrement
    

    Or you could give the properties a custom setter that forces them to stay within the limits you want:

    private var integer1: Int = 0
        set(value) { field = value.coerceIn(0..10) }
    private var integer2: Int = 0
        set(value) { field = value.coerceIn(0..10) }
    
    //...
    fun increaseInteger1() {
        display_number_1(++integer1)
    }
    
    fun decreaseInteger1() {
        display_number_1(--integer1)
    }