How to call Kotlin function with function as parameter in Java Code?





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







0















I have a kotlin method as follows:



fun methodWithCallBackAsParameter(callback:() -> Unit) {
//Do Somejob
callback()
}


In kotlin we simply use this method with following syntax:



methodWithCallBackAsParameter {
//Some Instructions here.
}


Now I want to use this Kotlin method in a Java class but I am not able to figure out how.










share|improve this question























  • Doesn't methodWithCallbackAsParameter(() -> { }); work?

    – EpicPandaForce
    Nov 16 '18 at 13:14











  • It says missing return statement.

    – Prajeet Shrestha
    Nov 16 '18 at 13:16











  • I tried returning Unit.INSTANCE; it now says, Lambda expressions are not supported at language level 7.

    – Prajeet Shrestha
    Nov 16 '18 at 13:19











  • Possible duplicate of Is it possible to use Java 8 for Android development?

    – DaveyDaveDave
    Nov 16 '18 at 13:58






  • 1





    If you are using Android Studio 3.0+ then you just need to enable Java 8 language features by copy-pasting a block of code into your build.gradle, see developer.android.com/studio/write/java8-support

    – EpicPandaForce
    Nov 16 '18 at 14:02


















0















I have a kotlin method as follows:



fun methodWithCallBackAsParameter(callback:() -> Unit) {
//Do Somejob
callback()
}


In kotlin we simply use this method with following syntax:



methodWithCallBackAsParameter {
//Some Instructions here.
}


Now I want to use this Kotlin method in a Java class but I am not able to figure out how.










share|improve this question























  • Doesn't methodWithCallbackAsParameter(() -> { }); work?

    – EpicPandaForce
    Nov 16 '18 at 13:14











  • It says missing return statement.

    – Prajeet Shrestha
    Nov 16 '18 at 13:16











  • I tried returning Unit.INSTANCE; it now says, Lambda expressions are not supported at language level 7.

    – Prajeet Shrestha
    Nov 16 '18 at 13:19











  • Possible duplicate of Is it possible to use Java 8 for Android development?

    – DaveyDaveDave
    Nov 16 '18 at 13:58






  • 1





    If you are using Android Studio 3.0+ then you just need to enable Java 8 language features by copy-pasting a block of code into your build.gradle, see developer.android.com/studio/write/java8-support

    – EpicPandaForce
    Nov 16 '18 at 14:02














0












0








0








I have a kotlin method as follows:



fun methodWithCallBackAsParameter(callback:() -> Unit) {
//Do Somejob
callback()
}


In kotlin we simply use this method with following syntax:



methodWithCallBackAsParameter {
//Some Instructions here.
}


Now I want to use this Kotlin method in a Java class but I am not able to figure out how.










share|improve this question














I have a kotlin method as follows:



fun methodWithCallBackAsParameter(callback:() -> Unit) {
//Do Somejob
callback()
}


In kotlin we simply use this method with following syntax:



methodWithCallBackAsParameter {
//Some Instructions here.
}


Now I want to use this Kotlin method in a Java class but I am not able to figure out how.







java android lambda kotlin






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 16 '18 at 13:13









Prajeet ShresthaPrajeet Shrestha

5,16432148




5,16432148













  • Doesn't methodWithCallbackAsParameter(() -> { }); work?

    – EpicPandaForce
    Nov 16 '18 at 13:14











  • It says missing return statement.

    – Prajeet Shrestha
    Nov 16 '18 at 13:16











  • I tried returning Unit.INSTANCE; it now says, Lambda expressions are not supported at language level 7.

    – Prajeet Shrestha
    Nov 16 '18 at 13:19











  • Possible duplicate of Is it possible to use Java 8 for Android development?

    – DaveyDaveDave
    Nov 16 '18 at 13:58






  • 1





    If you are using Android Studio 3.0+ then you just need to enable Java 8 language features by copy-pasting a block of code into your build.gradle, see developer.android.com/studio/write/java8-support

    – EpicPandaForce
    Nov 16 '18 at 14:02



















  • Doesn't methodWithCallbackAsParameter(() -> { }); work?

    – EpicPandaForce
    Nov 16 '18 at 13:14











  • It says missing return statement.

    – Prajeet Shrestha
    Nov 16 '18 at 13:16











  • I tried returning Unit.INSTANCE; it now says, Lambda expressions are not supported at language level 7.

    – Prajeet Shrestha
    Nov 16 '18 at 13:19











  • Possible duplicate of Is it possible to use Java 8 for Android development?

    – DaveyDaveDave
    Nov 16 '18 at 13:58






  • 1





    If you are using Android Studio 3.0+ then you just need to enable Java 8 language features by copy-pasting a block of code into your build.gradle, see developer.android.com/studio/write/java8-support

    – EpicPandaForce
    Nov 16 '18 at 14:02

















