How to Create Our Own Listener Interface in Android

Android Custom Event Listener

public class CustomView extends View(){
OnCustomEventListener mListener;
:
://some code
:
:

Create an interface that will be implemented by your activity:

public interface OnCustomEventListener {
void onEvent();
}

public void setCustomEventListener(OnCustomEventListener eventListener) {
mListener = eventListener;
}

Now you need to know when the event is actually occurring. For example when the user touches a point on screen, override onTouchEvent method:

onTouchEvent(MotionEvent ev) {
if (ev.getAction==MotionEvent.ACTION_DOWN) {
if(mListener!=null)
mListener.onEvent();
}
}

Similarly, you can create a specific event that you want. (examples could be touch down, wait for exactly 2 seconds and release- you would need to do some logic inside touch event).

In your activity, you can use the customView object to set an eventListener as such:

 customView.setCustomEventListener(new OnCustomEventListener() {
public void onEvent() {
//do whatever you want to do when the event is performed.
}
});

How to implement a listener

AsyncTask has nothing to do with implementing a listener.

Here's a listener:

public interface TheListener {
public void somethingHappened();
}

Call it however you want. For example, here's a class doing something like View:

public class Something {
private TheListener mTheListener;

public void setTheListener(TheListener listen) {
mTheListener = listen;
}

private void reportSomethingChanged() {
if (mTheListener != null) {
mTheListener.somethingHappened();
}
}
}

You can make this as complicated as you want. For example, instead of a single listener pointer you could have an ArrayList to allow multiple listeners to be registered.

Calling this from native code also has nothing to do with implementing a listener interface. You just need to learn about JNI to learn how native code can interact with Java language code.

Create my own listener using lambdas

Your lambda is not suitable to cast to the OnItemClickListener interface. You'll have to implement the interface manually and call the function in there. To improve the implementation, you can also use the inline feature with it.

inline fun setOnItemClickListener(crossinline listener: (Item) -> Unit) {
this.listener = OnItemClickListener { listener(it) }
}

This will work if OnItemClickListener is a Java interface. If it's a Kotlin interface, you'd have to implement it with the object notation.

inline fun setOnItemClickListener(crossinline listener: (Item) -> Unit) {
this.listener = object : OnItemClickListener {
override fun onItemClick(item: Item) = listener(item)
}
}

Instantiating an Interface Listener in Kotlin

You can do watchers/listeners like this:

val textView: TextView = this.findViewById(R.id.amountEdit)
val watcher = object : TextWatcher {
override fun afterTextChanged(p0: Editable?) {
val inputAmount = textView.text.toString
val amount = if (!inputAmount.isEmpty()) inputAmount.toDouble() else 0.0
conversionViewModel?.convert(amount)
}

override fun beforeTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {
println("before text changed called..")
}

override fun onTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {
println("text changed..")
}
}

and then:

textView.addTextChangedListener(watcher)

Notice the object in declaring the watcher. Can do the same with a listener.



Related Topics



Leave a reply



Submit