- Create a new EditText Widget programmatically in Android using Kotlin
var dynamicEditext = EditText(this)
dynamicEditext.layoutParams = LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT)
dynamicEditext.hint="Dynamic EdiText..."
linearLayout.addView(dynamicEditext)
- Set hint to EditText.
dynamicEditext.hint="Dynamic EdiText..."
- Set inputType.
dynamicEditext.inputType=InputType.TYPE_CLASS_NUMBER
- Set imeOption.
dynamicEditext.imeOptions = EditorInfo.IME_ACTION_DONE
- Set isSingleLine.
dynamicEditext.isSingleLine=true
- TextWather on EditText on text change – Example Kotlin.
dynamicEditext.addTextChangedListener(object :TextWatcher{
override fun afterTextChanged(s: Editable?) {
//TODO("Not yet implemented")
}
override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {
//TODO("Not yet implemented")
}
override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
//Write your code here
}
})
- Floating Label in EditText using TextInputLayout – Example
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/linearLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<com.google.android.material.textfield.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="30dp">
<EditText
android:layout_width="match_parent"
android:layout_height="match_parent"
android:hint="Enter UserId"
android:inputType="text"
android:padding="20dp"
android:singleLine="true" />
</com.google.android.material.textfield.TextInputLayout>
<com.google.android.material.textfield.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="30dp">
<EditText
android:layout_width="match_parent"
android:layout_height="match_parent"
android:hint="Password"
android:padding="20dp" />
</com.google.android.material.textfield.TextInputLayout>