Join Albert Lo for an in-depth discussion in this video Set up OnClick handlers, part of Android Development: Modern Android UI Design Elements.
- [Instructor] Let's finish off our implementation of the side navigation drawer. We will switch over to the main activity. We got up to the onCreate. There's a lot of initialization code here. The plan is to create one method to hold all of our view initialization code. I'm going to highlight all of the code in here, and cut it with Command X. I'm going to scroll down near the bottom. I'm going to add a new method called inflateViews, it's a private method.
And in here, I'm going to paste back the code that I cut, with Command V. InflateViews is a best practice to define a single method, where all of the views for a given screen will be initialized. We can also get rid of the deprecation warning here, by replacing setDrawerListener with addDrawerListener. The default menu items for navigation view, are no longer needed, so we can also delete the setNavigationItemSelectedListener.
At the bottom of inflateViews method, we are going to add a new method called setNavBarButtons. With the nav bar item that we created in the previous video, now it becomes easy to initialize the onClick handlers for our text views. Let's go ahead and create setNavBar private method. To initialize our onClick handlers, all we need to do is iterate through all of the nav bar items.
For each nav bar item, from the set of values, we will find the TextView from the view ID. We will call the getItemID method of the eh-nu.
Now that we have the TextView, we can set the onClickListener to this activity. Now we have set the onClickListener to this activity, I am going to press Alt Return, and select the section that says make activity implement the OnClickListener. Now in our onClick method, we have a view that can return a view ID, so we can switch.
We will pass the views ID to the nav bar items from ViewID method, and pass in our view getID method. We will add in our case for Google Photos. In the case of Google Photos, we are going to do three actions. The first action will be to display our Google Photos fragment, and we'll add the private method here.
The next action will be to close the drawer. So I'm going to scroll back up to where the drawer is defined. Instead of local scope, I'm going to change this to be a class member. So I'm going to cut with Command X, and move this to the beginning, and insert it with Command V, and make it a private member.
And we go back in, fix our syntax error. Now we have access to our drawer. I'm going to copy it with Command C, and go back down to our onClick method for our Google Photos case, and add in an action to close the drawer by calling the closeDrawer method, and passing in a gravity of start.
So we can also add in the other cases for the other nav drawer items here. I'm going to leave them empty for now. Now we have the cases for our onClick handler all complete, let's scroll back up to our inflateViews. At line 62, our inflateViews method is also complete now, so let's call it.
I'm going to press Command C to copy this method, and let's scroll up to the onCreate. At line 27 I'm going to paste it in with Command V to call inflateViews. Now let's scroll back down to the bottom of the file, and now we're ready to implement displayGooglePhotosFragment.
- Creating a custom layout with XML
- Setting up OnClick handlers
- Making API calls
- Creating custom views
- Implementing ViewHolder objects
- Signing certificates
- Adding a checkbox