I have a simple fragment that has EditText, Button, TextView . A simple string will be taken from the user and the TextView will be updated when the Button is clicked. But it is not working for me, my TextView disappears somehow. But if I don't take the string from the user and pre-define it, the TextView does change.
My code,
class First_Fragment : Fragment() {
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_first_, container, false)
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
val text=editTextNumber.text.toString() //user input
button.setOnClickListener {
textView.text=text
}
}
}
By capturing editTextNumber.text.toString()
during onViewCreated
, you are retrieving the text in the EditText
in onViewCreated
, which is empty.
So you should be retrieving the value when the button is clicked as you mentioned.