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:
Showing posts with label API Level1. Show all posts
Showing posts with label API Level1. Show all posts
Deprecated: How to Display a Time Picker Dialog from a Button
Note: Some of these steps use deprecated items.
1. Create a Click Handler for a Button, using a method named registerButtonListenersAndSetDefaultText and a private class level defined Button named myTimeButton.
2. Add a private class level integer constant named TIME_PICKER_DIALOG and set 1.
return showTimePicker();
4. Add a New method named updateTimeButtonText to your class, and call it from the registerButtonListenersAndSetDefaultText method, after the setOnClickListener.
5. In the onCreate method, Get Current Date/Time and Save to Calendar object with a private class level declared Calendar object named myCalendar.
6. Instantiate a TimePickerDialog object with the TimePickerDialog initial values of:
hour of: myCalendar.get(Calendar.HOUR_OF_DAY),
minute of: myCalendar.get(Calendar.MINUTE),
AM/PM of: true
7. In the onTimeSet method,
- set the Calendar.YEAR to the year variable
- set the Calendar.MONTH to the monthOfYear variable
- set the Calendar.DAY_OF_MONTH to the dayOfMonth variable
- then call the updateDateButtonText method
8. In the updateDateButtonText method, add a Format Calendar Date using SimpleDateFormat with a format of "yyyy-MM-dd".
9. Then set the Text for the Button named myTimeButton to the myString variable.
10. Compile and run!
Resources:
setImageDrawable - Set a Drawable to an ImageView
setImageDrawable() was added in API Level 1
Sets a drawable as the content of this ImageView.
1. Set a Drawable Object Variable with a drawable named myDrawable.
2. Find an ImageView using ID, named myImageView.
3. In the MainActivity.java file, add the following code to the onCreate method. This will set the Drawable to the ImageView.
myImageView.setImageDrawable(myDrawable);
4. Compile and run!
Resources:
http://developer.android.com/reference/android/widget/ImageView.html#setImageDrawable(android.graphics.drawable.Drawable)
Sets a drawable as the content of this ImageView.
1. Set a Drawable Object Variable with a drawable named myDrawable.
2. Find an ImageView using ID, named myImageView.
3. In the MainActivity.java file, add the following code to the onCreate method. This will set the Drawable to the ImageView.
myImageView.setImageDrawable(myDrawable);
4. Compile and run!
Resources:
http://developer.android.com/reference/android/widget/ImageView.html#setImageDrawable(android.graphics.drawable.Drawable)
Subscribe to:
Posts (Atom)
Popular Posts
- android:ellipsize - Add an ellipsis(...) in a TextView
- Save LogCat to a Text File
- Solution: error: Error parsing XML: unbound prefix
- Perform an Action after Enter keypress on EditText
- Intent setType(String type) - How to Set an Explicit MIME data type
- Cursor getColumnIndex - Get the Index for a Column Name
- android:nextFocusRight - Set the Next Focus Right to a TextView/EditText
- android:textColorHint - Set the Color of Hint Text in TextView/EditText
- android:nextFocusDown - Set Next Focus to a TextView/EditText
- Determine the Hex value for a color