I am trying to change the EditText border if the if else statement is successful. I have 4 similar EditText. However 1 of it, the second one acts a bit weird where its size will expand after the successful verification. All the sizes, layout everything is same (i just copy and paste), but why the second EditText is acting weirdly?
The question is how to make sure the size of the EditText is consistent and same as the other EditText.
Before Input
Successful input
the code would be as below
button.setOnClickListener {
val brnText = brn_TextBox.text.toString().trim()
val tinText = tin_TextBox.text.toString().trim()
if (isValidBrn(brnText)) {
brn_TextBox.setBackgroundResource(R.drawable.edittext_green_border)
brn_TextBox.isEnabled = false
showDrawable(brn_TextBox)
} else {
brn_TextBox.setBackgroundResource(R.drawable.edittext_red_border)
brn_TextBox.setOnClickListener{
brn_TextBox.setBackgroundResource(R.drawable.edittext_border)
}
brn_TextBox.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) {
brn_TextBox.setBackgroundResource(R.drawable.edittext_border)
}
override fun afterTextChanged(s: Editable?) {}
})
}
if (isValidTin(tinText)) {
tin_TextBox.setBackgroundResource(R.drawable.edittext_green_border)
tin_TextBox.isEnabled = false
showDrawable(tin_TextBox)
} else {
tin_TextBox.setBackgroundResource(R.drawable.edittext_red_border)
tin_TextBox.setOnClickListener{
tin_TextBox.setBackgroundResource(R.drawable.edittext_border)
}
tin_TextBox.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) {
tin_TextBox.setBackgroundResource(R.drawable.edittext_border)
}
override fun afterTextChanged(s: Editable?) {}
})
}
if(isValidTin(tinText) && isValidBrn(brnText)){
button.isEnabled = false
button.visibility = View.GONE
}
}
//Bottom part basically is the same thing just different name
button2.setOnClickListener {
val msicText = msic_TextBox.text.toString().trim()
val sstText = sst_TextBox.text.toString().trim()
if (isValidMsic(msicText)) {
msic_TextBox.setBackgroundResource(R.drawable.edittext_green_border)
msic_TextBox.isEnabled = false
showDrawable(msic_TextBox)
} else {
msic_TextBox.setBackgroundResource(R.drawable.edittext_red_border)
msic_TextBox.setOnClickListener{
msic_TextBox.setBackgroundResource(R.drawable.edittext_border)
}
msic_TextBox.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) {
tin_TextBox.setBackgroundResource(R.drawable.edittext_border)
}
override fun afterTextChanged(s: Editable?) {}
})
}
if (isValidSst(sstText)) {
sst_TextBox.setBackgroundResource(R.drawable.edittext_green_border)
sst_TextBox.isEnabled = false
showDrawable(sst_TextBox)
} else {
sst_TextBox.setBackgroundResource(R.drawable.edittext_red_border)
sst_TextBox.setOnClickListener{
sst_TextBox.setBackgroundResource(R.drawable.edittext_border)
}
sst_TextBox.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) {
tin_TextBox.setBackgroundResource(R.drawable.edittext_border)
}
override fun afterTextChanged(s: Editable?) {}
})
}
if(isValidMsic(msicText) && isValidSst(sstText)){
button2.isEnabled = false
button2.visibility = View.GONE
}
}
To show the tick icon after successful
private fun showDrawable(editText: EditText) {
val drawable = ContextCompat.getDrawable(this, R.drawable.tick_icon_svg)
drawable?.setBounds(0, 0, drawable.intrinsicWidth, drawable.intrinsicHeight)
editText.setCompoundDrawables(drawable, null, null, null)
}
xml file
brn_EditText (EditText1)
<EditText
android:id="@+id/brn_TextBox"
android:layout_width="match_parent"
android:textColor="@color/edittext_text_color"
android:layout_height="wrap_content"
android:layout_marginStart="@dimen/_32sdp"
android:layout_marginEnd="@dimen/_32sdp"
android:layout_marginBottom="@dimen/_31sdp"
android:background="@drawable/edittext_border"
android:drawableStart="@drawable/tick_icon_svg"
android:drawableTint="@color/colorGreen"
android:ems="10"
android:gravity="center"
android:hint="@string/tin_eg"
android:importantForAutofill="no"
android:inputType="number"
android:maxLength="12"
android:padding="@dimen/_14sdp"
android:textCursorDrawable="@drawable/cursor_textinput_edit_text" />
tin_EditText (EditText2)
<EditText
android:id="@+id/tin_TextBox"
android:textColor="@color/edittext_text_color"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="@dimen/_32sdp"
android:layout_marginEnd="@dimen/_32sdp"
android:layout_marginBottom="@dimen/_31sdp"
android:background="@drawable/edittext_border"
android:drawableStart="@drawable/tick_icon_svg"
android:drawableTint="@color/colorGreen"
android:ems="10"
android:gravity="center"
android:hint="@string/tin_eg"
android:importantForAutofill="no"
android:maxLength="15"
android:maxLines="1"
android:padding="@dimen/_14sdp"
android:textCursorDrawable="@drawable/cursor_textinput_edit_text" />
msic_EditText (EditText3)
<EditText
android:id="@+id/msic_TextBox"
android:textColor="@color/edittext_text_color"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="@dimen/_32sdp"
android:layout_marginEnd="@dimen/_32sdp"
android:layout_marginBottom="@dimen/_31sdp"
android:background="@drawable/edittext_border"
android:drawableStart="@drawable/tick_icon_svg"
android:drawableTint="@color/colorGreen"
android:ems="10"
android:maxLength="5"
android:gravity="center"
android:hint="@string/msic_eg"
android:inputType="numberSigned"
android:padding="@dimen/_14sdp"
android:textCursorDrawable="@drawable/cursor_textinput_edit_text" />
sst_editText (EditText4)
<EditText
android:id="@+id/sst_TextBox"
android:textColor="@color/edittext_text_color"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="@dimen/_32sdp"
android:layout_marginEnd="@dimen/_32sdp"
android:layout_marginBottom="@dimen/_31sdp"
android:background="@drawable/edittext_border"
android:drawableStart="@drawable/tick_icon_svg"
android:drawableTint="@color/colorGreen"
android:ems="10"
android:maxLength="15"
android:gravity="center"
android:hint="@string/sst_eg"
android:importantForAutofill="no"
android:inputType="number"
android:padding="@dimen/_14sdp"
android:textCursorDrawable="@drawable/cursor_textinput_edit_text" />