Skip to content
Advertisement

Using kotlin extension function in Java code

I have created an extension function for listening to click listener for buttons, when I try to access it from Java code it does not work,

Cannot access clicks() from java file, this is what I tried

Can you please suggest how to resolve this.

Thanks R

File – Extension.kt

   fun Button.clicks(): Flow<Unit> = callbackFlow {
        setOnClickListener {
            offer(Unit)
        }
        awaitClose { setOnClickListener(null) }
    }

In my java code – FillingFragment.java

 @NotNull
    @Override
    public Flow<Void> getStartFillingObservableFlow() {
        return dataViewHolder.btnStartFilling.clicks(); //CLICKS IS NOT. RECOGNISED
    }

DataViewHolder.Java

  @BindView(R.id.start_filling_action)
    public Button btnStartFilling;

Advertisement

Answer

Extension functions usually compiles to statics functions so you can invoke them like this:

ExtensionKt.clicks(dataViewHolder.btnStartFilling);
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement