CalendarView doesn't show when visabilty is initally set to GONE - android

UPDATE: I added my whole code after I fixed it as I stated in my answer
hello guys I am having a weird bug. I have a calendar view that's initially set to GONE and I try to change it to VISIBLE and GONE when the user clicks on an icon.
it doesn't show.
but, when I set it initially to VISIBLE it works as expected.
I tried debugging and it enters both branches of the when statement, but it doesn't change the visibility
any idea what I am doing wrong here?
calendar_icon.setOnClickListener {
when (calendarView.visibility) {
View.GONE -> {
calendarView.visibility = View.VISIBLE
}
else -> { calendarView.visibility = View.GONE
}
}
}
XML code
<?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:id="#+id/per_day_root"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/brighter_white">
<androidx.cardview.widget.CardView
android:id="#+id/cardView2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="16dp"
app:cardBackgroundColor="#color/brighter_white"
app:cardCornerRadius="12dp"
app:cardElevation="12dp"
app:contentPadding="4dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:id="#+id/per_day_search"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="8dp"
android:layout_weight="4"
android:background="#android:color/transparent"
android:hint="#string/search"
android:textColorHint="#color/silver"
android:textSize="14sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="#+id/imageView11"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.6" />
<ImageView
android:id="#+id/imageView11"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_weight="1"
android:background="#drawable/rounded_corner_azure_bg"
android:padding="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="#drawable/ic_search_line" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.cardview.widget.CardView>
<LinearLayout
android:id="#+id/linearLayout"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginEnd="16dp"
android:background="#drawable/rounded_corner_lavendar_bg"
android:padding="8dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/calendarView"
app:layout_goneMarginTop="16dp">
<TextView
android:id="#+id/per_day_current_date"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:layout_weight="4"
android:fontFamily="#font/montserrat_medium"
android:text="TextView"
android:textColor="#color/charcoal"
android:textSize="15sp" />
<ImageView
android:id="#+id/calendar_icon"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
app:srcCompat="#drawable/ic_calendar" />
</LinearLayout>
<LinearLayout
android:id="#+id/linearLayout5"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:orientation="horizontal"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/linearLayout">
<TextView
android:id="#+id/report_item_name"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginRight="16dp"
android:layout_weight="1"
android:background="#color/periwinkle"
android:fontFamily="#font/montserrat_medium"
android:gravity="center"
android:paddingStart="8dp"
android:paddingLeft="8dp"
android:paddingTop="16dp"
android:paddingEnd="8dp"
android:paddingRight="8dp"
android:paddingBottom="16dp"
android:text="#string/user_name"
android:textColor="#color/charcoal"
android:textStyle="bold" />
<TextView
android:id="#+id/report_item_attend"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginRight="16dp"
android:layout_weight="1"
android:background="#color/platinum"
android:fontFamily="#font/montserrat_medium"
android:gravity="center"
android:maxLines="2"
android:paddingStart="8dp"
android:paddingLeft="8dp"
android:paddingTop="16dp"
android:paddingEnd="8dp"
android:paddingRight="8dp"
android:paddingBottom="16dp"
android:text="#string/attend_time"
android:textColor="#color/charcoal"
android:textStyle="bold" />
<TextView
android:id="#+id/report_item_leave"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginRight="16dp"
android:layout_weight="1"
android:background="#color/misty_rose"
android:fontFamily="#font/montserrat_medium"
android:gravity="center"
android:maxLines="2"
android:paddingStart="8dp"
android:paddingLeft="8dp"
android:paddingTop="16dp"
android:paddingEnd="8dp"
android:paddingRight="8dp"
android:paddingBottom="16dp"
android:text="#string/leave_time"
android:textColor="#color/charcoal"
android:textStyle="bold" />
<TextView
android:id="#+id/report_item_lost_points"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginRight="16dp"
android:layout_weight="1"
android:background="#color/magnolia"
android:ellipsize="end"
android:fontFamily="#font/montserrat_medium"
android:gravity="center"
android:maxLines="2"
android:paddingStart="8dp"
android:paddingLeft="8dp"
android:paddingTop="16dp"
android:paddingEnd="8dp"
android:paddingRight="8dp"
android:paddingBottom="16dp"
android:text="#string/lostPoints"
android:textColor="#color/charcoal"
android:textStyle="bold" />
</LinearLayout>
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/report_rv"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginTop="8dp"
android:layout_marginBottom="16dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/linearLayout5" />
<CalendarView
android:id="#+id/calendarView"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="16dp"
android:visibility="visible"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/cardView2"
tools:visibility="visible" />
</androidx.constraintlayout.widget.ConstraintLayout>
fragment code
package iti.intake40.mawgood_admin.reports.per_day
import android.app.DatePickerDialog
import android.os.Build
import android.os.Bundle
import android.text.Editable
import android.text.TextWatcher
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.annotation.RequiresApi
import androidx.fragment.app.Fragment
import androidx.recyclerview.widget.LinearLayoutManager
import com.google.android.material.snackbar.Snackbar
import iti.intake40.mawgood_admin.R
import iti.intake40.mawgood_admin.core.DateUtilities
import iti.intake40.mawgood_admin.models.Report
import kotlinx.android.synthetic.main.fragment_reports.*
class ReportsFragment : Fragment(), ReportPerDayContract.IView {
private val TAG = ReportsFragment::class.java.simpleName
private lateinit var presenter: ReportPerDayPresenter
private lateinit var adapter: ReportPerDayAdapter
private val reportList = ArrayList<Report>()
private var rootView: View? = null
private var mDatePickerDialog: DatePickerDialog? = null
private var per_day_current_date: TextView? = null
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
val root = inflater.inflate(R.layout.fragment_reports, container, false)
per_day_current_date = root.findViewById(R.id.per_day_current_date)
presenter = ReportPerDayPresenter(context!!, this)
return root
}
#RequiresApi(Build.VERSION_CODES.O)
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
rootView = view.findViewById(R.id.per_day_root)
presenter.getOrganizationCreationDate()
calendarView.maxDate = System.currentTimeMillis()
calendarView.visibility = View.GONE
calendar_icon.setOnClickListener {
when (calendarView.visibility) {
View.GONE -> {
calendarView.visibility = View.VISIBLE
}
View.VISIBLE -> {
calendarView.visibility = View.GONE
}
}
}
calendarView.setOnDateChangeListener { view, year, month, dayOfMonth ->
calendarView.visibility = View.GONE
val date = "$dayOfMonth-${month + 1}-$year"
Log.d(TAG, date)
val dayInUnix = DateUtilities.convertDateToTimestamp(date)
presenter.listenForChanges(dayInUnix.toString())
}
}
#RequiresApi(Build.VERSION_CODES.O)
override fun onStart() {
super.onStart()
activity!!.title = "Reports"
search()
per_day_current_date?.text = DateUtilities.getTodayInDate()
val today = DateUtilities.getTodayInUnix().toString()
presenter.listenForChanges(today)
}
private fun search() {
per_day_search.addTextChangedListener(object : TextWatcher {
override fun afterTextChanged(s: Editable?) {}
override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {}
override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
adapter.filter.filter(s)
}
})
}
override fun onStop() {
super.onStop()
presenter.detachListeners()
}
override fun displayNoData() {
// per_day_empty_iv.visibility = View.VISIBLE
// per_day_empty_tv.visibility = View.VISIBLE
report_rv.visibility = View.INVISIBLE
}
override fun hideNoData() {
// per_day_empty_iv.visibility = View.INVISIBLE
// per_day_empty_tv.visibility = View.INVISIBLE
report_rv.visibility = View.VISIBLE
}
override fun updateRecyclerView(reports: List<Report>) {
reportList.clear()
reportList.addAll(reports)
adapter = ReportPerDayAdapter(reportList)
report_rv.setHasFixedSize(true)
report_rv.layoutManager = LinearLayoutManager(context)
report_rv.adapter = adapter
adapter.notifyDataSetChanged()
}
override fun updateDayView(date: String) {
per_day_current_date?.text = date
}
override fun showConnectionError() {
Snackbar.make(rootView!!, R.string.no_connection_show_cache, Snackbar.LENGTH_LONG)
.show()
}
override fun setCalendarStartDate(creationDateUnix: Long) {
calendarView.minDate = creationDateUnix
Log.d(TAG, creationDateUnix.toString())
}
}

