'Android Kotlin: onTextChanged not being triggered for EditText
I am creating an EditText
box with line numbers on the side. To achieve this, I have code in the onTextChanged
method of the addTextChangedListener
that updates the line numbers for the EditText
. For some reason however, the onTextChanged
event never seems to fire when I type in the EditText
, as the println
statement is not being activated.
Below is my XML code:
<GridLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:rowCount="1"
android:columnCount="2">
<TextView
android:id="@+id/linenumber"
android:layout_width="30dp"
android:layout_height="200dp"
android:layout_row="0"
android:layout_column="0"
android:layout_columnSpan="1"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_alignParentBottom="true"
android:background="@drawable/border_style"
android:gravity="center_horizontal"
android:text="1"
android:textSize="18dp"></TextView>
<EditText
android:id="@+id/textbox"
android:layout_width="0dp"
android:layout_height="200dp"
android:layout_row="0"
android:layout_column="1"
android:layout_columnSpan="1"
android:layout_columnWeight="1"
android:ems="5"
android:gravity="left|top"
android:inputType="textMultiLine"
android:padding="10dp"
android:text="" />
</GridLayout>
Below is my Kotlin code:
var textbox1: EditText = findViewById(R.id.textbox);
var lineNumber: TextView = findViewById(R.id.linenumber);
// Add the line numbers or remove the line numbers based on the user's input
textbox1.addTextChangedListener(object : TextWatcher {
override fun afterTextChanged(p0: Editable?) {
}
override fun beforeTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {
}
override fun onTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {
// Retrieve the number of lines in the user's input
var lines:Int = lineNumber.getLineCount()
var linesText:String = "";
// Loop through the lines
for (i in 1..lines) {
linesText = linesText + "\n" + i;
}
println("linesText = " + linesText)
// Update the line numbers
lineNumber.setText(linesText);
}
})
Solution 1:[1]
I figured out the bug and I apologize for not including the line of code in my post where the bug was:
I had the following extra line of code that reset the listener after I initialized the listener:
setContentView(R.layout.activity_main)
Solution 2:[2]
The only problem I can spot is:
var lines:Int = lineNumber.getLineCount()
Shouldn't this be:
var lines : Int = textbox1.getLineCount()
Also your listener in this instance should be added in onCreate() or onResume().
Solution 3:[3]
In case somebody lands here, android:textAllCaps="true"
in EditText prevented for some reason onTextChanged from being called in my project.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Adam Lee |
Solution 2 | Scott Evans |
Solution 3 | Amir Golan |