skip to Main Content

Note: This has to be done in Kotlin

I want to update the quantity when the user presses the "+" or the "-" button
enter image description here

I also want the "0" (Quantity) to be aligned between the two buttons.

<RelativeLayout
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:layout_marginStart="36dp"
   android:layout_marginTop="36dp"
   android:orientation="vertical"
   tools:ignore="HardcodedText">

   <TextView
       android:id="@+id/Quantity"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:layout_marginBottom="16dp"
       android:text="QUANTITY"
       android:textSize="23sp" />

   <TextView
       android:id="@+id/qtr"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:layout_below="@+id/Quantity"
       android:layout_toRightOf="@+id/add"
       android:text="@string/Qtr"
       android:textSize="23sp" />

   <Button
       android:id="@+id/orderbutton"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:layout_below="@+id/add"
       android:onClick="order"
       android:text="Order" />

   <Button
       android:id="@+id/add"
       android:layout_width="45dp"
       android:layout_height="wrap_content"
       android:layout_below="@+id/Quantity"
       android:layout_marginRight="16dp"
       android:text="+" />

   <Button
       android:id="@+id/sub"
       android:layout_width="45dp"
       android:layout_height="wrap_content"
       android:layout_below="@+id/Quantity"
       android:layout_marginLeft="16dp"
       android:layout_toRightOf="@+id/qtr"
       android:text="-" />

</RelativeLayout>

And this is the Kotlin file code (MainActivity.kt)

package com.example.demoapplicaltion
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        // Write code here
    }
}

3

Answers


  1. First get current quantity from shown textview, like this

    String currentQuant = qtr.gettext().toString();
    

    then parse into Integer

    int quantity = Integer.parseInt(currentQuant);
    

    now you have the current quantity as int just increment it, like this

    quantity ++ ;
    

    then

    qtr.settext(String.valueOf(quantity));
    
    Login or Signup to reply.
  2. First create a counter above your main method:

    var quantity: Int = 0
    

    Next set the click listeners to your two buttons within your onCreate():

    findViewById<Button>(R.id.add).setOnClickListener{
      quantity++
      updateValue()
    }
    findViewById<Button>(R.id.min).setOnClickListener{
      quantity--
      updateValue()
    }
    

    and finally create the updateValue() method:

    fun updateValue(){
      findViewById<TextView>(R.id.Quantity).text = quantity
    }
    
    Login or Signup to reply.
  3. I’ll give you a step by step recepy instead of writing the whole code here.

    Step 1
    In your MainActivity you need some sort of reference to your two buttons and to the textview from the xml in order to do anything with them. The easiest way is to use the findViewById() fuction.
    For example you would need a variable like

    var addButton: Button? = null
    

    in your Activity.

    Step 2
    To assgin the button from the xml to this varibale call

    addButton = findViewById( R.id.add )
    

    Step 3
    Now that the button from the xml is assigned to your variable inside the activity you can access it to define what should happen if the button is clicked. Like for example:

    addButton.setOnClickListener {
    code that increases the quantity, sth like:
    val oldQuantity = quantityTextView.text.toInt()
    quantityTextView.text = oldQuantity + 1
    }
    

    To see findViewById work have a look at: https://www.tutorialkart.com/kotlin-android/access-a-view-programmatically-using-findviewbyid/
    Note that there are better, but a bit more complex ways to link between xml and Activity like ViewBinding: https://developer.android.com/topic/libraries/view-binding

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search