it seems like a bug. I was able to fix this by removing the visibility attribute from XML.
and set the visibility programmatically in onViewCreated like this: calendarView.visibility = View.GONE

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.

Horizontal RecyclerView in Horizontal RecyclerView is not scrolling

I have horizontal recycler view in which each child has nested scroll view in which I have another horizontal recycler view. View seems to bo displaying fine but I am not able to scroll child recycler view. When I am trying to scroll - parent recycler view is scrolling, which is not what I want. Already was trying to set android:nestedScrollingEnabled="false” but it’s not working.
Main view 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=".views.home.WeatherView">
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/rv_WeatherRecycler"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginStart="1dp"
android:layout_marginTop="1dp"
android:layout_marginEnd="1dp"
android:layout_marginBottom="1dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<ProgressBar
android:id="#+id/pb_WeatherLoading"
style="?android:attr/progressBarStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
Parent recycler view item xml:
<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView 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_margin="8dp"
app:cardCornerRadius="40dp"
app:cardElevation="5dp">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
app:srcCompat="#drawable/ic_location">
<ImageView
android:id="#+id/iv_CityImage"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="gone"
tools:srcCompat="#tools:sample/avatars"
/>
<androidx.core.widget.NestedScrollView
android:id="#+id/sv_ScrollView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="#+id/iv_LocationIcon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="32dp"
app:layout_constraintEnd_toStartOf="#+id/tv_CityName"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintHorizontal_chainStyle="packed"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="#drawable/ic_location" />
<TextView
android:id="#+id/tv_CityName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView"
app:layout_constraintBottom_toBottomOf="#+id/iv_LocationIcon"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toEndOf="#+id/iv_LocationIcon"
app:layout_constraintTop_toTopOf="#+id/iv_LocationIcon" />
<TextView
android:id="#+id/tv_CurrentTemp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="400dp"
android:text="TextView"
android:textSize="48sp"
app:layout_constraintBottom_toTopOf="#+id/tv_CurrentWeatherDesc"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.1"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/iv_LocationIcon"
app:layout_constraintVertical_chainStyle="spread" />
<TextView
android:id="#+id/tv_CurrentWeatherDesc"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="24dp"
android:text="TextView"
android:textSize="20sp"
app:layout_constraintBottom_toTopOf="#+id/tv_SunriseLabel"
app:layout_constraintEnd_toEndOf="#+id/tv_CurrentTemp"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="#+id/tv_CurrentTemp"
app:layout_constraintTop_toBottomOf="#+id/tv_CurrentTemp" />
<TextView
android:id="#+id/tv_SunriseLabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="32dp"
android:text="Sunrise"
app:layout_constraintBottom_toTopOf="#+id/tv_SunsetLabel"
app:layout_constraintEnd_toStartOf="#+id/tv_SunriseValue"
app:layout_constraintHorizontal_chainStyle="packed"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/tv_CurrentWeatherDesc" />
<TextView
android:id="#+id/tv_SunsetLabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:layout_marginBottom="32dp"
android:text="Sunset"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="#+id/tv_SunsetValue"
app:layout_constraintHorizontal_chainStyle="packed"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/tv_SunriseLabel" />
<TextView
android:id="#+id/tv_SunriseValue"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="32dp"
android:text="SunriseVal"
app:layout_constraintBaseline_toBaselineOf="#+id/tv_SunriseLabel"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toEndOf="#+id/tv_SunriseLabel" />
<TextView
android:id="#+id/tv_SunsetValue"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="32dp"
android:text="SunsetVal"
app:layout_constraintBaseline_toBaselineOf="#+id/tv_SunsetLabel"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toEndOf="#+id/tv_SunsetLabel" />
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/rv_HourlyForecastRecycler"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="32dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="32dp"
android:scrollbars="horizontal|vertical"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/tv_SunsetLabel" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.core.widget.NestedScrollView>
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.cardview.widget.CardView>
Child item xml:
<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView 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="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="8dp"
app:cardCornerRadius="10dp"
app:cardElevation="4dp">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/tv_HourlyForecast_Time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="8dp"
android:text="TextView"
app:layout_constraintBottom_toTopOf="#+id/iv_ForecastHourly_WeatherIcon"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<ImageView
android:id="#+id/iv_ForecastHourly_WeatherIcon"
android:layout_width="64dp"
android:layout_height="64dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="8dp"
app:layout_constraintBottom_toTopOf="#+id/tv_HourlyForecast_Temp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/tv_HourlyForecast_Time"
app:srcCompat="#drawable/ic_error" />
<TextView
android:id="#+id/tv_HourlyForecast_Temp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="8dp"
android:text="TextView"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/iv_ForecastHourly_WeatherIcon" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.cardview.widget.CardView>
Main view code:
#AndroidEntryPoint
class WeatherView : Fragment() {
private var _binding: FragmentWeatherViewBinding? = null
private val binding: FragmentWeatherViewBinding get() = _binding!!
private val viewModel by viewModels<WeatherViewModel>()
private lateinit var weatherAdapter: WeatherAdapter
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View {
_binding = FragmentWeatherViewBinding.inflate(inflater, container, false)
return binding.root
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
val cities = getSavedCitiesFromFirebase()
viewModel.getWeatherAndImageForCities(cities)
setupRecyclerView()
observeWeatherData()
}
private fun observeWeatherData() {
viewModel.citiesLoadingLiveData.observe(viewLifecycleOwner, { isLoading ->
if (isLoading) {
showLoading(true)
} else {
showLoading(false)
}
})
viewModel.citiesDataLiveData.observe(viewLifecycleOwner, { cities ->
if (cities != null) {
weatherAdapter.setCities(cities)
}
})
viewModel.cityFetchErrorLiveData.observe(viewLifecycleOwner, {
/* To Do */
})
}
private fun setupRecyclerView() {
weatherAdapter = WeatherAdapter()
binding.rvWeatherRecycler.apply {
layoutManager = LinearLayoutManager(
requireContext(), RecyclerView.HORIZONTAL, false
)
adapter = weatherAdapter
}
}
private fun showLoading(isLoading: Boolean) {
binding.pbWeatherLoading.visibility = if (isLoading) View.VISIBLE else View.GONE
}
private fun getSavedCitiesFromFirebase(): List<String> {
return listOf("Berlin", "Amsterdam")
}
}
Parent recycler view adapter code:
class WeatherAdapter: RecyclerView.Adapter<WeatherAdapter.WeatherViewHolder>() {
private var cities: MutableList<CityData> = mutableListOf()
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): WeatherViewHolder {
val itemBinding = ItemWeatherBinding.inflate(LayoutInflater.from(parent.context), parent, false)
return WeatherViewHolder(itemBinding.root, itemBinding)
}
override fun onBindViewHolder(holder: WeatherViewHolder, position: Int) {
holder.bind(cities[position])
}
override fun getItemCount(): Int {
return cities.size
}
fun setCities(newCities: List<CityData>) {
cities.clear()
cities.addAll(newCities)
notifyDataSetChanged()
}
class WeatherViewHolder(private val itemView: View, private val itemBinding: ItemWeatherBinding): RecyclerView.ViewHolder(itemBinding.root) {
fun bind(item: CityData) {
itemBinding.rvHourlyForecastRecycler.apply {
layoutManager = LinearLayoutManager(
itemView.context, RecyclerView.HORIZONTAL, false
)
adapter = HourlyForecastAdapter(item.forecastModel.list)
setHasFixedSize(true)
}
itemBinding.tvCityName.text = item.weatherModel.name
itemBinding.tvCurrentTemp.text = TemperatureConverter.convertKelvinToCelsius(item.weatherModel.main.temp).toString()
itemBinding.tvCurrentWeatherDesc.text = item.weatherModel.weather[0].description
itemBinding.tvSunriseValue.text = TimeConverter.convertTime(item.weatherModel.sys.sunrise)
itemBinding.tvSunsetValue.text = TimeConverter.convertTime(item.weatherModel.sys.sunset)
Glide
.with(itemBinding.ivCityImage)
.load(item.cityImg)
.error(R.drawable.ic_error)
.centerCrop()
.into(itemBinding.ivCityImage)
}
}
}
Child recycler view adapter code:
class HourlyForecastAdapter(private val forecast: List<WeatherForecast>): RecyclerView.Adapter<HourlyForecastAdapter.HourlyForecastViewHolder>() {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): HourlyForecastViewHolder {
val itemBinding = ItemWeatherHourlyForecastBinding.inflate(LayoutInflater.from(parent.context), parent, false)
return HourlyForecastViewHolder(itemBinding)
}
override fun onBindViewHolder(holder: HourlyForecastViewHolder, position: Int) {
holder.bind(forecast[position])
}
override fun getItemCount(): Int {
return forecast.size
}
class HourlyForecastViewHolder(private val itemBinding: ItemWeatherHourlyForecastBinding): RecyclerView.ViewHolder(itemBinding.root) {
fun bind(item: WeatherForecast) {
Timber.d("DT: ${item.dt}")
itemBinding.tvHourlyForecastTime.text = TimeConverter.convertTime(item.dt.toInt())
itemBinding.tvHourlyForecastTemp.text = TemperatureConverter.convertKelvinToCelsius(item.main.temp).toString()
Glide
.with(itemBinding.ivForecastHourlyWeatherIcon)
.load("https://openweathermap.org/img/wn/${item.weather[0].icon}#2x.png")
.error(R.drawable.ic_error)
.centerCrop()
.into(itemBinding.ivForecastHourlyWeatherIcon)
}
}
}

