How do you build a lambda expression for the EditText addTextChangeListener in Kotlin? Below gives an error:
passwordEditText.addTextChangedListener { charSequence ->
try {
password = charSequence.toString()
} catch (error: Throwable) {
raise(error)
}
}
addTextChangedListener() takes a TextWatcher which is an interface with 3 methods. What you wrote would only work if TextWatcher had only 1 method. I'm going to guess the error you're getting relates to your lambda not implementing the other 2 methods. You have 2 options going forward.
Ditch the lambda and just use an anonymous inner class
editText.addTextChangedListener(object : TextWatcher {
override fun afterTextChanged(s: Editable?) {
}
override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {
}
override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
}
})
Create an extension method so you can use a lambda expression:
fun EditText.afterTextChanged(afterTextChanged: (String) -> Unit) {
this.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(editable: Editable?) {
afterTextChanged.invoke(editable.toString())
}
})
}
And then use the extension like so:
editText.afterTextChanged { doSomethingWithText(it) }
Add this core ktx dependence
implementation 'androidx.core:core-ktx:1.0.0'
You simply have to do
passwordEditText.doAfterTextChanged{ }
A bit old, but using Kotlin Android extensions you can do something like that:
editTextRequest.textChangedListener {
afterTextChanged {
// Do something here...
}
}
No extra code needed, just add:
implementation 'androidx.core:core-ktx:1.0.0'
Sorry for being late!
If you add implementation 'androidx.core:core-ktx:1.1.0' to your module's build.gradle file then you can use
etPlayer1.doOnTextChanged { text, start, count, after -> // Do stuff }
Test it :
passwordEditText.addTextChangedListener(object:TextWatcher{
override fun afterTextChanged(s: Editable?) { }
override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) { }
override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) { }
})
hope this Kotlin sample help making it clear:
class MainFragment : Fragment() {
private lateinit var viewModel: MainViewModel
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?): View {
val view = inflater.inflate(R.layout.main_fragment, container, false)
view.user.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) {
userLayout.error =
if (s.length > userLayout.counterMaxLength) {
"Max character length is: ${userLayout.counterMaxLength}"
} else null
}
})
return view
}
override fun onActivityCreated(savedInstanceState: Bundle?) {
super.onActivityCreated(savedInstanceState)
viewModel = ViewModelProviders.of(this).get(MainViewModel::class.java)
// TODO: Use the ViewModel
}
}
With this XML layout:
<android.support.design.widget.TextInputLayout
android:id="#+id/userLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:counterMaxLength="5"
app:counterEnabled="true"
android:hint="user_name">
<android.support.design.widget.TextInputEditText
android:id="#+id/user"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</android.support.design.widget.TextInputLayout>
And this Gradle:
android {
compileSdkVersion 'android-P'
...
}
api 'com.android.support:design:28.0.0-alpha1'
implementation 'com.android.support:appcompat-v7:28.0.0-alpha1' // appcompat library
In case you're using Material Filled text field or Outlined text field, attempt to respond to input text change as mentioned by documentation, respectively:
filledTextField.editText?.doOnTextChanged { inputText, _, _, _ ->
// Respond to input text change
}
and
outlinedTextField.editText?.doOnTextChanged { inputText, _, _, _ ->
// Respond to input text change
}
if you use implementation 'androidx.core:core-ktx:1.1.0-alpha05' you can use
For android.widget.TextView
TextWatcher
TextView.doBeforeTextChanged(crossinline action: (text: CharSequence?, start: Int, count: Int, after: Int) -> Unit)
Add an action which will be invoked before the text changed.
TextWatcher
TextView.doOnTextChanged(crossinline action: (text: CharSequence?, start: Int, count: Int, after: Int) -> Unit)
Add an action which will be invoked when the text is changing.
TextWatcher
TextView.doAfterTextChanged(crossinline action: (text: Editable?) -> Unit)
https://developer.android.com/reference/kotlin/androidx/core/widget/package-summary#extension-functions
Add the core ktx dependency
implementation 'androidx.core:core-ktx:1.3.0'
And you can simply implement like this
edit_text.addTextChangedListener { it: Editable? ->
// Do your stuff here
}
Another alternative is the KAndroid library -
implementation 'com.pawegio.kandroid:kandroid:0.8.7#aar'
Then you could do something like this...
editText.textWatcher { afterTextChanged { doSomething() } }
Obviously it is excessive to use an entire library to solve your problem, but it also comes with a range of other useful extensions that eliminate boilerplate code in the Android SDK.
You can make use of kotlin's named parameters:
private val beforeTextChangedStub: (CharSequence, Int, Int, Int) -> Unit = { _, _, _, _ -> }
private val onTextChangedStub: (CharSequence, Int, Int, Int) -> Unit = { _, _, _, _ -> }
private val afterTextChangedStub: (Editable) -> Unit = {}
fun EditText.addChangedListener(
beforeTextChanged: (CharSequence, Int, Int, Int) -> Unit = beforeTextChangedStub,
onTextChanged: (CharSequence, Int, Int, Int) -> Unit = onTextChangedStub,
afterTextChanged: (Editable) -> Unit = afterTextChangedStub
) = addTextChangedListener(object : TextWatcher {
override fun beforeTextChanged(charSequence: CharSequence, i: Int, i1: Int, i2: Int) {
beforeTextChanged(charSequence, i, i1, i2)
}
override fun onTextChanged(charSequence: CharSequence, i: Int, i1: Int, i2: Int) {
onTextChanged(charSequence, i, i1, i2)
}
override fun afterTextChanged(editable: Editable) {
afterTextChanged(editable)
}
})
This is the lambda function with edit text field with TextWatcher
searchField.addTextChangedListener(
afterTextChanged = {
},
onTextChanged = {s, start, before, count->
TODO("DO your code")
},
beforeTextChanged = {s, start, before, count->
TODO("DO your code")
}
)
This looks neat:
passwordEditText.setOnEditorActionListener {
textView, keyCode, keyEvent ->
val DONE = 6
if (keyCode == DONE) {
// your code here
}
false
}
Related
I have been working on a stock keeping application as a demo to learn kotlin and android studio, I have added textchangedlistener in a recycler view item, which everytime it text is changed, gets information from Api and displays new item on textview. This works good for only one time, After that it keeps infinitely changing. please check video for better understanding : ScreenRecording .
Here is my rcv adapter :
class RecyclerViewAdapterU (val dataList:ArrayList<ModelClass>): RecyclerView.Adapter<RecyclerViewAdapterU.ViewHolder>() {
var _binding: UploadItemViewBinding? = null
val binding get() = _binding!!
override fun onCreateViewHolder(
parent: ViewGroup,
viewType: Int
): RecyclerViewAdapterU.ViewHolder {
val v =
LayoutInflater.from(parent.context).inflate(R.layout.upload_item_view, parent, false)
_binding = UploadItemViewBinding.bind(v)
return ViewHolder(binding.root)
}
override fun onBindViewHolder(holder: ViewHolder, #SuppressLint("RecyclerView") position: Int) {
bindItems(dataList[position])
holder.getStock()
holder.updateStockDetail()
}
fun bindItems(data: ModelClass) {
binding.apply {
itemquant.text=data.item_quant
uploadItemName.text = data.item_name
uploadMfg.text = data.mfg
skuStock.setText(data.item_stock.toString())
skuCode.setText(data.sku_code)
}
}
fun getUpdatedDetails(skucode:String,pos:Int){
val call: Call<List<ModelClass>>? =
ApiClient.instance?.myApi?.getfromsku(skucode)!!
call!!.enqueue(object : Callback<List<ModelClass>?> {
override fun onResponse(
call: Call<List<ModelClass>?>,
response: Response<List<ModelClass>?>
) {
val skuDetails=response.body()
if (skuDetails != null) {
dataList.removeAt(pos)
for (i in skuDetails){
println(i.item_name)
dataList.add(pos,i)
}
notifyItemChanged(pos)
}
}
override fun onFailure(call: Call<List<ModelClass>?>, t: Throwable) {
}
})
}
override fun getItemCount(): Int {
return dataList.size
}
inner class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
fun getStock() {
binding.skuStock.addTextChangedListener(object : TextWatcher {
override fun beforeTextChanged(charSequence: CharSequence, i: Int, i1: Int, i2: Int) {}
override fun onTextChanged(charSequence: CharSequence, i: Int, i1: Int, i2: Int) {}
override fun afterTextChanged(editable: Editable) {
for (i in 0 until RecyclerViewAdapter.ob.dataSelected.size){
if (editable.toString().trim()!=""){
var x= editable.toString().trim().toInt()
RecyclerViewAdapter.ob.dataSelected[adapterPosition].item_stock=x
//getting current itemstock before pushing update.
//assigning latest itemstock to the data for the update
}
}
}
})
}
fun updateStockDetail(){
binding.skuCode.addTextChangedListener(object : TextWatcher{
override fun beforeTextChanged(charSequence: CharSequence, i: Int, i1: Int, i2: Int) {}
override fun onTextChanged(charSequence: CharSequence, i: Int, i1: Int, i2: Int) {
}
override fun afterTextChanged(editable: Editable) {
var x:String=""
var pos:Int=adapterPosition
for (i in 0 until RecyclerViewAdapter.ob.dataSelected.size){
if (editable.toString().trim()!=""){
x=editable.toString().trim()
//RecyclerViewAdapter.ob.dataSelected[adapterPosition].sku_code=x
println("$x in if")
}
}
//println(RecyclerViewAdapter.ob.dataSelected[adapterPosition].sku_code)
//getting edited text and calling the function to get updated details.
getUpdatedDetails(x,pos)
binding.skuStock.removeTextChangedListener(this)
}
})
}
}
}
If possible please review my code and let me know what are things i need to work on.
Note: ob.dataselected is a global variable from another recyclerview adapter.
Textchangedlistener i am talking about is in the fun updateStockDetail()
In the documentation for afterTextChanged we see the following:
This method is called to notify you that, somewhere within s, the text has been changed. It is legitimate to make further changes to s from this callback, but be careful not to get yourself into an infinite loop, because any changes you make will cause this method to be called again recursively.
(The emphasis is mine.) I don't see where you remove the text watcher before changing the text, so you may be in an infinite loop.
To remove the text watcher use removeTextChangedListener. You can replace the text watcher after making changes.
I have a recyclerview where each view contains a TextView and an EditText. The part where I am stuck is when I start typing in any of the row's EditText box, it automatically updates another row further down the list. I know this is occurring because of the whole recycling aspect of recyclerview and the same view is re-used for the rows further down. I just don't know how to implement this correctly.
class TodoAdapter(private val tasks : List<TodoItem>) : RecyclerView.Adapter<TodoAdapter.ViewHolder>() {
inner class ViewHolder(private val view: View) : RecyclerView.ViewHolder(view) {
var editText = view.findViewById<EditText>(R.id.edit_text)
init {
editText.addTextChangedListener(object : TextWatcher{
override fun afterTextChanged(s: Editable?) {
tasks[adapterPosition].task = s.toString()
}
override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {
}
override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
}
})
}
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
val view = LayoutInflater.from(parent.context).inflate(R.layout.todo_item,parent,false)
return ViewHolder(view)
}
override fun getItemCount() = tasks.size
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
val item = tasks[position]
holder.editText.setText(item.task)
}
}
class TodoItem(var task: String = "")
editText.addTextChangedListener(object TextWatcher() {
override fun afterTextChanged(s : Editable) {
item.textFieldValue = s.toString()
}
});
Put textChangeListener inside init block this should work
#MuhammadAhmed
Data class
class todolist(var hours: String, var task: String)
Adapter
override fun onBindViewHolder(holder: TodoAdapter.TodoViewHolder, position: Int) {
val currentItem = todo[position]
holder.todo.text = currentItem.hours
holder.task.text = currentItem.task
holder.task.addTextChangedListener(object: TextWatcher{
override fun afterTextChanged(s: Editable?) {
currentItem.task = s.toString()
}
override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {}
override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {}
})
}
I'm currently learning android in Kotlin, and I'm trying to create a tip calculator. I have two EditText views, one for the bill amount and tip amount. I also have two TextViews, one for the tip amount and one for the total + tip.
This is part of my code, where I am trying to have the EditText automatically update the TextViews upon user input.
billInput.addTextChangedListener(object: TextWatcher) {
override fun afterTextChanged(s: Editable) {
totalAmt.text = billInput + tipInput
}
override fun beforeTextChanged(s: CharSequence, start: Int, count: Int, after: Int) {
}
override fun onTextChanged(s: CharSequence, start: Int, before: Int, count: Int) {
}
}
For now, I am trying to have the bill + tip amount be added and update the TextView. I got the error "Expecting a class body" next to the first line. Am I using addTextChangedListener incorrectly or is something wrong with my code? I know there are probably more efficient ways to do this, but I was assuming I could just use addTextChangedListener for both EditText views. So, to clear things up: I want to input my bill amount and tip amount, and automatically update "Tip amount:" and "Total:" .
Edit*** : Would it make sense to do the numeric calculations (tip and total amount) under onTextChanged? That way as the input is received, the calculations are processed and afterTextChanged is responsible for displaying the final results?
You are adding EditText + EditText, should add values instead
billInput.addTextChangedListener(object: TextWatcher) {
override fun afterTextChanged(s: Editable) {
totalAmt.text = "${billInput.text.toString().toInt() + tipInput.text.toString().toInt()}"
}
override fun beforeTextChanged(s: CharSequence, start: Int, count: Int, after: Int) {
}
override fun onTextChanged(s: CharSequence, start: Int, before: Int, count: Int) {
}
}
Hello my friend you can set addTextChangedListener for both EditTexts and put same code for both of them because one person cant simultaneously type in both EditTexts.
do the code like below.
bill Input Edittext
billInput!!.addTextChangedListener(object : TextWatcher {
override fun afterTextChanged(editable: Editable) {
//code after changed
val bill = editable.toString().toInt()
val tip = tipInput!!.text.toString().toInt()
total.text = ((bill + tip).toString())
}
override fun beforeTextChanged(charSequence: CharSequence, i: Int, i1: Int, i2: Int) {
//code before text change
}
override fun onTextChanged(charSequence: CharSequence, i: Int, i1: Int, i2: Int) {
//code during change
}
})
tip Input EditText
tipInput!!.addTextChangedListener(object : TextWatcher {
override fun afterTextChanged(editable: Editable) {
//code after changed
val bill = editable.toString().toInt()
val tip = billInput!!.text.toString().toInt()
total.text = ((bill + tip).toString())
}
override fun beforeTextChanged(charSequence: CharSequence, i: Int, i1: Int, i2: Int) {
//code before text change
}
override fun onTextChanged(charSequence: CharSequence, i: Int, i1: Int, i2: Int) {
//code during change
}
})
I would like to add full text from the editText into an array. But while running this code the array
values will be like first word , second word etc. How to get the full sentence and i want to add it
into an array(Already did it using interface)
Note : The code is placed on the recyclerview adapter ,
Where texBox is the EditText and examinationListener is the interface used
Here i shared the code. Please check it.
textBox.addTextChangedListener(object : TextWatcher {
override fun afterTextChanged(s: Editable) {
textFromBox = s.toString()
examinationListener.addAnswer(textFromBox)
}
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) {
textFromBox = textBox.text.toString()
examinationListener.addAnswer(textFromBox)
}
I created an array up to the list size. Each items of RecyclerView have an EditText.
val array = arrayOfNulls<String>(listSize)
We need to know the position of EditText to add to the array. Change interface like this:
interface ExaminationListener {
fun addAnswer(text: String, position: Int)
}
function addAnswer:
override fun addAnswer(text: String, position: Int) {
array[position] = text // we add every changes to the array.
}
This use is more useful. We don't need other methods:
textBox.doAfterTextChanged {
examinationListener.addAnswer(it.toString(), position) // you can use adapterPosition if that is in Viewholder
}
We create a sentence from array elements. You can see your full sentence:
val sentence = array.filterNotNull().joinToString (separator = " ") { it -> it }
Log.d("Sentence" , sentence)
Very Simple just add a TextWatcher to your EditText like:
val answerWatcher = object : TextWatcher {
override fun beforeTextChanged(value: CharSequence, start: Int, count: Int, after: Int) {
}
override fun onTextChanged(value: CharSequence, start: Int, before: Int, count: Int) {
when {
// Here value is your full text from your EditText
value.toString().equals("I am a sentence", ignoreCase = true) -> {
}
else -> {
// Just a test condition
}
}
}
override fun afterTextChanged(s: Editable) {
}
}
// Add this TextWatcher to your EditText
edittext.addTextChangedListener(answerWatcher)
I am creating a verification screen with 4 EditText. And I create one common sub-class of TextWatcher in Kotlin: see below code (the code might be right or wrong).
When I use the below code it crashes the application.
view.etTwo.requestFocus()
Crash Log
java.lang.NullPointerException: Attempt to invoke virtual method 'boolean android.widget.EditText.requestFocus()' on a null object reference
at com.mindfulness.greece.activity.ConfirmationCodeActivity$GenericTextWatcher.afterTextChanged(ConfirmationCodeActivity.kt:123)
at android.widget.TextView.sendAfterTextChanged(TextView.java:8007)
And here are the classes:
class GenericTextWatcher(var view: View) : TextWatcher {
override fun afterTextChanged(s: Editable?) {
val text: String = s.toString();
when(view.id) {
R.id.etOne -> {
if(text.length==1){
view.etTwo.requestFocus()
}
}
R.id.etTwo -> {
if(text.length==1){
view.etThree.requestFocus()
}
}
R.id.etThree -> {
if(text.length==1){
view.etFour.requestFocus()
}
}
R.id.etFour -> {}
}
}
override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {}
override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {}
}
the problem is that you're trying to select etOne, etTwo, etThree & etFour from INSIDE the view you receive on TextWatcher.
The view you access in the function afterTextChanged(s: Editable?)
is already the editTexts you're willing to requestFocus() on.
The view you're accessing is one of the 4 TextViews you have. You need to change the logic here in order to have the access to other TextViews
view.etTwo is null as view is the EditText you're currently editing and it doesn't contain all other edittexts.
You could call view.getParent() list all views in parent, find the next one and focus on it. Lots of work!
Or to make it more generic you could extend EditText Like this:
fun EditText.onTextChange(onAfterTextChanged: OnAfterTextChangedListener) {
addTextChangedListener(object :TextWatcher{
private var text = ""
override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) = Unit
override fun afterTextChanged(s: Editable?) {
if (s?.length == 1) {
onAfterTextChanged.complete()
}
}
override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) = Unit
})
}
interface OnAfterTextChangedListener {
fun complete ()
}
Then in you activity where you have the edittexts, call something like this:
etOne.onTextChange(object: OnAfterTextChangedListener {
override fun complete() {
etTwo.requestFocus()
}
})
etTwo.onTextChange(object: OnAfterTextChangedListener {
override fun complete() {
etThree.requestFocus()
}
})
This creates a new method in EditText called onTextChange (could be called whatever). This method calls back to your OnAfterTextChangedListener in your Activity where you have access to all your edittexts. And it will be called afterTextChanged if the length of the text equals 1.
TextWatcher common class for multiple EditText
class GenericTextWatcher internal constructor(private val view: View) :
TextWatcher {
override fun afterTextChanged(editable: Editable) { // TODO Auto-generated method stub
val text = editable.toString()
when (view.id) {
R.id.otp1 -> if (text.length == 1) edit1.requestFocus()
R.id.otp2 -> if (text.length == 1) edit2.requestFocus() else if (text.isEmpty()) edit1.requestFocus()
R.id.otp3 -> if (text.length == 1) edit3.requestFocus() else if (text.isEmpty()) edit2.requestFocus()
R.id.otp4 -> if (text.isEmpty()) edit4.requestFocus()
}
}
override fun beforeTextChanged(
arg0: CharSequence,
arg1: Int,
arg2: Int,
arg3: Int
) { // TODO Auto-generated method stub
}
override fun onTextChanged(
arg0: CharSequence,
arg1: Int,
arg2: Int,
arg3: Int
) { // TODO Auto-generated method stub
}
}
Use into your class like this
edit1.addTextChangedListener(GenericTextWatcher(edit1))
edit2.addTextChangedListener(GenericTextWatcher(edit2))
edit3.addTextChangedListener(GenericTextWatcher(edit3))
edit4.addTextChangedListener(GenericTextWatcher(edit4))