Create the skeleton for the custom view.
- [Commentator] As previously mentioned, the main purpose of our checkbox view is to support loading different SVG icons that are specified through the XML file. As the first step in creating our checkbox view, let's go over to open our project view. And we go into app, go into our package. And we go into UI and select views. And right mouse and select new Java class. And we'll name our new class checkbox.
Checkbox will inherit from app compat image view. Now let's insert a line and we're going to add our constructors. First three constructors, now let's also add a method called init view. And we'll pass in context and attributes set.
I'm going to close my project view here. Now let's go and define our private method. Init view. I'm going to actually move it to the end of the file. Init view is going to do a few things. We need to call some framework methods, first one will be set save enabled. We'll set it to true.
Next one is set layer type. And we're going to set it to software. And the paint object will be null. We're also going to make a new method here. If we do have attributes, then we're going to call our new private method.
Our new private method will be called get checkbox vectors. Get checkbox vectors will take in package name, context and attributes, so let's define our package name here first. And it will come from context. The get package name method. We'll pass it in to get checkbox vectors.
And we'll also pass in the context and the attribute set. If we don't have an attribute set, we still want to call get checkbox vectors, but with some defaults. So let me copy this, I'm going to copy it right up to package name. And we'll supply some defaults. First one is check circle black, this is the default SVG icon name. And the second one is check circle off.
Okay, let's define our get checkbox vectors. So let's select create method. And select return on the defaults. Get checkbox vectors is going to read from a styleable resource, so let's set up for that with a typed array. We're going to call obtain styled attributes.
And we're going to pass in our attribute set. And also a new styleable resource. Called checkbox. And the other attributes, the style resource and default style attribute we'll leave as zero. Now let's also define some strings, first one is the string name of the icon or the on state.
Call it SVG file name on. And we'll pass in the styleable checkbox on. We need a string name for the off state as well, so I'm just going to copy this and paste it in and we'll change this to SVG file name off.
And we'll change the styleable resource to off. So once we read in our SVG icon file names, we need to do some basic checks. So I need to check whether the string is empty. If we have both valid SVG file name on and a valid SVG file name off, then we can call get checkbox vectors again.
Otherwise, we will call the defaults. Which are the same as what we have up here, so I'm just going to copy that. And insert into here. Now let's go and create this method so it's a private method it's similar to get checkbox vectors, it's an overridden method and it starts out the same so I'm going to copy that, insert it into here and we'll define two string parameters, file name on.
And I'm going to copy this. And define the other string parameter file name off. Okay, so far so good, we have our init view created, we have our get checkbox vectors, now we're ready to go and define the styleable resource.
Released
8/31/2017- Creating a custom layout with XML
- Setting up OnClick handlers
- Making API calls
- Creating custom views
- Implementing ViewHolder objects
- Signing certificates
- Adding a checkbox
Share this video
Embed this video
Video: Create custom CheckBox