2

I have an EditText component for entering the text that will be sent via a POST request to a backend server. As it takes time to process the POST request and wait for a response, I want to disable the EditText input until the request is completed (and if successful, it then clears the text) for UX purposes.

I'm currently using

val smsMessageInput = findViewById<EditText>(R.id.smsMessageInput)
smsMessageInput.isEnabled = false

However this causes the keyboard to disappear, which isn't what I want to occur. How do I disable the input so the user can't enter text without hiding the keyboard?

I've also tried changing the text colour before the API call runs, however, this doesn't seem to work until AFTER the API call has completed. Here's the code:

smsMessageInput.setTextColor(ContextCompat.getColor(this, R.color.colorLowEmphasis))

Ideally, I would like to change the text colour of the EditText to a low-emphasis colour (the typical light-grey disabled colour that is widely used for disabled inputs on websites) and prevent the user from entering text in the EditText component.

Here's the full code I've tried without success:

    fun sendSms(smsToInput: EditText, smsMessageInput: EditText) {
        smsMessageInput.setTextColor(ContextCompat.getColor(this, R.color.colorLowEmphasis_Dark))
        smsMessageInput.isEnabled = false

        val formBody = FormBody.Builder()
            .add("to", smsToInput.text.toString())
            .add("message", smsMessageInput.text.toString())
            .build()
        val request = Request.Builder()
            .url("https://MYURL.com/POST_ROUTE")
            .post(formBody)
            .build()

        client.newCall(request).execute().use { response ->
            if (!response.isSuccessful) throw IOException("Unexpected code $response")
            val body = response.body?.string()
            smsMessageInput.isEnabled = true
            smsMessageInput.text.clear()
            println(body)
        }
    }
ॐ Rakesh Kumar
  • 1,318
  • 1
  • 14
  • 24
Jake
  • 3,865
  • 5
  • 25
  • 58
  • can use `setEnabled` and `setFocusable` to false will work for you – ॐ Rakesh Kumar Aug 07 '19 at 04:44
  • I replaced `smsMessageInput.setTextColor(...)` with `smsMessageInput.isFocusable = false` and added `smsMessageInput.isFocusable = true` below `smsMessageInput.isEnabled = true` however 1) isFocusable isn't enabling again. I can't focus on the input, and 2) the text color isn't changed to the disabled grey color. – Jake Aug 07 '19 at 04:53
  • try this link and will work definitely, https://stackoverflow.com/questions/5879250/how-to-disable-edittext-in-android – ॐ Rakesh Kumar Aug 07 '19 at 04:58
  • OK that partially helped. I was able to re-enable the focus again by using `smsMessageInput.isFocusableInTouchMode = true` instead of `smsMessageInput.isFocusable = true`. The issue I still have though is that it's not disabling and unfocusing the input until AFTER `client.newCall(...)` runs. There's just a big lag on the screen (it basically freezes) and nothing happens until the text is cleared. The user doesn't see that the input has been disabled at all. – Jake Aug 07 '19 at 05:05

1 Answers1

1

I am using isFocusable instead of isEnabled for a similar purpose.

smsMessageInput.isFocusable = false

But anyway to show soft keyboard:

val inputMethodManager = getSystemService(Activity.INPUT_METHOD_SERVICE) as InputMethodManager
inputMethodManager.toggleSoftInput(InputMethodManager.SHOW_FORCED, InputMethodManager.HIDE_IMPLICIT_ONLY)

And you can requestFocus() in time you want.

ust
  • 161
  • 8