Prism / ViewPager

Prism Fundamentals – Part 2

IMPORTANT: Updates to Prism are on indefinite hold – more details can be found in the README in the within the Prism source. I have decided to continue publishing this short series because it documents how to use Prism in its current form and so still may be useful.

I am extremely excited to announce the release of Prism – an all-new dynamic theming library for Android. This is an initial release to get the basic functionality out there, but it is already pretty powerful. However there are some exciting additions in the pipeline which will make it more powerful still. In this series of articles we’ll cover the various aspects of prism to enable you to make use of it and even extend it yourself to meet the requirements of your project.

Prism LogoPreviously we looked at wiring up some UI components to a Prism instance so that calling setColour(int colour) on the Prism instance resulted in colour changes for all of the components that we’d wired up. So while using a combination of Setters and Filters wired together in a Prism instance simplified things and removed a lot of boilerplate code, it didn’t really do anything that we couldn’t do fairly easily. However when we add a Trigger in to the equation things become much more interesting!

First we need to add prism-viewpager as a dependency to our project:

A Trigger sits in front of the Prism instance and is essentially what triggers the colour changes. We’ll begin by looking at ViewPagerTrigger which will trigger colour changes in response to user interactions with a ViewPager. In order for this to happen the Adapter for the ViewPager will need to provide colour information for each position and so much implement the ColourProvider interface (or ColorProvider, if you prefer, and don’t mind the tiny performance hit that incurs):

For anyone who has ever used PagerTitleStrip or the new design library TabLayout will be familiar with the concept of providing a title for each pager position, and this interface does precisely the same thing only it provides an RGB colour value instead of a title String. You don’t need to worry about implementing getCount() if you’re extending an Adapter as this is already built in to the Adapter. All that’s required is to implement this in to your Adapter:

So now that we have an Adapter which implements ColourProvider we can hook this up to the Prism ViewPagerTrigger:

In setupViewPager() we first create an instance of our RainbowPagerAdapter and set this on the ViewPager. Next we create the a TintFilter which we’ll use to provide a lighter background for the FAB, and then create a Trigger instance based on the ViewPager and the Adapter.

Now we construct our Prism instance in exactly the same way as we did before. The only differences are that we’re hooking up a couple more components, and we add the Trigger that we just created. You may notice that we’re setting the colour of the ViewPager itself. This will actually change the ViewPager over-scroll glow colour. (This is actually quite tricky because it is done differently on different OS levels, but Prism takes care of all of this internally).

Then we wire the TabLayout up to the ViewPager (this is required by TabLayout and not Prism), and finally set the current selection of the ViewPager to the first item.

That is it. The UI will now change colour in response to the user scrolling between tabs:

The more observant will notice that the colour changes do not jump – the transitions are actually animated smoothly as the ViewPager animates between individual pages. The colour transitions will even follow the user’s dragging:

There is some more subtle stuff going on. If the user were to tap on tabs a long distance apart, then normal behaviour would be to transition between each colour in between the start and end tab. However this creates a kind of strobing effect so ViewPagerTrigger actually performs a smooth transition between the start and end colours (note how the colour transitions between yellow and violet directly without going through green, blue, and indigo):

Finally let’s have a look at the ViewPager over-scroll that we mentioned earlier:

So, with an understanding of what this does, take a look back at the code which was required to achieve this: Some additions to the Adapter to provide colour information, and 10 lines code to wire up Prism (plus another 3 lines of code to clean up).

But that’s not all – we can also trigger Prism from Palette and we’ll cover that in the next article in this series.

The full source for this sample is available as one of the sample apps within the Prism source.

© 2015, Mark Allison. All rights reserved.

CC BY-NC-SA 4.0 Prism Fundamentals – Part 2 by Styling Android is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License. Permissions beyond the scope of this license may be available at

Leave a Reply

Your email address will not be published. Required fields are marked *