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! While you're at it, check out some resources Treehouse students have shared here.

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 (2014) Basic Android Programming Making a Button Do Something

something is wrong with button code

this is my code

 (Button) findViewById(R.id.showFactButton);
Sultan Aljaradi
Sultan Aljaradi
2,592 Points

final Button showFactButton = (Button) findViewById(R.id.showFactButton);

post that code

also you must make the id of the button same as (showFactButton)

i hop that helps

1 Answer

David Geller
David Geller
3,024 Points

Provided you have a resource named showFactsButton in one of your Layout XML files, you can get its view and assign it to a variable like:

Button myButton = (Button) findViewById (R.id.showFactButton);

You can then setup up some code to respond to a click of that button with something like:

myButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                // call your function here
            }
        });

Hope that helps.