Reset integers in a TextView to 0 (Kotlin) - android

So I got the increment and decrement buttons work, but I need to reset the second set to 0 using the clear button. If you push it, it'll show you a 0 as a string value, but it won't reset the output. For example if I we're to increment the integer values up to 6 and then push the clear button, it'll show 0 in the TextView but once I push the increment button again it'll go straight up to 7 and not 1.
What am I missing?
KT
package com.example.plus_and_minus_input
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import kotlinx.android.synthetic.main.activity_main.*
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
increase_1.setOnClickListener { increaseInteger1() }
decrease_1.setOnClickListener { decreaseInteger1()
}
increase_2.setOnClickListener { increaseInteger2() }
decrease_2.setOnClickListener { decreaseInteger2() }
clear_button.setOnClickListener {
integer_number_2.text ="0"
}
}
private var integer1 = 0
private var integer2 = 0
fun increaseInteger1() {
//display_number_1(integer_number_1.text.toString().toInt() + 1)
integer1 = (integer1 + 1).coerceAtMost(10)
display_number_1(integer1)
}
fun decreaseInteger1() {
//display_number_1(integer_number_1.text.toString().toInt() - 1)
integer1 = (integer1 - 1).coerceAtLeast(0)
display_number_1(integer1)
}
fun increaseInteger2() {
//display_number_2(integer_number_2.text.toString().toInt() + 1)
integer2 = (integer2 + 1).coerceAtMost(10)
display_number_2(integer2)
}
fun decreaseInteger2() {
//display_number_2(integer_number_2.text.toString().toInt() - 1)
integer2 = (integer2 - 1).coerceAtLeast(0)
display_number_2(integer2)
}
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="309dp"
android:layout_height="118dp"
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" />
<Button
android:id="#+id/clear_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Clear"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/integer_number_2" />
</androidx.constraintlayout.widget.ConstraintLayout>

You need to reset the value of the global int var also:
clear_button.setOnClickListener {
integer_number_2.text ="0"
integer2 = 0
}
It's bad practise to name your variables with underscores in kotlin/java. Use camel case instead tvIntegerNumberTwo and probably assign it the initials of the xml element type to better recognise what it is.

Related

Passing data from an activity (On Button clicked), to be added to a recyclerView adapter at another activity, without starting the activity

