Join Albert Lo for an in-depth discussion in this video Implement CheckBox, part of Android Development: Modern Android UI Design Elements.
- [Instructor] Let's go back into our CheckBox class. I'm going to dismiss our project view, I'm going to close off our gradle file. So loadVectorFromSvgFile method, what it will do is handle the actual loading of the SVG resource from a file. So let's implement that here. First, let's rename this to rawFileId, and let's do a validation check on our rawFileId.
If we have a valid file ID then let's try loading it. So let's get an SVG reference. And we're going to call getFromResource, passing in our context, and the rawFileId. If we have an SVG and we didn't run into an exception we'll just return it as a PictureDrawable.
And we call the renderToPicture to convert to a PictureDrawable. If there is an error this method will throw an exception. So let's handle that here with our catch and the exception we want to catch is SVGParseException. And let's just print the StackTrace if there was an exception. If we don't have a valid file ID we will return null.
Now let's add some other methods. The next one we want to add is the onClick. It's a public method and what it will do is handle the checked and unchecked state for this view. Let's add a private class member called isChecked, so we're going to scroll to the top and declare a private boolean called isChecked. I'm going to copy that and go back down to the bottom and add a check, if isChecked is true then we toggle it.
And we set our ImageDrawable to the unchecked resource, otherwise we set it to the opposite state. And we set our ImageDrawable to checked resource. The next method we want to add is the setChecked method. This is a public method and it's similar to onClick, except it actually sets the checked and unchecked state for this view.
SetChecked will take in a boolean parameter, we'll call it isChecked. And we're going to assign our class member to it. Let's declare a local variable for our content. If it is checked we're going to set our content and our ImageDrawable. If it's not checked we'll do something similar.
And set our ImageDrawable to the unchecked resource. For accessibility we're going to add setContentDescription here and pass in the content that we just defined above. We will also send an AccessibilityEvent and the type will be TYPE_VIEW_CLICKED. Now let's add a private class to store the state of our CheckBox.
We will be declaring a private static class and the name will be SavedState. It will extend BaseSavedState. I'm going to press Control + O to insert a constructor. We'll take the first constructor that takes in a Parcel parameter. I'm going to rename this to in.
And we're going to add a method for reading BooleanArray here and we'll pass in states. Let's declare two private class members. The first one is isChecked and the other one is an array of states. Now let's go back to our constructor and let's add a logic check, if states.length is 1 then we can just initialize isChecked to the first element.
Let's press Control + O to add another constructor. This one will take the Parcelable parameter. Now let's add in an Override method, press Control + O again, we'll select writeToParcel. And here we're going to add out.writeBoolean and pass in our states array. Now let's add a creator. Public static method, and type is Parcelable.Creator, type SavedState.
We'll create a new Parcelable.Creator, type SavedState. And we'll add two methods. The first is createFromParcel and the other is newArray. For createFromParcel I'm just going to return a new SavedState, passing in source. For the other one I'm going to create a new SavedState, passing in size as my parameter.
Now let's use our SavedState that we created here, let's copy it, I'm going to go up to initView. Right above a initView I'm going to add an Override method. The method we'll add is onSavedInstanceState. We're going to get a reference to Parcelable here, call it superState, and just call the super class method, onSavedInstanceState.
We'll get a reference to SavedState here by constructing a new instance of SavedState. And we pass in our superState. We're going to save the state of our isChecked local variable. And then return state. So let's fix this syntax error, it's actually called onSaveInstanceState.
Now I'm going to create a corresponding method for restoring instant state. It's called onRestoreInstanceState. It will take one parameter, Parcelable. So let's do a logic check here. If we don't have a state that is an instance of our SavedState, then we're going to call the super class onRestoreInstanceState.
Passing in our state parameter. Otherwise, we will get a reference to the SavedState by casting our Parcelable and we'll call the super class on this onRestoreInstanceState, and we'll pass in our savedState.getSuperState.
Let's fix that syntax error there. And then lastly, we're going to call setChecked. And pass in our savedState CheckBox state. Now let's go up to our constructor. We need to call initView for the other constructors. And this one we have no attribute set, so it will be null. Now let's go down to initView and we want to initialize setImageDrawable if it passes some logic check.
So if we have the checked and unchecked resources then we can initialize our ImageDrawable, and we'll set it to unchecked state. Otherwise we will just declare an error saying that we couldn't find the Checkbox vectors. Now let's go up to the top of the class.
One last thing we'll do is declare that String TAG. And we'll use CheckBox.class and call the getSimpleName as our tag. Okay, now we have finished the coding for CheckBox class.
- Creating a custom layout with XML
- Setting up OnClick handlers
- Making API calls
- Creating custom views
- Implementing ViewHolder objects
- Signing certificates
- Adding a checkbox