Chap 11 Challenge - First and Last Buttons

Implemented my UI and added buttons to CrimeFragment. Struggled with hooking up to the ViewPager, but found something that seems to work. Any comments?

    mLastButton = (Button)v.findViewById(;
    mLastButton.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            Log.d("CrimeFragment", "last");
            ViewPager mViewPager = (ViewPager)getActivity().findViewById(;


While that would work really well, it breaks the encapsulation because now CrimeFragment has to know about ViewPager. A possible alternative solution could be to subclass CrimeFragment, add the 2 buttons to that subclass and create and interact with that fragment in CrimePagerActivity.

However, the challenge said to “Add two buttons to CrimePagerActivity that allow jumping the ViewPager to the first or last crime instantly.” I assume that the author meant that CrimePagerActivity activity would add the 2 buttons to the CrimeFragment as well as the 2 listeners to go to first or last crimes.

I would like to try to do this but I can’t figure out how to get a reference to the CrimeFragment’s view from the CrimePagerActivity so that the 2 buttons could be added.

Excellent point. It seems like the idea would be to have CrimePagerActivity’s layout file (activity_crime_pager.xml) consist of the ViewPager on top and another layout item with the two buttons on the bottom. Then the buttons need only be defined in CrimePagerActivity. Would this be possible?

Yea, the best way to solve this challenge would be to have those buttons exist at the Activity level rather than the fragment level.

You can add views to your activity’s layout file in addition to the container for the Fragment. So, you could do something like this in the Activity’s layout file:


Thanks. Off to Studio!

Thanks! That was the hint I was looking for! Also, instead of making my root layout a FrameLayout I used ConstraintLayout which made it easier for me.

Great! ConstraintLayout will work too. It’s a subclass of FrameLayout, so the view positioning behavior will be the same.

If using a FrameLayout (instead of a LinearLayout for example), how do you prevent the ViewPager area from extending down into the FrameLayout area that includes the two Buttons? I can see how to do this with a LinearLayout or ConstraintLayout, but the FrameLayout seems less obvious…

If you do not want the buttons to overlap the ViewPager area, then you would have to switch over to a LinearLayout at the root level. Something like this:

1 Like

Ok, that makes sense – thanks!

I suppose mine is highly inefficient :man_facepalming: Your layout is more flatter, whereas mine looks like below:


Yes, I also decided to control the buttons within the Activity rather than the Fragment. I would like to discuss my answer but I do not know if this is the right place. I do dread giving away spoilers :grin: