I created an activity which have a button, when clicked it show a dialog box, this dialog box have two buttons, “Confirm” and “Cancel”. When I press the cancel, I want to add data to alist and show the list. I wnat the answer to be based on Kotlin, not Java.
I created the code which will be used
package com.example.food
import android.annotation.SuppressLint
import android.app.Dialog
import android.content.Intent
import android.graphics.Color
import android.graphics.drawable.ColorDrawable
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.Window
import android.widget.Button
import android.widget.ImageView
import android.widget.TextView
import android.widget.Toast
class GrillDetailedActivity : AppCompatActivity() {
private val comenziList = mutableListOf<String>()
@SuppressLint("MissingInflatedId")
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_grill_detailed)
val imagine: ImageView = findViewById(R.id.detailedImageView)
val nume: TextView = findViewById(R.id.detailedNume)
val cantitate: TextView = findViewById(R.id.detailedCantitate)
val pret: TextView = findViewById(R.id.detailedPret)
val bundle: Bundle? = intent.extras
val imageId = bundle?.getInt("imageId")
val grillName = bundle!!.getString("grillName")
val cantitateGrill = bundle!!.getString("cantitateGrill")
val pretGrill = bundle!!.getString("pretGrill")
imagine.setImageResource(imageId!!)
nume.text = grillName
cantitate.text = cantitateGrill
pret.text = pretGrill
val comandaBtn: Button = findViewById(R.id.buttonComanda)
comandaBtn.setOnClickListener {
val message: String? = "Doriti sa finalizati comanda?"
showCustomDialogBox(message)
}
}
private fun showCustomDialogBox(grillName: String?) {
val dialog = Dialog(this)
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE)
dialog.setCancelable(false)
dialog.setContentView(R.layout.comanda_dialog_box)
dialog.window?.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
val tvMessage: TextView = dialog.findViewById(R.id.tvMessage)
val btnConfirm: Button = dialog.findViewById(R.id.btnConfirm)
val btnAnuleaza: Button = dialog.findViewById(R.id.btnAnuleaza)
val message: String? = "Doriti sa finalizati comanda pentru $grillName?"
tvMessage.text = message
btnConfirm.setOnClickListener {
Toast.makeText(this, "Ati comandat produsul", Toast.LENGTH_SHORT).show()
}
btnAnuleaza.setOnClickListener {
dialog.dismiss()
}
dialog.show()
}
}
New contributor
RaphPaladine is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.