1

I got null data when try to get data from edittext there is my code

class LoginActivity : AppCompatActivity() {
lateinit var etUsername : EditText
lateinit var etPassword : EditText
var username = ""
var password = ""
override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_login)
    etPassword = findViewById(R.id.tanggalLahir) as EditText
    etUsername = findViewById(R.id.nomerInduk) as EditText
    username = etUsername.text.toString()
    password = etPassword.text.toString()

    buttonLogin.setOnClickListener() {
        if (username.isEmpty()){
            Log.d("User", "Username = $username")
            nomerInduk.error = "Nomor Induk Kosong!"
            return@setOnClickListener
        }

        if (password.isEmpty()){
            Log.d("User", "Password = $password")
            tanggalLahir.error = "Tanggal Lahir Kosong!"
            return@setOnClickListener
        }

        login(username,password)
    }


}

and there is when i check in log image check log

i already change edittext to textinputedittext but still same

Axifive
  • 1,159
  • 2
  • 19
  • 31
Thoriqul Umar
  • 35
  • 1
  • 1
  • 6

2 Answers2

0
class LoginActivity : AppCompatActivity() {
lateinit var etUsername : EditText
lateinit var etPassword : EditText
var username = ""
var password = ""
override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_login)
    etPassword = findViewById(R.id.tanggalLahir) as EditText
    etUsername = findViewById(R.id.nomerInduk) as EditText


    buttonLogin.setOnClickListener() {
username = etUsername.text.toString()
    password = etPassword.text.toString()
        if (username.isEmpty()){
            Log.d("User", "Username = $username")
            nomerInduk.error = "Nomor Induk Kosong!"
            return@setOnClickListener
        }

        if (password.isEmpty()){
            Log.d("User", "Password = $password")
            tanggalLahir.error = "Tanggal Lahir Kosong!"
            return@setOnClickListener
        }

        login(username,password)
    }


}
MMG
  • 3,226
  • 5
  • 16
  • 43
0
class LoginActivity : AppCompatActivity() {
lateinit var etUsername : EditText
lateinit var etPassword : EditText
var username = ""
var password = ""
override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_login)
    etPassword = findViewById(R.id.tanggalLahir) as EditText
    etUsername = findViewById(R.id.nomerInduk) as EditText


    buttonLogin.setOnClickListener() {
    // once button click you have to get the current text value from the edit text what you have entered. 
    username = etUsername.text.toString()
    password = etPassword.text.toString()
        if (username.isEmpty()){
            Log.d("User", "Username = $username")
            nomerInduk.error = "Nomor Induk Kosong!"
            return@setOnClickListener
        }

        if (password.isEmpty()){
            Log.d("User", "Password = $password")
            tanggalLahir.error = "Tanggal Lahir Kosong!"
            return@setOnClickListener
        }

        login(username,password)
    }


}

you have to study about basic android lifecycle. and there you first time get the edit text value and assigned to the global variable username and password. but after you enter some and click enter you have to get that newly added text values when submit button click. that why those assigning codes comes inside to the button click.