How to test(tdd) an element inside child recycler view?

add to cart errorI've used a recycler view inside another recycler view. While using test-driven development, I was trying to test the click functionality of add to cart button code as follows:
MenuDishAdapter- child recycler view
MenuAdapter- parent recycler view
class MenuAdapter(private val context: Context, val menuList:List<MenuData>, val menuActivity: MenuActivity):RecyclerView.Adapter<MenuAdapter.MenuViewHolder>() {
class MenuViewHolder(private val itemBinding:RvDishGroupBinding):RecyclerView.ViewHolder(itemBinding.root) {
val dishGroupBinding:RvDishGroupBinding = itemBinding
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MenuViewHolder = MenuViewHolder(
RvDishGroupBinding.inflate(LayoutInflater.from(parent.context), parent, false))
#SuppressLint("CutPasteId")
override fun onBindViewHolder(holder: MenuViewHolder, position: Int) {
val getPos= menuList[position]
holder.dishGroupBinding.dishCatgTypeMtv.text=getPos.categoryType
val adapter = MenuDishAdapter(context, menuList[position].categoryDish, menuActivity, )
holder.dishGroupBinding.dishLl.adapter= adapter
/*Logger.i("Menu list size ${menuList.size}")
Logger.i("Menu list $menuList")*/
if(position==menuList.size-1)
{
holder.dishGroupBinding.rvGroupSeparatorVw.visibility= View.GONE
}
}
override fun getItemCount(): Int = menuList.size
}
class MenuDishAdapter(
private val context: Context,
val menuList: List<DishData>,
val menuActivity: MenuActivity
) :
RecyclerView.Adapter<MenuDishAdapter.MenuDishViewHolder>() {
class MenuDishViewHolder(private val dishItem: RvDishItemBinding) :
RecyclerView.ViewHolder(dishItem.root) {
val dishItemBinding = dishItem
}
var isFavourite: Boolean = false
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MenuDishViewHolder =
MenuDishViewHolder(
RvDishItemBinding.inflate(LayoutInflater.from(parent.context), parent, false)
)
override fun onBindViewHolder(holder: MenuDishViewHolder, position: Int) {
val getPos= menuList[position]
Glide.with(context).load(getPos.dishImage).placeholder(R.drawable.img_dessert).into( holder.dishItemBinding.rvDishImgIv)
holder.dishItemBinding.rvDishNameMtv.text= getPos.dishName
if(getPos.dishDescription.isNullOrEmpty())
{
holder.dishItemBinding.rvDishDescriptionMtv.visibility=View.GONE
}
else
{
holder.dishItemBinding.rvDishDescriptionMtv.text= getPos.dishDescription
}
holder.dishItemBinding.rvDishPriceMtv.text= "$"+ getPos.dishPrice
if(!getPos.isVeg)
{
holder.dishItemBinding.rvDishTypeIv.backgroundTintList=
ContextCompat.getColorStateList(menuActivity, R.color.red)
}
holder.dishItemBinding.rvDishCustomizationMtv.text= context.getString(R.string.your_customisations_are)+" Radish, Mushroom, carrots, turnip, radish"
holder.dishItemBinding.rvDishFavMarkIb.setOnClickListener {
if(isFavourite)
{
holder.dishItemBinding.rvDishFavMarkIb.setBackgroundResource(R.drawable.ic_unfav)
Toast.makeText(context, "${getPos.dishName} removed from Favourite. ", Toast.LENGTH_SHORT).show()
notifyDataSetChanged()
isFavourite= false
}
else
{
holder.dishItemBinding.rvDishFavMarkIb.setBackgroundResource(R.drawable.ic_fav)
Toast.makeText(context, "${getPos.dishName} marked Favourite. ", Toast.LENGTH_SHORT).show()
notifyDataSetChanged()
isFavourite=true
}
}
holder.dishItemBinding.rvAddToCartBtn.setOnClickListener {
Utils.printShortToast(context, "${getPos.dishName} added to the cart")
}
holder.dishItemBinding.rvDishCustomizeBtn.setOnClickListener {
Utils.printShortToast(context, "${getPos.dishName} customised")
}
}
override fun getItemCount(): Int = menuList.size
}
XML for MenuDishAdapter
<LinearLayout 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="133dp"
android:layout_marginStart="6dp"
android:layout_marginTop="5dp"
android:layout_marginEnd="6dp"
android:id="#+id/rv_dish_item_ll">
<androidx.constraintlayout.widget.ConstraintLayout
android:id="#+id/detail_parent_cl"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<androidx.cardview.widget.CardView
android:id="#+id/rv_dish_img_cv"
android:layout_width="109dp"
android:layout_height="104dp"
android:elevation="5dp"
app:cardCornerRadius="8dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<androidx.appcompat.widget.AppCompatImageView
android:id="#+id/rv_dish_img_iv"
android:layout_width="109dp"
android:layout_height="104dp"
android:scaleType="fitXY"
tools:src="#drawable/img_fav_2" />
</androidx.cardview.widget.CardView>
<androidx.appcompat.widget.LinearLayoutCompat
android:id="#+id/dish_details_ll"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginStart="8dp"
android:layout_marginEnd="0dp"
android:orientation="vertical"
android:weightSum="5"
app:layout_constraintBottom_toBottomOf="#id/rv_dish_img_cv"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="#id/rv_dish_img_cv"
app:layout_constraintTop_toTopOf="#id/rv_dish_img_cv">
<androidx.constraintlayout.widget.ConstraintLayout
android:id="#+id/dish_name_cl"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="0dp"
android:layout_weight="1"
android:orientation="horizontal">
<com.google.android.material.textview.MaterialTextView
android:id="#+id/rv_dish_name_mtv"
style="#style/TextAppearance.MdcTypographyStyles.Body22"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="0dp"
android:layout_marginEnd="8dp"
android:ellipsize="end"
android:gravity="start"
android:maxLines="1"
android:textColor="#color/black"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="#id/rv_dish_type_iv"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:text="Masala Noodles" />
<androidx.appcompat.widget.AppCompatImageView
android:id="#+id/rv_dish_type_iv"
android:layout_width="21dp"
android:layout_height="18dp"
android:layout_gravity="end"
android:background="#drawable/ic_veg_dish"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
<androidx.constraintlayout.widget.ConstraintLayout
android:id="#+id/detail_middle_cl"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="2"
android:orientation="horizontal">
<LinearLayout
android:id="#+id/text_ll"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="#id/rv_dish_fav_mark_ib"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<com.google.android.material.textview.MaterialTextView
android:id="#+id/rv_dish_description__mtv"
style="#style/TextAppearance.MdcTypographyStyles.Body12"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:maxLines="2"
android:layout_marginEnd="2dp"
android:layout_marginStart="0dp"
android:textAlignment="textStart"
android:textColor="#color/black"
tools:text="#string/description" />
<com.google.android.material.textview.MaterialTextView
android:id="#+id/rv_dish_price_mtv"
style="#style/TextAppearance.MdcTypographyStyles.Body22"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:maxLines="1"
android:textAlignment="textStart"
tools:text="$10.5" />
</LinearLayout>
<androidx.appcompat.widget.AppCompatImageButton
android:id="#+id/rv_dish_fav_mark_ib"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="1"
android:clickable="true"
android:elevation="5dp"
android:focusable="true"
android:paddingStart="10dp"
android:paddingTop="0dp"
android:paddingEnd="0dp"
android:paddingBottom="5dp"
android:background="#drawable/ic_unfav"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
<androidx.constraintlayout.widget.ConstraintLayout
android:id="#+id/buttons_cl"
android:layout_width="match_parent"
android:layout_height="23dp"
android:layout_marginTop="2dp"
android:paddingBottom="1dp"
android:paddingTop="0dp"
android:layout_weight="1"
android:orientation="horizontal">
<androidx.appcompat.widget.AppCompatButton
android:id="#+id/rv_add_to_cart_btn"
style="#style/TextAppearance.MdcTypographyStyles.Button1"
android:layout_width="109dp"
android:layout_height="0dp"
android:layout_marginStart="0dp"
android:layout_marginEnd="2dp"
android:background="#drawable/bg_button"
android:backgroundTint="#color/primaryColor"
android:drawableStart="#drawable/ic_button_cart"
android:drawablePadding="0dp"
android:focusable="true"
android:padding="2dp"
android:paddingStart="5dp"
android:paddingEnd="5dp"
android:text="#string/add_to_cart"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<androidx.appcompat.widget.AppCompatButton
android:id="#+id/rv_dish_customize_btn"
style="#style/TextAppearance.MdcTypographyStyles.Button1"
android:layout_width="109dp"
android:layout_height="0dp"
android:layout_marginStart="2dp"
android:layout_marginEnd="0dp"
android:background="#drawable/bg_button"
android:backgroundTint="#color/black"
android:focusable="true"
android:padding="2dp"
android:text="#string/customize"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.appcompat.widget.LinearLayoutCompat>
<com.google.android.material.textview.MaterialTextView
android:id="#+id/rv_dish_customization_mtv"
style="#style/TextAppearance.MdcTypographyStyles.Body12"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="4dp"
android:layout_marginBottom="3dp"
android:ellipsize="end"
android:maxLines="1"
android:text="#string/your_customisations_are"
android:textAlignment="textStart"
android:textColor="#color/black"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="#id/dish_details_ll"
app:layout_constraintStart_toStartOf="#id/rv_dish_img_cv"
app:layout_constraintTop_toBottomOf="#id/rv_dish_img_cv"
tools:text="Your customisations are - Onion, Tomato, Mushroom, cucumber, radish" />
</androidx.constraintlayout.widget.ConstraintLayout>
</LinearLayout>
Now while testing the add to cart button the test case is as follows:
**Class name: MenuActivityTest
see code commented in testingMenuList() **
#RunWith(AndroidJUnit4::class)
#LargeTest
class MenuActivityTest {
#get:Rule
var rule = ActivityScenarioRule(MenuActivity::class.java)
//test to check the visibility of the menu screen layout on the device
#Test
fun checkScreenDisplay() {
onView(withId(R.id.menu_cl))
.check(matches(isDisplayed()))
}
#Test
fun testingMenuList() {
//testing whether the recycler view is displayed for not
onView(withId(R.id.menu_rv)).check(matches(isDisplayed()))
onView(allOf(withId(R.id.dish_ll), isDisplayed()))
//testing the scroll to position to 3rd position
onView(withId(R.id.menu_rv)).perform(
RecyclerViewActions
.scrollToPosition<RecyclerView.ViewHolder>(3)
)
//testing a particular item of the list
onView(allOf(withId(R.id.dish_catg_type_mtv), withText("Beverages")))
onView(allOf(withId(R.id.rv_dish_name_mtv), withText("Coffee")))
onView(allOf(withId(R.id.rv_dish_price_mtv), withText("0")))
onView(
allOf(
withId(R.id.rv_dish_description__mtv),
withText("Because ground coffee beans, coffee roasts, and brewing methods vary, so do coffee drinks. They come in so many different varieties and flavors, such as black coffee, espresso, latte, cappuccino, or mocha.")
)
)
/*onView(allOf(isDisplayed(), withId(R.id.rv_add_to_cart_btn)))
.perform(RecyclerViewActions.actionOnItemAtPosition<MenuDishAdapter.MenuDishViewHolder>(2, click()));
*/
/*val result = onView(
allOf(
withId(R.id.rv_add_to_cart_btn),
withText("Coffee")
)
).perform(
click()
)
Utils.printErrorLog("Result is: $result")*/
onView(allOf(withId(R.id.rv_add_to_cart_btn), isDescendantOfA(allOf(withId(R.id.buttons_cl))))).check(matches(isFocusable()))
*/
/* onData(allOf(withId(R.id.rv_add_to_cart_btn)))
.inAdapterView(allOf(withId(R.id.rv_dish_item_ll)))
.atPosition(1)
.perform(click())*/
}
}
Each and every time in running the testingMenuList I'm getting the error(screenshot attached).
What is the correct method to check the add to cart button click functionality?
The flow is as: MenuActivity has a recycler view menu_rv and this menu_rv contains another recycler view name dish_ll. Inside dish_ll, rv_add_to_cart_btn is the button that is being tested using the TDD approach.

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")
}

Reset integers in a TextView to 0 (Kotlin)

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.

Categories

Resources