Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community!

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Android Build a Simple Android App with Java Basic Android Programming Adding the OnClick Method

Binyamin Friedman
Binyamin Friedman
14,615 Points

Why can't the new View.OnClickListener and it's single method onClick be turned into a lambda?

View.OnClickListener listener = new View.OnClickListener() {
       @Override
        public void onClick(View view) {
              String fact = "Ostriches can run faster than horses";
              factTextView.setText(fact);
        }
};

Why can't this be a lambda?

I tried to change the language level to java 8, but it still didn't work

1 Answer

Seth Kroger
Seth Kroger
56,409 Points

The short answer is: Lambdas yes with new preview versions of Android Studio, steams and other functional programming stuff Nougat and higher only. If you are on the "Stable" channel for updates change it to Beta or higher/riskier.

https://developer.android.com/studio/write/java8-support.html

(For the past year or so there was a separate build toolchain that came with Andoird N/Nougat called jack you could enable for lambda support but that's been deprecated. The tools in Android Studio 3 (preview) are the way to go going forward)