I'm trying to make a food application with android-Kotlin, and it contains:
The problem is i tried many times to send data from "PopularDetailedActivity" to another activity onClick (Add To Cart Button) which contains my Cart or basket that i choose, but i can't access a solution.
PopularAdapter.kt that pass the data to PopularDetailedActivity.kt
class PopularAdapter(private var popItems: List<Popular>): RecyclerView.Adapter<PopularAdapter.MyViewHolder>() {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder {
val popView = LayoutInflater.from(parent.context).inflate(R.layout.pop_items, parent, false)
return MyViewHolder(popView)
}
override fun onBindViewHolder(holder: MyViewHolder, position: Int) {
val newPop: Popular = popItems[position]
holder.foodName.text = newPop.foodName
holder.foodImg.setImageResource(newPop.foodImage)
holder.foodDollar.text = newPop.foodDollar
holder.foodPrice.text = newPop.foodPrice.toString()
holder.foodDetails.text = newPop.foodDetails
holder.myPop = newPop
}
override fun getItemCount(): Int {
return popItems.size
}
fun submitList(categories: List<Popular>){
popItems = categories
}
class MyViewHolder constructor(itemView: View, var myPop: Popular?= null):
RecyclerView.ViewHolder(itemView){
init {
itemView.addBtn.setOnClickListener {
val myIntent = Intent(itemView.context, PopularDetailed::class.java)
myIntent.putExtra("title", myPop!!.foodName)
myIntent.putExtra("price", myPop!!.foodPrice)
myIntent.putExtra("img", myPop!!.foodImage)
myIntent.putExtra("description", myPop!!.foodDetails)
itemView.context.startActivity(myIntent)
}
}
val foodName: TextView = itemView.popMainText
val foodImg: ShapeableImageView = itemView.popMainImg
val foodDollar: TextView = itemView.dollarTxt
val foodPrice: TextView = itemView.priceValue
val foodDetails: TextView = itemView.popDetail
}
}
PopularDetailedActivity.kt
class PopularDetailed : AppCompatActivity() {
private lateinit var cTitle: TextView
private lateinit var cImage: ShapeableImageView
private lateinit var cPrice: TextView
private lateinit var cDollar: TextView
private lateinit var cMinus: ImageView
private lateinit var cPlus: ImageView
private lateinit var cItemVal: TextView
private lateinit var cDescription: TextView
private lateinit var cAddBtn: Button
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_popular_details)
val bundle = intent.extras
val localTitle = bundle!!.getString("title")
val localPrice = bundle.getDouble("price")
val localImage = bundle.getInt("img")
val localDescription = bundle.getString("description")
initValues()
cTitle.text = localTitle
cPrice.text = localPrice.toString()
cImage.setImageResource(localImage)
cDescription.text = localDescription
incDecNum()
cAddBtn.setOnClickListener {
if (incDecValue.text.toString().toInt() > 0){
Toast.makeText(applicationContext, "Added To Your Cart", Toast.LENGTH_SHORT).show()
}
}
}
//Initiate Values
private fun initValues(){
cTitle = findViewById(R.id.pop_local_title)
cImage = findViewById(R.id.pop_local_img)
cPrice = findViewById(R.id.localPriceValue)
cDollar = findViewById(R.id.localDollarTxt)
cPlus = findViewById(R.id.incNum)
cItemVal = findViewById(R.id.incDecValue)
cMinus = findViewById(R.id.decNum)
cDescription = findViewById(R.id.pop_local_des)
cAddBtn = findViewById(R.id.local_add_btn)
}
//Increase or decrease items' number
private fun incDecNum(){
val priceValueInString = localPriceValue.toString()
var priceInDouble = priceValueInString.split("")[0].toDoubleOrNull()
val incDecValueInString = incDecValue.toString()
var numberOfItems = incDecValueInString.split(" ")[0].toIntOrNull()
numberOfItems = 0
decNum.setOnClickListener {
if (numberOfItems > 0){
numberOfItems -= 1
val newVal = numberOfItems.toString()
incDecValue.text = newVal
//Changing the price
/*if (priceInDouble != null) {
if (newVal > 0.toString()){
priceInDouble -= priceInDouble
localPriceValue.text = priceInDouble.toString()
}
}*/
} else{
//Toast.makeText(applicationContext, "", Toast.LENGTH_SHORT).show()
}
}
incNum.setOnClickListener {
numberOfItems += 1
val newVal = numberOfItems.toString()
incDecValue.text = newVal
//Changing the price
/*if (priceInDouble != null) {
if (newVal > 1.toString()){
priceInDouble += priceInDouble
localPriceValue.text = priceInDouble.toString()
}
}*/
}
}
}
MyCartBasketActivity.kt
class MyCartBasket : AppCompatActivity() {
private val myCartItems = ArrayList<MyCart>()
private val myCartAdapter = MyCartAdapter(myCartItems)
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_my_cart_basket)
myCartDynamic.layoutManager = LinearLayoutManager(applicationContext, RecyclerView.VERTICAL, false)
myCartDynamic.adapter = myCartAdapter
}
//Adding Data
/*private fun addData(){
val cartItems = MyCartDynamicDataSource.createDynamicCartItems(myCartItems)
myCartAdapter.submitList(cartItems)
}*/
}
MyCartAdapter.kt for the result activity at MyCartBasketActivity.kt
class MyCartAdapter(private var cartItems: List<MyCart>):
RecyclerView.Adapter<MyCartAdapter.MyViewHolder>() {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder {
val cartView = LayoutInflater.from(parent.context).inflate(R.layout.my_cart_items, parent, false)
return MyViewHolder(cartView)
}
override fun onBindViewHolder(holder: MyViewHolder, position: Int) {
val newCart = cartItems[position]
holder.cartedImg.setImageResource(newCart.cartedImg)
holder.cartedTitle.text = newCart.cartedTitle
holder.cartedItemNum.text = newCart.cartedItemNum
holder.cartedLPriceValue.text = newCart.cartedLPriceValue
holder.cartedIPriceValue.text = newCart.cartedIPriceValue
holder.cartedPlusItem.text = newCart.cartedPlusItem
holder.cartedMinusItem.text = newCart.cartedMinusItem
}
fun submitList(cartNewItems: List<MyCart>){
cartItems = cartNewItems
}
override fun getItemCount(): Int {
return cartItems.size
}
class MyViewHolder constructor(itemView: View): RecyclerView.ViewHolder(itemView){
val cartedImg: ShapeableImageView = itemView.myCartedImg
val cartedTitle: TextView = itemView.myCartedTitle
val cartedItemNum: TextView = itemView.myCartItemNum
val cartedLPriceValue: TextView = itemView.localPriceCart
val cartedIPriceValue: TextView = itemView.localPriceCartA
val cartedPlusItem: Button = itemView.incNumCart
val cartedMinusItem: Button = itemView.decNumCart
}
}
Popular.kt
class Popular {
var foodName:String
var foodImage:Int
var foodDollar:String
var foodPrice:Double
var foodDetails:String
constructor(foodName:String, foodImg:Int, foodDollar:String, foodPrice:Double, foodDetails:String){
this.foodName = foodName
this.foodImage = foodImg
this.foodDollar = foodDollar
this.foodPrice = foodPrice
this.foodDetails = foodDetails
}
activity_popular_details.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"
android:layout_marginStart="25dp"
android:layout_marginEnd="25dp"
tools:context=".PopularDetailed">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/pop_local_title"
android:theme="#style/MyPopLocalText"
android:text="#string/hello_blank_fragment"
android:layout_marginTop="35dp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/local_price"
android:orientation="horizontal"
android:layout_marginTop="15dp"
app:layout_constraintTop_toBottomOf="#id/pop_local_title"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent">
<TextView
android:id="#+id/localDollarTxt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/dollarSign"
android:textColor="#android:color/holo_orange_light"
android:textSize="16sp"
tools:ignore="TextContrastCheck" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/localPriceValue"
android:text="#string/price_val"
android:textStyle="bold"
android:textColor="#color/black"
android:textSize="16sp"/>
</LinearLayout>
<com.google.android.material.imageview.ShapeableImageView
android:layout_width="300dp"
android:layout_height="350dp"
android:id="#+id/pop_local_img"
android:src="#drawable/hot_breakfast"
android:layout_marginTop="5dp"
app:layout_constraintTop_toBottomOf="#id/local_price"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"/>
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/incDecItemNum"
android:orientation="horizontal"
app:layout_constraintTop_toBottomOf="#id/pop_local_img"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent">
<ImageView
android:layout_width="35dp"
android:layout_height="35dp"
android:id="#+id/decNum"
android:contentDescription="#string/dollarSign"
android:background="#drawable/inc_dec_back"
android:src="#drawable/minus"
tools:ignore="TouchTargetSizeCheck"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/incDecValue"
android:text="#string/_0"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp"
android:textSize="15sp"
android:textStyle="bold"
android:textColor="#color/black"
app:layout_constraintStart_toEndOf="#id/decNum"
app:layout_constraintTop_toTopOf="#id/decNum"
app:layout_constraintBottom_toBottomOf="#id/decNum"/>
<ImageView
android:layout_width="35dp"
android:layout_height="35dp"
android:id="#+id/incNum"
android:layout_marginStart="8dp"
android:contentDescription="#string/dollarSign"
android:background="#drawable/inc_dec_back"
android:src="#drawable/plus"
tools:ignore="TouchTargetSizeCheck"
app:layout_constraintTop_toTopOf="#id/decNum"
app:layout_constraintStart_toEndOf="#id/incDecValue"/>
</androidx.constraintlayout.widget.ConstraintLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/pop_local_des"
android:text="#string/meal_description"
android:layout_marginTop="10dp"
android:theme="#style/MyPopLocalDes"
app:layout_constraintTop_toBottomOf="#id/incDecItemNum"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/local_add_btn"
android:backgroundTint="#FF5E00"
android:text="#string/add_to_cart"
android:textStyle="bold"
android:textSize="17sp"
app:shapeAppearanceOverlay="#style/Button5"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"/>
</androidx.constraintlayout.widget.ConstraintLayout>
activity_my_cart_basket.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=".MyCartBasket">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/maiCartV"
android:layout_marginBottom="50dp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintBottom_toBottomOf="parent">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/myCartView"
android:orientation="vertical"
android:layout_marginTop="15dp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/myCartMainText"
android:text="#string/your_cart"
android:theme="#style/MyHeadText"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"/>
<androidx.recyclerview.widget.RecyclerView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:id="#+id/myCartDynamic"
android:layout_marginTop="15dp"/>
</LinearLayout>
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/itemsTotalPrice"
android:orientation="horizontal"
android:layout_marginTop="25dp"
app:layout_constraintTop_toBottomOf="#id/myCartView"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/totalPriceText"
android:layout_marginStart="15dp"
android:text="#string/items_total_price"
android:textStyle="bold"
android:textColor="#color/black"
android:textSize="16sp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/mainTotalValue"
android:text="#string/_0"
android:layout_marginEnd="15dp"
android:textColor="#color/black"
android:textSize="13sp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"/>
<TextView
android:id="#+id/mainTotalDollar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="5dp"
android:text="#string/dollarSign"
android:textColor="#FF5E00"
android:textSize="13sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="#id/mainTotalValue"
app:layout_constraintTop_toTopOf="parent"
tools:ignore="TextContrastCheck" />
</androidx.constraintlayout.widget.ConstraintLayout>
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/itemsDeliveryPrice"
android:orientation="horizontal"
android:layout_marginTop="10dp"
app:layout_constraintTop_toBottomOf="#id/itemsTotalPrice"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/totalDeliveryText"
android:layout_marginStart="15dp"
android:text="#string/delivery_services"
android:textStyle="bold"
android:textColor="#color/black"
android:textSize="16sp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/mainDeliveryValue"
android:text="#string/_0"
android:layout_marginEnd="15dp"
android:textColor="#color/black"
android:textSize="13sp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"/>
<TextView
android:id="#+id/mainDeliveryDollar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="5dp"
android:text="#string/dollarSign"
android:textColor="#FF5E00"
android:textSize="13sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="#id/mainDeliveryValue"
app:layout_constraintTop_toTopOf="parent"
tools:ignore="TextContrastCheck" />
</androidx.constraintlayout.widget.ConstraintLayout>
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/itemsTaxPrice"
android:orientation="horizontal"
android:layout_marginTop="10dp"
app:layout_constraintTop_toBottomOf="#id/itemsDeliveryPrice"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/totalTaxText"
android:layout_marginStart="15dp"
android:text="#string/taxes"
android:textStyle="bold"
android:textColor="#color/black"
android:textSize="16sp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/mainTaxValue"
android:text="#string/_0"
android:layout_marginEnd="15dp"
android:textColor="#color/black"
android:textSize="13sp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"/>
<TextView
android:id="#+id/mainTaxDollar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="5dp"
android:text="#string/dollarSign"
android:textColor="#FF5E00"
android:textSize="13sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="#id/mainTaxValue"
app:layout_constraintTop_toTopOf="parent"
tools:ignore="TextContrastCheck" />
</androidx.constraintlayout.widget.ConstraintLayout>
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/itemsTotal"
android:orientation="horizontal"
android:layout_marginTop="25dp"
app:layout_constraintTop_toBottomOf="#id/itemsTaxPrice"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/totalTotalText"
android:layout_marginStart="15dp"
android:text="#string/total"
android:textStyle="bold"
android:textColor="#color/black"
android:textSize="25sp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/mainTotalV"
android:text="#string/_0"
android:layout_marginEnd="15dp"
android:textColor="#color/black"
android:textSize="18sp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"/>
<TextView
android:id="#+id/mainTotalD"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="5dp"
android:text="#string/dollarSign"
android:textColor="#FF5E00"
android:textSize="18sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="#id/mainTotalV"
app:layout_constraintTop_toTopOf="parent"
tools:ignore="TextContrastCheck" />
</androidx.constraintlayout.widget.ConstraintLayout>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/cart_check_btn"
android:backgroundTint="#FF5E00"
android:text="#string/checkout"
android:textStyle="bold"
android:textSize="25sp"
android:layout_marginStart="50dp"
android:layout_marginEnd="50dp"
android:layout_marginBottom="35dp"
app:shapeAppearanceOverlay="#style/Button5"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"/>
</androidx.constraintlayout.widget.ConstraintLayout>
<include layout="#layout/bottom_bar"/>
</androidx.constraintlayout.widget.ConstraintLayout>
I want to add the data which viewed at "PopularDetailedActivity" to "MyCartActivity" without starting it, and then to the adapter of its dynamic recyclerView, and update it. But, i can't access the proper solution.
You need to persist basket data somehow, then pass data itself (or reference to this data) to new screen (MyCartActivity).
The tricky question is to how to persist the data.
IMO, good solution can be to use fragments instead of activities, then you can use sharedViewModel, where you store data with basket items. This shared view model will be accessible by both fragments.
If for some reason you can't use fragments (although I strongly advise to do so), then another solution may be to have some singleton object, where you store basket items. Note though, that usage of singleton objects is risky - for example, when you start updating data in singletons from different places, you may loose control on it at some point in time.
object TempBasketDataHolder {
val basketItems: MutableList<Popular> = mutableListOf()
}
Then in your PopularDetailedActivity you can define methods to handle basket content:
fun clearBasket() {
TempBasketDataHolder.basketItems.clear()
}
fun addBasketItem(item: Popular) {
TempBasketDataHolder.basketItems.add(item)
}
fun removeBasketItem(item: Popular) {
TempBasketDataHolder.basketItems.remove(item)
}
In your MyCartActivity you can then access TempBasketDataHolder.basketItems correspondingly.

Unresolved reference to constraintLayout variable in Kotlin code in android app

I am following this tutorial to create an android app with space animation stuff. In the step titled "Transition Manager" you're instructed to add the following code to your MainActivity.kt file:
`
constraintSet1.clone(constraintLayout) //1
constraintSet2.clone(this, R.layout.activity_main) //2
departButton.setOnClickListener { //3
//apply the transition
TransitionManager.beginDelayedTransition(constraintLayout) //4
val constraint = if (!isOffscreen) constraintSet1 else constraintSet2
isOffscreen = !isOffscreen
constraint.applyTo(constraintLayout) //5
}
`
When this code is added the variable "constraintLayout" throws up an unresolved reference error
I think constraintLayout is supposed to be pointing to something in the xml and something is failing to make that happen, I just don't know what.
The kotlin code after adding the above block is:
`
package com.raywenderlich.android.razegalactic
import android.os.Bundle
import android.support.constraint.*
import android.support.v7.app.AppCompatActivity
import android.transition.TransitionManager
import kotlinx.android.synthetic.main.keyframe1.*
/**
* Main Screen
*/
class MainActivity : AppCompatActivity() {
private val constraintSet1 = ConstraintSet()
private val constraintSet2 = ConstraintSet()
private var isOffscreen = true
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.keyframe1)
switch1.setOnCheckedChangeListener { _, isChecked ->
switch1.setText(if (isChecked) R.string.round_trip else R.string.one_way)
constraintSet1.clone(constraintLayout) //1
constraintSet2.clone(this, R.layout.activity_main) //2
departButton.setOnClickListener { //3
//apply the transition
TransitionManager.beginDelayedTransition(constraintLayout) //4
val constraint = if (!isOffscreen) constraintSet1 else constraintSet2
isOffscreen = !isOffscreen
constraint.applyTo(constraintLayout) //5
}
}
}
}
`
The main xml is as follows:
`
<android.support.constraint.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:id="#+id/ConstraintLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal"
android:orientation="vertical"
tools:context=".MainActivity">
<ImageView
android:id="#+id/flightsIcon"
android:layout_width="30dp"
android:layout_height="30dp"
android:src="#drawable/rocket_icon"
app:layout_constraintBottom_toBottomOf="#+id/spaceStationIcon"
app:layout_constraintEnd_toStartOf="#+id/roverIcon"
app:layout_constraintStart_toEndOf="#+id/spaceStationIcon"
app:layout_constraintTop_toTopOf="#+id/spaceStationIcon" />
<ImageView
android:id="#+id/spaceStationIcon"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_marginTop="15dp"
android:src="#drawable/space_station_icon"
app:layout_constraintEnd_toStartOf="#+id/flightsIcon"
app:layout_constraintHorizontal_chainStyle="spread"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<ImageView
android:id="#+id/roverIcon"
android:layout_width="30dp"
android:layout_height="30dp"
android:src="#drawable/rover_icon"
app:layout_constraintBottom_toBottomOf="#+id/flightsIcon"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="#+id/flightsIcon"
app:layout_constraintTop_toTopOf="#+id/flightsIcon" />
<TextView
android:id="#+id/spaceStationLabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="15dp"
android:text="#string/space_stations"
app:layout_constraintEnd_toEndOf="#+id/spaceStationIcon"
app:layout_constraintStart_toStartOf="#+id/spaceStationIcon"
app:layout_constraintTop_toBottomOf="#+id/spaceStationIcon" />
<TextView
android:id="#+id/textView1"
android:layout_width="124dp"
android:layout_height="98dp"
android:layout_marginEnd="40dp"
android:background="#color/colorPrimary"
android:gravity="center"
android:paddingEnd="20dp"
android:text="#string/dca"
android:textColor="#android:color/white"
app:layout_constraintBottom_toBottomOf="#+id/doubleArrowsIcon"
app:layout_constraintEnd_toEndOf="#+id/doubleArrowsIcon"
app:layout_constraintTop_toTopOf="#+id/doubleArrowsIcon" />
<TextView
android:id="#+id/roverLabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="15dp"
android:text="#string/rovers"
app:layout_constraintEnd_toEndOf="#+id/roverIcon"
app:layout_constraintStart_toStartOf="#+id/roverIcon"
app:layout_constraintTop_toBottomOf="#+id/roverIcon" />
<TextView
android:id="#+id/textView2"
android:layout_width="124dp"
android:layout_height="98dp"
android:layout_marginStart="40dp"
android:background="#color/colorPrimary"
android:gravity="center"
android:paddingStart="20dp"
android:text="#string/mars"
android:textColor="#android:color/white"
app:layout_constraintBottom_toBottomOf="#+id/doubleArrowsIcon"
app:layout_constraintStart_toStartOf="#+id/doubleArrowsIcon"
app:layout_constraintTop_toTopOf="#+id/doubleArrowsIcon" />
<TextView
android:id="#+id/flightsLabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="15dp"
android:text="#string/flights"
app:layout_constraintEnd_toEndOf="#+id/flightsIcon"
app:layout_constraintStart_toStartOf="#+id/flightsIcon"
app:layout_constraintTop_toBottomOf="#+id/flightsIcon" />
<ImageView
android:id="#+id/doubleArrowsIcon"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_marginBottom="40dp"
android:src="#drawable/double_arrows"
app:layout_constraintBottom_toTopOf="#+id/guideline1"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
<Switch
android:id="#+id/switch1"
android:layout_width="160dp"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="200dp"
android:background="#color/colorAccent"
android:checked="false"
android:padding="8dp"
android:switchPadding="24dp"
android:text="#string/one_way"
android:textColor="#android:color/white"
app:layout_constraintStart_toStartOf="#+id/guideline2"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:background="#color/colorAccent"
android:padding="8dp"
android:text="#string/traveller"
android:textColor="#android:color/white"
app:layout_constraintStart_toStartOf="#+id/guideline2"
app:layout_constraintTop_toBottomOf="#+id/switch1" />
<ImageView
android:id="#+id/rocketIcon"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_marginEnd="8dp"
android:src="#drawable/rocket_icon"
app:layout_constraintCircle="#id/galaxyIcon"
app:layout_constraintCircleAngle="270"
app:layout_constraintCircleRadius="100dp" />
<ImageView
android:id="#+id/galaxyIcon"
android:layout_width="90dp"
android:layout_height="90dp"
android:src="#drawable/galaxy"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="#+id/guideline1"
app:layout_constraintVertical_bias="0.495" />
<Button
android:id="#+id/departButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/depart"
android:textColor="#android:color/white"
app:backgroundTint="#color/colorPrimary"
app:layout_constraintBottom_toBottomOf="parent"
tools:layout_editor_absoluteX="158dp" />
<android.support.constraint.Guideline
android:id="#+id/guideline1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_begin="0dp" />
<android.support.constraint.Guideline
android:id="#+id/guideline2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintGuide_percent="1" />
</android.support.constraint.ConstraintLayout>
`
can anyone tell me what I might be missing or how this can be fixed?
Thanks in advance.
You're suppose to reference a view from the xml before you can use it.
Do this, also if the name of the layout the MainActivity is suppose to use is main.xml, it should be the one your setContentView use not some other layout.
lateinit var cosntraintLayout: ConstraintLayout
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.main) // <- main.xml
cosntraintLayout = findViewById<ConstraintLayout>(R.id.ConstraintLayout) // <- add this line
...
...
}
switch1 and departButton will also throw the same error, but I'll leave it for you to figure it out based on the code I provided.

