Getting References to the Widgets

Once the layout is inflated, you can get references to the individual widgets so that you can interact with them programmatically. To do so, you use class Activity’s findViewById method. This method takes an int constant representing a specific view’s Id and returns a reference to the view. The name of each view’s R.id constant is determined by the component’s Id property that you specified when designing the GUI. For example, amount-EditText’s constant is R.id.amountEditText.

Lines 43–51 obtain references to the TextViews that are changed by the app. Lines 43–44 obtain a reference to the amountDisplayTextView that’s updated when the user enters the bill amount. Lines 45–46 obtain a reference to the percentCustomTextView ...

Get Android™ How to Program, Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.