Doesn't methodWithCallbackAsParameter(() -> { }); work?

– EpicPandaForce
Nov 16 '18 at 13:14





Doesn't methodWithCallbackAsParameter(() -> { }); work?

– EpicPandaForce
Nov 16 '18 at 13:14













It says missing return statement.

– Prajeet Shrestha
Nov 16 '18 at 13:16





It says missing return statement.

– Prajeet Shrestha
Nov 16 '18 at 13:16













I tried returning Unit.INSTANCE; it now says, Lambda expressions are not supported at language level 7.

– Prajeet Shrestha
Nov 16 '18 at 13:19





I tried returning Unit.INSTANCE; it now says, Lambda expressions are not supported at language level 7.

– Prajeet Shrestha
Nov 16 '18 at 13:19













Possible duplicate of Is it possible to use Java 8 for Android development?

– DaveyDaveDave
Nov 16 '18 at 13:58





Possible duplicate of Is it possible to use Java 8 for Android development?

– DaveyDaveDave
Nov 16 '18 at 13:58




1




1





If you are using Android Studio 3.0+ then you just need to enable Java 8 language features by copy-pasting a block of code into your build.gradle, see developer.android.com/studio/write/java8-support

– EpicPandaForce
Nov 16 '18 at 14:02





If you are using Android Studio 3.0+ then you just need to enable Java 8 language features by copy-pasting a block of code into your build.gradle, see developer.android.com/studio/write/java8-support

– EpicPandaForce
Nov 16 '18 at 14:02












2 Answers
2






active

oldest

votes


















1














Just return Unit.INSTANCE in the end of the lambda expression.






share|improve this answer
























  • I tried returning Unit.INSTANCE; it now says, Lambda expressions are not supported at language level 7.

    – Prajeet Shrestha
    Nov 16 '18 at 13:19











  • Either set language level to 1.8 or transform the lambda into anonymous class. This error occurred because lambdas are supported from Java 1.8, and your project uses Java 1.7.

    – Andrey Ilyunin
    Nov 16 '18 at 13:21



















1














if you want to use lambda in android you have to change your app to use Java 1.8 instead of 1.7 use can do that by it from gradle



android {
...
// Configure only for each module that uses Java 8
// language features (either in its source code or
// through dependencies).
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
}


for more look this documentation https://developer.android.com/studio/write/java8-support






share|improve this answer


























  • While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - From Review

    – Mustapha Larhrouch
    Nov 16 '18 at 14:02











  • @MustaphaLarhrouch done

    – gmetax
    Nov 16 '18 at 14:19












Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53338638%2fhow-to-call-kotlin-function-with-function-as-parameter-in-java-code%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














Just return Unit.INSTANCE in the end of the lambda expression.






share|improve this answer
























  • I tried returning Unit.INSTANCE; it now says, Lambda expressions are not supported at language level 7.

    – Prajeet Shrestha
    Nov 16 '18 at 13:19











  • Either set language level to 1.8 or transform the lambda into anonymous class. This error occurred because lambdas are supported from Java 1.8, and your project uses Java 1.7.

    – Andrey Ilyunin
    Nov 16 '18 at 13:21
















1














Just return Unit.INSTANCE in the end of the lambda expression.






share|improve this answer
























  • I tried returning Unit.INSTANCE; it now says, Lambda expressions are not supported at language level 7.

    – Prajeet Shrestha
    Nov 16 '18 at 13:19











  • Either set language level to 1.8 or transform the lambda into anonymous class. This error occurred because lambdas are supported from Java 1.8, and your project uses Java 1.7.

    – Andrey Ilyunin
    Nov 16 '18 at 13:21














1












1








1







Just return Unit.INSTANCE in the end of the lambda expression.






share|improve this answer













Just return Unit.INSTANCE in the end of the lambda expression.







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 16 '18 at 13:19









