Showing posts with label API Level1. Show all posts
Showing posts with label API Level1. Show all posts

findViewById - Find a CheckBox using ID

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:

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.

3. Add a Deprecated: onCreateDialog method to your class. Add the showDialog with a constant of TIME_PICKER_DIALOG to the onClick in the setOnClickListener in step 1. Add a case, named TIME_PICKER_DIALOG. In the case add the below line.

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)