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
- Perform an Action after Enter keypress on EditText
- android:ellipsize - Add an ellipsis(...) in a TextView
- Create a Drawable Object Variable
- Solution: Running Android Lint has encountered a problem Failed
- MenuInflater - How to Inflate a Menu
- How to Register an Activity that Responds to NFC tags for URI's that point to a URL
- setOnClickListener - How to Create a Click Handler for a Button, using a method
- android:alpha - Set the Opacity/Transparency of Text in TextView/EditText
- Create a Web Browser
- How to Add a Toast