setOnClickListener not working for button in Android Kotlin

I am trying to build a simple calculator app for android using kotlin. But setOnClickListener is not working while clicking the buttons.
MainActiviy.kt
package com.example.calculator
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.view.View
import android.widget.Button
import android.widget.EditText
import android.widget.TextView
import android.widget.Toast
import kotlinx.android.synthetic.main.activity_main.*
import kotlin.math.log
class MainActivity : AppCompatActivity() {
private lateinit var result:EditText
private lateinit var newNumber:EditText
private lateinit var displayOperation:TextView
private var operand1:Double?=null
private var operand2:Double=0.0
private var pendingOperation ="="
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
result = findViewById(R.id.result)
newNumber=findViewById(R.id.newNumber)
displayOperation=findViewById(R.id.displayOperation)
val button0:Button = findViewById(R.id.button0)
val button1:Button = findViewById(R.id.button1)
val button2:Button = findViewById(R.id.button2)
val button3:Button = findViewById(R.id.button3)
val button5:Button = findViewById(R.id.button5)
val button6:Button = findViewById(R.id.button6)
val button4:Button = findViewById(R.id.button4)
val button7:Button = findViewById(R.id.button7)
val button8:Button = findViewById(R.id.button8)
val button9:Button = findViewById(R.id.button9)
val buttonDot:Button = findViewById(R.id.buttonDot)
val buttonEqual:Button = findViewById(R.id.buttonEqual)
val buttonAdd:Button = findViewById(R.id.buttonAdd)
val buttonSub:Button = findViewById(R.id.buttonSub)
val buttonMul:Button = findViewById(R.id.buttonMul)
val buttonDiv:Button = findViewById(R.id.buttonDiv)
val listener = View.OnClickListener { v->
val b = v as Button
newNumber.append(b.text)
}
button0.setOnClickListener { listener }
button1.setOnClickListener { listener }
button2.setOnClickListener { listener }
button3.setOnClickListener { listener }
button4.setOnClickListener { listener }
button5.setOnClickListener { listener }
button6.setOnClickListener { listener }
button7.setOnClickListener { listener }
button8.setOnClickListener { listener }
button9.setOnClickListener { listener }
buttonDot.setOnClickListener { listener }
}
}
activity_main.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">
<Button
android:id="#+id/button0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:clickable="true"
android:minWidth="48dp"
android:text="0"
app:layout_constraintStart_toStartOf="#+id/button1"
app:layout_constraintTop_toBottomOf="#+id/button1" />
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:minWidth="48dp"
android:text="1"
app:layout_constraintStart_toStartOf="#+id/button4"
app:layout_constraintTop_toBottomOf="#+id/button4" />
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:minWidth="48dp"
android:text="2"
app:layout_constraintBaseline_toBaselineOf="#+id/button1"
app:layout_constraintStart_toEndOf="#+id/button1" />
<Button
android:id="#+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:minWidth="48dp"
android:text="3"
app:layout_constraintBaseline_toBaselineOf="#+id/button2"
app:layout_constraintStart_toEndOf="#+id/button2" />
<Button
android:id="#+id/button8"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:minWidth="48dp"
android:text="8"
app:layout_constraintBaseline_toBaselineOf="#+id/button7"
app:layout_constraintStart_toEndOf="#+id/button7" />
<Button
android:id="#+id/button6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:minWidth="48dp"
android:text="6"
app:layout_constraintBaseline_toBaselineOf="#+id/button5"
app:layout_constraintStart_toEndOf="#+id/button5" />
<Button
android:id="#+id/button4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:minWidth="48dp"
android:text="4"
app:layout_constraintStart_toStartOf="#+id/button7"
app:layout_constraintTop_toBottomOf="#+id/button7" />
<Button
android:id="#+id/button5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:minWidth="48dp"
android:text="5"
app:layout_constraintBaseline_toBaselineOf="#+id/button4"
app:layout_constraintStart_toEndOf="#+id/button4" />
<Button
android:id="#+id/button7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:minWidth="48dp"
android:text="7"
app:layout_constraintStart_toStartOf="#+id/newNumber"
app:layout_constraintTop_toBottomOf="#+id/newNumber" />
<Button
android:id="#+id/button9"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:minWidth="48dp"
android:text="9"
app:layout_constraintBaseline_toBaselineOf="#+id/button8"
app:layout_constraintStart_toEndOf="#+id/button8" />
<Button
android:id="#+id/buttonDot"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:minWidth="48dp"
android:text="."
app:layout_constraintBaseline_toBaselineOf="#+id/button0"
app:layout_constraintStart_toEndOf="#+id/button0" />
<Button
android:id="#+id/buttonEqual"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:minWidth="48dp"
android:text="="
app:layout_constraintBaseline_toBaselineOf="#+id/buttonDot"
app:layout_constraintStart_toEndOf="#+id/buttonDot" />
<Button
android:id="#+id/buttonAdd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:minWidth="48dp"
android:text="+"
app:layout_constraintBaseline_toBaselineOf="#+id/buttonEqual"
app:layout_constraintStart_toEndOf="#+id/buttonEqual" />
<Button
android:id="#+id/buttonDiv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:minWidth="48dp"
android:text="/"
app:layout_constraintBaseline_toBaselineOf="#+id/button9"
app:layout_constraintStart_toEndOf="#+id/button9" />
<Button
android:id="#+id/buttonMul"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:minWidth="48dp"
android:text="*"
app:layout_constraintBaseline_toBaselineOf="#+id/button6"
app:layout_constraintStart_toEndOf="#+id/button6" />
<Button
android:id="#+id/buttonSub"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:minWidth="48dp"
android:text="-"
app:layout_constraintBaseline_toBaselineOf="#+id/button3"
app:layout_constraintStart_toEndOf="#+id/button3" />
<TextView
android:id="#+id/displayOperation"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp"
android:textSize="18sp"
android:textStyle="bold"
app:layout_constraintBaseline_toBaselineOf="#+id/newNumber"
app:layout_constraintEnd_toStartOf="#+id/newNumber"
app:layout_constraintStart_toStartOf="parent" />
<EditText
android:id="#+id/result"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:ems="10"
android:focusable="false"
android:focusableInTouchMode="false"
android:inputType="numberSigned|numberDecimal"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<EditText
android:id="#+id/newNumber"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:ems="10"
android:inputType="numberSigned|text|numberDecimal"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/result" />
</androidx.constraintlayout.widget.ConstraintLayout>
This is because you're not setting any listener to the buttons, but you're defining new listeners that don't invoke any method
This will work:
button.setOnClickListener(listener)
This will work too, but here you have 2 listeners: the original one invoked by another one:
button.setOnClickListener{ listener.onClick(it) }
If you really do not want to implement View.OnClickListener you just need to trigger your local listener on button click.
e.g. for button0 make it
button0.setOnClickListener { listener.onClick(it) }
Data binding can reduce the findViewById lines. My latest environment provides data binding for kotlin by default.
first of all, you don't need to use findViewById in Kotlin. You can use it directly using id you provide in xml.
Use click listener like below
button6.setOnClickListener(object :View.OnClickListener{
// do button click
})
For multiple button click you can implement View.OnClickListener in activity and use it like.
class MainActivity : AppCompatActivity(),View.OnClickListener {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
button6.setOnClickListener = this
}
override fun onClick(v: View?) {
when (v?.id) {
R.id.button6 -> {
// do button click
}
}
}
}
If you don't want to set setOnClickListener for every button you can use databinding for that as well. It reduce lots of code.

