0

I'm trying to reset an EditText after form submission. EditText is inside a TextInputLayout.

Xml snippet of EditText:

<android.support.design.widget.TextInputLayout
                android:id="@+id/til_email"
                android:layout_width="match_parent"
                android:layout_height="wrap_content">

                <EditText
                    android:id="@+id/et_email"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:hint="Email ID *"
                    android:inputType="textEmailAddress"
                    android:textSize="10sp" />

</android.support.design.widget.TextInputLayout>

Kotlin snippet:

private lateinit var etEmail: EditText
private lateinit var tilEmail: TextInputLayout

override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_online_academy)

        etEmail = findViewById(R.id.et_email)
        tilEmail = findViewById(R.id.til_email)

    }

I've tried different methods to clear EditText but nothing works.

Method 1:

etEmail.text.clear()

Method 2:

etEmail.text = null

Method 3:

etEmail.setText("", TextView.BufferType.EDITABLE)

Method 4:

val blank = "\u0020"
etEmail.setText(blank)

Method 5:

textInputLayoutEmail.editText.text.clear()

Method 6:

etEmail.post { etEmail.text.clear() }

I'm not getting any error or warning in logcat. Form submission also works fine. Thank you.

Zayid Mohammed
  • 2,275
  • 2
  • 10
  • 17

2 Answers2

0

First of all make sure that you have the below line in imports of your class activity:

import kotlinx.android.synthetic.main.activity_online_academy.*

In Kotlin you don't need to use findViewById.
You can use et_email and til_email anywhere in your activity class code.
So just do et_email.setText("")

forpas
  • 160,666
  • 10
  • 38
  • 76
0

There is some debugging related issue to find out. Maybe some strange behavior due to your some other part of the code which is interfering or maybe just glitches on the device.

There are a few things that you can try

  1. First of all Just remove clearing code. And instead of clearing the edittextView just put some string to see if you are able to set string through your code. If you are able to set any string like et_email.setText("some random string") then just replace this line with this code et_email.setText("")
  2. If it still doesn't work (you are able to set string but not able to set "" string) Then it is not just strange, It's the weird thing. You should definitely try your code in some other device or just restart your android phone or emulator.
  3. If you are testing in Emulator, I highly recommend Testing this in the Actual device or on New Emulator or just simply uninstalling the app and re-installing it. Cause strange and unusual behaviors (if not due to Coding or logical Errors) Are most of the time simply due to Unknown Software Issues or due to Computer infections.
  4. Last but not the least try to clean build or Invalidate cache and restart android studio and recompile whole code
Abdul Rehman
  • 2,224
  • 25
  • 30
  • 1. I tried this already, i' not able to clear or set any text. setting hint also not working 2 & 3 . I'm not using Emulator, testing in 2 phone, both is behaving same. 4. Already tried, no change in this strange behaviour. – Zayid Mohammed Nov 22 '18 at 13:46
  • @ZayidMohammed try in some new activity or some new xml file. Figure out does it do for the specific activity only behavior or is it something else. – Abdul Rehman Nov 22 '18 at 15:13