Andrey IlyuninAndrey Ilyunin

1,287224




1,287224













  • I tried returning Unit.INSTANCE; it now says, Lambda expressions are not supported at language level 7.

    – Prajeet Shrestha
    Nov 16 '18 at 13:19











  • Either set language level to 1.8 or transform the lambda into anonymous class. This error occurred because lambdas are supported from Java 1.8, and your project uses Java 1.7.

    – Andrey Ilyunin
    Nov 16 '18 at 13:21



















  • I tried returning Unit.INSTANCE; it now says, Lambda expressions are not supported at language level 7.

    – Prajeet Shrestha
    Nov 16 '18 at 13:19











  • Either set language level to 1.8 or transform the lambda into anonymous class. This error occurred because lambdas are supported from Java 1.8, and your project uses Java 1.7.

    – Andrey Ilyunin
    Nov 16 '18 at 13:21

















I tried returning Unit.INSTANCE; it now says, Lambda expressions are not supported at language level 7.

– Prajeet Shrestha
Nov 16 '18 at 13:19





I tried returning Unit.INSTANCE; it now says, Lambda expressions are not supported at language level 7.

– Prajeet Shrestha
Nov 16 '18 at 13:19













Either set language level to 1.8 or transform the lambda into anonymous class. This error occurred because lambdas are supported from Java 1.8, and your project uses Java 1.7.

– Andrey Ilyunin
Nov 16 '18 at 13:21





Either set language level to 1.8 or transform the lambda into anonymous class. This error occurred because lambdas are supported from Java 1.8, and your project uses Java 1.7.

– Andrey Ilyunin
Nov 16 '18 at 13:21













1














if you want to use lambda in android you have to change your app to use Java 1.8 instead of 1.7 use can do that by it from gradle



android {
...
// Configure only for each module that uses Java 8
// language features (either in its source code or
// through dependencies).
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
}


for more look this documentation https://developer.android.com/studio/write/java8-support






share|improve this answer


























  • While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - From Review

    – Mustapha Larhrouch
    Nov 16 '18 at 14:02











  • @MustaphaLarhrouch done

    – gmetax
    Nov 16 '18 at 14:19
















1














if you want to use lambda in android you have to change your app to use Java 1.8 instead of 1.7 use can do that by it from gradle



android {
...
// Configure only for each module that uses Java 8
// language features (either in its source code or
// through dependencies).
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
}


for more look this documentation https://developer.android.com/studio/write/java8-support






share|improve this answer


























  • While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - From Review

    – Mustapha Larhrouch
    Nov 16 '18 at 14:02











  • @MustaphaLarhrouch done

    – gmetax
    Nov 16 '18 at 14:19














1












1








1







if you want to use lambda in android you have to change your app to use Java 1.8 instead of 1.7 use can do that by it from gradle



android {
...
// Configure only for each module that uses Java 8
// language features (either in its source code or
// through dependencies).
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
}


for more look this documentation https://developer.android.com/studio/write/java8-support






share|improve this answer















if you want to use lambda in android you have to change your app to use Java 1.8 instead of 1.7 use can do that by it from gradle



android {
...
// Configure only for each module that uses Java 8
// language features (either in its source code or
// through dependencies).
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
}


for more look this documentation https://developer.android.com/studio/write/java8-support







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 16 '18 at 14:19

























answered Nov 16 '18 at 13:21









gmetaxgmetax

2,55422036




2,55422036













  • While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - From Review

    – Mustapha Larhrouch
    Nov 16 '18 at 14:02











  • @MustaphaLarhrouch done

    – gmetax
    Nov 16 '18 at 14:19



















  • While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - From Review

    – Mustapha Larhrouch
    Nov 16 '18 at 14:02











  • @MustaphaLarhrouch done

    – gmetax
    Nov 16 '18 at 14:19

















While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - From Review

– Mustapha Larhrouch
Nov 16 '18 at 14:02





While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - From Review

– Mustapha Larhrouch
Nov 16 '18 at 14:02













@MustaphaLarhrouch done

– gmetax
Nov 16 '18 at 14:19





@MustaphaLarhrouch done

– gmetax
Nov 16 '18 at 14:19


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53338638%2fhow-to-call-kotlin-function-with-function-as-parameter-in-java-code%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Bressuire

Vorschmack

Quarantine