Create multiple sets of plus and minus buttons with TextView on same page of App (Kotlin)

I have two controls that i can increase or decrease an integer. I click on either the first or second set of plus and minus buttons,despite being separate functions, it'll display the Integers on both outputs. For example if I click on the plus and minus button for the integer_number_1, it'll display the numbers on both the integer_number_1 and integer_number_2 outputs/TextView.
How do I fix this??? I've so many different ways and none of them worked.
Here's my xml section:
<?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>
And here's my kt file:
package com.example.plus_and_minus_input
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import kotlinx.android.synthetic.main.activity_main.*
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
increase_1.setOnClickListener { increaseInteger1() }
decrease_1.setOnClickListener { decreaseInteger1() }
increase_2.setOnClickListener { increaseInteger2() }
decrease_2.setOnClickListener { decreaseInteger2() }
}
fun increaseInteger1() {
display(integer_number_1.text.toString().toInt() + 1)
}
fun decreaseInteger1() {
display(integer_number_1.text.toString().toInt() - 1)
}
fun increaseInteger2() {
display(integer_number_2.text.toString().toInt() + 1)
}
fun decreaseInteger2() {
display(integer_number_2.text.toString().toInt() - 1)
}
private fun display(number: Int) {
integer_number_1.setText("$number")
integer_number_2.setText("$number")
}
}
I think you need to separate this following display methods.
private fun display(number: Int) {
integer_number_1.setText("$number")
integer_number_2.setText("$number")
}
to
private fun display_number_1(number: Int) {
integer_number_1.setText("$number")
}
and
private fun display_number_2(number: Int) {
integer_number_2.setText("$number")
}

