1

I am unable to retrieve the input text from a EditText in Android. Here's my code:

package com.example.fragments2

import ...
import com.example.fragments2.databinding.FragmentLoginPageBinding
class LoginPage : Fragment()
{

    lateinit var loginId:EditText
    lateinit var password:EditText
    lateinit var loginButton:Button

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View?

        {
            var binding:FragmentLoginPageBinding = DataBindingUtil.inflate(inflater,R.layout.fragment_login_page,container,false)
            var view  = binding.root
            loginId = view.findViewById(R.id.edittext_userid)
            password = view.findViewById(R.id.edittext_password)
            loginButton = view.findViewById(R.id.button_login)

            val text1: String = loginId.text.toString()
            val text2:String = password.text.toString()

        loginButton.setOnClickListener {
               Navigation.findNavController(it).navigate(LoginPageDirections.actionLoginPageToMainPage(text1,text2))
               Toast.makeText(context,text1,Toast.LENGTH_SHORT).show() //<-- shows empty string
           }

            return view
        }


}

The value of Text1, Text2 always remains "" . What's wrong in this code?

WinterSoldier
  • 393
  • 4
  • 15

2 Answers2

2

I think that click callback ins´t take the correct instance of yours TextViews. Try get these values inside click button:

class LoginPage : Fragment()
{

    lateinit var loginId:EditText
    lateinit var password:EditText
    lateinit var loginButton:Button

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View?

        {
            var binding:FragmentLoginPageBinding = DataBindingUtil.inflate(inflater,R.layout.fragment_login_page,container,false)
            var view  = binding.root
            loginId = view.findViewById(R.id.edittext_userid)
            password = view.findViewById(R.id.edittext_password)
            loginButton = view.findViewById(R.id.button_login)

            loginButton.setOnClickListener {
                val text1: String = loginId.text.toString()
                val text2: String = password.text.toString()
                Navigation.findNavController(it).navigate(LoginPageDirections.actionLoginPageToMainPage(text1,text2))
                Toast.makeText(context,text1,Toast.LENGTH_SHORT).show() //<-- shows empty string
           }

           return view
        }
}

Note: Maybe you must call the Toast.makeText with runOnUiMainThread(). It's been some time since I've done this.

this.runOnUiThread {
     Toast.makeText(context,text1,Toast.LENGTH_SHORT).show() //<-- shows empty string
}
Augusto
  • 3,825
  • 9
  • 45
  • 93
0

text1 and text2 always remain values you assign them in the beginning. If you want to get the text of the editText, you can directly request it loginId.text.toString().
Otherwise, you can create val text1: String = "" in the beginning of the class. But to update it, you need to do this inside of TextWatcher. Take a look at the link
The issue of your code is that onCreateView invokes only when Fragment initialized. But you need to update values every time your text changes.

Community
  • 1
  • 1
samaromku
  • 560
  • 2
  • 7