findViewbyId was added in API Level 1
findViewById: Look for a child CheckBox with the given id. If this view has a given id, return this CheckBox. The findViewById() method is available to all activities in Android. findViewById allows you to find a view inside an activity layout.
1. Name a CheckBox myCheckBox.
2. Declare a CheckBox named myCheckBox..
3. In MainActivity, java file, add the below code to the onCreate method. Where R.id.myCheckBox is the same as the android:id name in your main.xml file, and myCheckbox is the CheckBox name referred to in Java. This returns a CheckBox class and is casted to a CheckBox type so you can work with it.
myCheckBox = (CheckBox) findViewById(R.id.myCheckBox);
4. Compile and run!
Resources:
Popular Posts
- Cursor getColumnIndex - Get the Index for a Column Name
- Solution: error: Error parsing XML: unbound prefix
- Save LogCat to a Text File
- android:ellipsize - Add an ellipsis(...) in a TextView
- Intent setType(String type) - How to Set an Explicit MIME data type
- How to Determine if Android Device has GSM Telephony
- Change the line spacing between lines of text in an TextView/EditText, using lineSpacingMultiplier
- setVisibility() - Hide a Button, in Java
- android:ems
- android:alpha - Set the Opacity/Transparency of Text in TextView/EditText