Android (Kotlin): text now showing after I set it

Playing around with Android and following this simple introduction but I can't get the line resultsTextView.text = rand.toString() to trigger. Not sure what I'm missing.
The main Kotlin code looks like this (MainActivity.kt):
package io.github.ovid.randomizer
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Button
import android.widget.SeekBar
import android.widget.TextView
import kotlin.random.Random
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val rollButton = findViewById<Button>(R.id.rollButton)
val resultsTextView = findViewById<TextView>(R.id.resultsTextView)
val seekBar = findViewById<SeekBar>(R.id.seekBar)
rollButton.setOnContextClickListener {
val rand = Random.nextInt(seekBar.progress) + 1
resultsTextView.text = rand.toString()
true
}
}
}
And my activity_main.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" >
<Button
android:id="#+id/rollButton"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="96dp"
android:layout_marginLeft="96dp"
android:layout_marginEnd="96dp"
android:layout_marginRight="96dp"
android:layout_marginBottom="24dp"
android:text="Roll"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
<SeekBar
android:id="#+id/seekBar"
style="#style/Widget.AppCompat.SeekBar.Discrete"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="32dp"
android:layout_marginLeft="32dp"
android:layout_marginEnd="32dp"
android:layout_marginRight="32dp"
android:layout_marginBottom="24dp"
android:max="10"
android:progress="3"
app:layout_constraintBottom_toTopOf="#+id/rollButton"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="24dp"
android:layout_marginLeft="24dp"
android:layout_marginBottom="16dp"
android:text="How Many"
app:layout_constraintBottom_toTopOf="#+id/seekBar"
app:layout_constraintStart_toStartOf="parent" />
<View
android:id="#+id/divider"
android:layout_width="409dp"
android:layout_height="1dp"
android:layout_marginBottom="16dp"
android:background="?android:attr/listDivider"
app:layout_constraintBottom_toTopOf="#+id/textView"
tools:layout_editor_absoluteX="1dp" />
<TextView
android:id="#+id/resultsTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="0dp"
android:layout_marginLeft="0dp"
android:textAppearance="#style/TextAppearance.AppCompat.Large"
android:textSize="144sp"
app:layout_constraintBottom_toTopOf="#+id/divider"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
Everything on the layout in the emulator looks correct, but pressing the "Roll" button shows I clicked the button, but no text shows up in the resultsTextView.
D'oh! It was setOnClickListener, not setOnContextClickListener. Made the change and removed the true at the end and it worked fine. Sorry for the noise.
Just replace the event method from setOnContextClickListener to setOnClickListener, an it will work fine. Just like this:
rollButton.setOnClickListener {
val rand = Random.nextInt(seekBar.progress) + 1
resultsTextView.text = rand.toString()
}

Categories

Resources