I cannot seem to figure out how to take the value entered into a textedit, convert to srting/int and then multiply them together. I have 3 inputs i want multiplied with a button click and then displayed in the text of a text view. as a exercise to see if my button works and changing the text view was working, i used append and it works, I just cant get them to multiply.
looking over some of these threads I still cannot seem to make it work:
Android - How to get text from edittext and calculate them?
Android - How to get text from edittext and calculate them?
Convert EditText input to integer and multiply?
import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import kotlinx.android.synthetic.main.MultiplyExample.*
class MultiplyExample : Fragment() {
override fun onCreateView( inflater: LayoutInflater, container:
ViewGroup?, savedInstanceState: Bundle? ): View? {
return inflater.inflate(R.layout.fragment_retention, container, false) }
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
button.setOnClickListener(object : View.OnClickListener {
override fun onClick(v: View) {
val a: Int = length.text.toString().toInt()
val b: Int = flight.text.toString().toInt()
val c: Int = rotation.text.toString().toInt()
val sb = StringBuilder()
sb.append(a).append(b).append(c)
d.text = sb
<EditText
android:inputType="number"
Your issue is that you are trying to reference the widgets in the fragments without findViewById
.
In your method onCreateView
put some code like so before the return statement.
val view = inflater.inflate(R.layout.fragment_retention, container, false)
val a: Int = view.findViewById<TextView>(R.id.length).text.toString().toInt()
val b: Int = view.findViewById<TextView>(R.id.flight).text.toString().toInt()
val c: Int = view.findViewById<TextView>(R.id.rotation).text.toString().toInt()
val multiplication = a * b * c
return view