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
First get current quantity from shown
textview
, like thisthen parse into Integer
now you have the current quantity as
int
just increment it, like thisthen
First create a counter above your main method:
Next set the click listeners to your two buttons within your onCreate():
and finally create the updateValue() method:
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
in your Activity.
Step 2
To assgin the button from the xml to this varibale call
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:
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