71

I have implemented an AlertDialog with normal negative and positive button click listeners.

When I called new DialogInterface.OnClickListener() it was showing me a suggestion saying: Anonymous new DialogInterface.OnClickListener() can be replaced with lambda. I know it's not an error or something big but what exactly is this suggestion and what can I do about it?

AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setPositiveButton("Text", new DialogInterface.OnClickListener() {
    @Override
    public void onClick(DialogInterface dialog, int which) {
        // do something here
    }
});

Android Studio V1.2.1.1 compileSdkVersion 22 buildToolsVersion "22.0.0" minSdkVersion 14 targetSdkVersion 22

TylerH
  • 20,799
  • 66
  • 75
  • 101
Kavin Prabhu
  • 2,307
  • 2
  • 17
  • 36

3 Answers3

139

It means that you can shorten up your code.

An example of onClickListener() without lambda:

mButton.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        // do something here
    }
});

can be rewritten with lambda:

mButton.setOnClickListener((View v) -> {
    // do something here
});

It's the same code. This is useful when using a lot of listeners or when writing code without an IDE. For more info, check this.

Hope this answers your question.

Strider
  • 4,452
  • 3
  • 24
  • 35
  • 9
    Just remember that lambda is Java 8 feature which is not supported in android. If you like this feature use retrolambda - https://github.com/evant/gradle-retrolambda – rwojcik Jun 10 '15 at 09:49
  • 9
    @rwojcik Lambda is supported in Android – portfoliobuilder Jun 26 '15 at 17:04
  • @Strider What happened if I want to add another method inside of listener? How can I write it? – emaleavil Nov 26 '15 at 09:33
  • 7
    Android N started to support Java 8, so `Lambda` is officially supported now. – IronBlossom Apr 01 '16 at 15:29
  • And you don't need to import "android.view.View" or "android.view.View.OnClickListener" for click Listeners. – Arash Mar 29 '18 at 11:04
  • Note that the usage of lambda functions not only allows for shortening code and making code more elegant; it also produces [more efficient bytecode](https://www.infoq.com/articles/Java-8-Lambdas-A-Peek-Under-the-Hood). – MC Emperor May 22 '18 at 17:45
  • Which is fast?? Lamda or Simple interface . or can anyone explain what is the difference between them besides shortening the code. is it worth to replace all interfaces by lamda in fully developed app ??? – Prinkal Kumar Jul 04 '18 at 05:58
  • @PrinkalKumar You should ask a new question for that rather than hope it gets answered in the comments of this one. – TylerH Apr 22 '19 at 19:14
  • So what is actually going on? Does a lamda sport a hidden anonymous class underneath like without, or is it just the function pointer and the anonymous class can be discarded? It would seem a class is not the same parameter type as a function pointer...? – JohnyTex Apr 20 '23 at 11:41
7

Its as simple as this:

button.setOnClickListener(view -> username = textView.getText());
LEMUEL ADANE
  • 8,336
  • 16
  • 58
  • 72
5

To replace the classic new DialogInterface.OnClickListener() implementation with lambda expression is enough with the following

 builder.setPositiveButton("resourceId", ((DialogInterface dialog, int which) -> {
      // do something here
 }));

It´s just taking the onClick event parameters.

Gabriel Perez
  • 373
  • 6
  • 11