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
}
}
CodePudding user response:
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
}
CodePudding user response:
well, first get current quantity from shown textview, String currentQuant = qtr.gettext().toString(); then parse into Integer int quantity = Integer.parseInt(currentQuant); now u have the current quantity as int just increment it, quantity ; then qtr.settext(String.valueOf(quantity));
CodePudding user response:
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