Home > Blockchain >  How can I change the background color of an EditText when text is entered using Kotlin in Android St
How can I change the background color of an EditText when text is entered using Kotlin in Android St

Time:11-16

Just to be clear, I want to change the color of the textField to be clear once text is entered and for it to go back to its old color, which in my case is gray when it has no text. I looked through stackoverflow and found some answers, however it was in Java and I am still a beginner with Kotlin, but I am writing this in Kotlin for my work.

Essentially, I want to know how can you change the background of the textField once text is entered?

enter image description here

I want to have the background color of that to change once text is entered using Kotlin.

This is my XML EditText that I am referring to:

  <EditText
    android:id="@ id/description"
    android:layout_width="350dp"
    android:layout_height="200dp"
    android:layout_below="@id/addImage"
    android:layout_alignBottom="@id/addImage"
    android:layout_marginStart="50dp"
    android:layout_marginTop="125dp"
    android:layout_marginEnd="50dp"
    android:layout_marginBottom="75dp"
    android:background="@color/lightGray"
    android:gravity="center"
    android:hint="@string/description"
    android:inputType="text"
    app:layout_constraintBottom_toTopOf="@ id/bottomNavigationView"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

Where do I go from here now?

class PostActivity : AppCompatActivity() {
    private lateinit var button: Button
    private lateinit var imageView: ImageView
    private lateinit var editText: EditText


override fun onCreate(savedInstanceState: Bundle?) {

    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_post)

    editText = findViewById(R.id.description)

}
}

This is just what I have on my activity. I don't have any method really on how to do this.

CodePudding user response:

Add a TextWatcher to react when text has been changed.

val originalColor = (editText.background as ColorDrawable).color // or Color.LIGHT_GRAY
val haveTextColor = Color.TRANSPARENT // or whatever you want
editText.addTextChangedListener(object: TextWatcher {
        override fun beforeTextChanged(s: CharSequence, start: Int, count: Int, after: Int) { }
        override fun onTextChanged(s: CharSequence, start: Int, before: Int, count: Int) { }

        override fun afterTextChanged(s: Editable) {
            editText.setBackgroundColor(if (s.isEmpty()) originalColor else haveTextColor)
        }

    })

CodePudding user response:

You can use the doAfterTextChanged Kotlin extension function here. It can be used to perform some action every time the text changes.

editText.doAfterTextChanged { text ->
    editText.setBackgroundColor(if (text.isEmpty()) color1 else color2) // Choose whatever colors you want
}
  • Related