findviewbyid

Android: How to look up a widget (defined in XML layout file) by its id

Android FAQ: How do I look up (find) a widget in my Java code that I’ve defined in an XML layout file?

Solution: When you define your Android widgets in XML and then need to find a widget by its id, the method you need is named findViewById.

For example, you’ll use code like this to look up a widget by its id and create it in your Java code:

An Android Button click (tap) listener example

Android Button FAQ: How do I add a listener to a Button in Android?

To add a click (tap) listener to a button in Android, just get a reference to the button (typically using findViewById), then call the setOnClickListener method of the button, like this: