Search code examples
androidkotlinanko

how to set text, focus, error on editText in android with Kotlin


I searched heavily on internet about how can i use setFocusable(), setText(), setError() etc etc methods on editText(s) in android with Kotlin (I know the fact that we can use the above mentioned methods in java) but I was not able to find the exact solution that will work for me.

I am using 1.) Volley for http calls 2.) kotlin plugin for android studio with version = '1.1.3-2' 3.) anko library

Problems that i am facing when app is running : 1.)The setError() method is not getting called. 2.)i am not able to use setText() and setFocus() on editText.

please note that I need the solution(s) in Kotlin not Java.

Thanks in advance!

private fun askAppointment() {

    if (editTextPersonName?.text.isNullOrEmpty()) {
        editTextPersonName?.error ="Person Name cannot be empty."
        return
    } else if (editTextPersonMobile?.text.isNullOrEmpty()) {
        editTextPersonMobile?.error = "Person Mobile cannot be empty."
        return
    } else if (editTextPersonEmail?.text.isNullOrEmpty()) {
        editTextPersonEmail?.error = "Person Email cannot be empty."
        return
    } else if (editTextSubject?.text.isNullOrEmpty()) {
        editTextSubject?.error = "Subject cannot be empty."
        return
    } else if (editTextDescription?.text.isNullOrEmpty()) {
        editTextDescription?.error = "Description cannot be empty."
        return
    } else if (editTextAppointmentDate?.text.isNullOrEmpty()) {
        editTextAppointmentDate?.error = "Appointment Date cannot be empty."
        return
    } else if (editTextAppointmentTime?.text.isNullOrEmpty()) {
        editTextAppointmentTime?.error = "Appointment Time cannot be empty."
        return
    }

Solution

  • This is simple. Lets suppose etEmail is the EditText. You can set text like this

    etEmail?.setText("some text")
    

    And for error you can use this

    etEmail?.error = "This is error"
    

    And for set foucus you can try this one but i am not sure about it.

    etEmail?.isFocusable = false
    

    I hope this will help you.

    Check the working screenshot of above code.

    enter image description here

    use this logic in askAppointment()

    private fun askAppointment() {
    
        if (editTextPersonName?.text.isNullOrEmpty()) {
            editTextPersonName?.error = "Person Name cannot be empty."
            return
        } else if (editTextPersonMobile?.text.isNullOrEmpty()) {
            editTextPersonMobile?.error = "Person Mobile cannot be empty."
            return
        } else if (editTextPersonEmail?.text.isNullOrEmpty()) {
            editTextPersonEmail?.error = "Person Email cannot be empty."
            return
        } else if (editTextSubject?.text.isNullOrEmpty()) {
            editTextSubject?.error = "Subject cannot be empty."
            return
        } else if (editTextDescription?.text.isNullOrEmpty()) {
            editTextDescription?.error = "Description cannot be empty."
            return
        } else if (editTextAppointmentDate?.text.isNullOrEmpty()) {
            editTextAppointmentDate?.error = "Appointment Date cannot be empty."
            return
        } else if (editTextAppointmentTime?.text.isNullOrEmpty()) {
            editTextAppointmentTime?.error = "Appointment Time cannot be empty."
            return
        } else {
            //creating volley string request
            val stringRequest = object : StringRequest(Request.Method.POST, URL,
                    Response.Listener<String> { response ->
                        try {
                            val jsonObject = JSONObject(response)
                            val feedback = jsonObject.getString("response")
                            toast("$feedback")
                            //finish()       //finish Activity after sending request
                        } catch (e: JSONException) {
                            e.printStackTrace()
                        }
                    },
                    object : Response.ErrorListener {
                        override fun onErrorResponse(volleyError: VolleyError) {
                            toast("error :(")
                        }
                    }) {
                @Throws(AuthFailureError::class)
                override fun getParams(): Map<String, String> {
                    val params = HashMap<String, String>()
                    params.put("personName", editTextPersonName?.text.toString())
                    params.put("personMobile", editTextPersonMobile?.text.toString())
                    params.put("personEmail", editTextPersonEmail?.text.toString())
                    params.put("subject", editTextSubject?.text.toString())
                    params.put("description", editTextDescription?.text.toString())
                    params.put("appointMentDate", editTextAppointmentDate?.text.toString())
                    params.put("appointMentTime", editTextAppointmentTime?.text.toString())
                    return params
                }
            }
    
            //adding request to queue
            AppController.instance?.addToRequestQueue(stringRequest)
        }
    }