I'm new to kotlin and i was trying to make a simple plus minus controller, so the layout has two buttons, Plus and Minus and an EditText both are inside a fragment, the issue is that when i'm trying to press one of the buttons the app crash on .setText with error Resuource not found.
So the question is what is the right way to set text in an EditText inside a fragment?
Here is my code:
FirstFragment.tk
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
view.findViewById<ImageButton>(R.id.btnPlus).setOnClickListener {
changeQta(it)
}
view.findViewById<ImageButton>(R.id.btnMinus).setOnClickListener {
changeQta(it)
}
}
private fun changeQta(v: View) {
val txtQta = view?.findViewById<EditText>(R.id.txtQta)
val curVal = txtQta?.text.toString().toInt()
when (v.id) {
R.id.btnMinus -> {
if (curVal > 1) {
txtQta?.setText(curVal - 1)
}
if (txtQta?.text.toString().toInt() == 1) {
v.isEnabled = false
}
}
R.id.btnPlus -> {
txtQta?.setText(curVal + 1)
}
else -> return;
}
}