PicassoBackgroundManager
– Android TV application hands on tutorial 4

picassobackgroundmanager-2015-07-03-235505

[Updated 2015.11.18]: Revise.
[Updated 2016.3.13]: Updated wrong source code.

Aim of this chapter

Implementing background image update feature. Application was boring with no background, and it becomes much nice if it has appropriate background.
Just setting background is easy, though, Android TV sample application explains how to dynamically change background linking with your current selection of contents.

Before explanation of background change, I start explanation of onItemSelected callback function so that we get the event notification when the item is selected. Next, I will show simple background change implementation followed by better performance implementation using Picasso library.

setOnItemViewSelectedListener listener & onItemSelected callback

BrowseFragment supports to set listener when the itemview is selected & clicked. Current target is to be get notified when the user move the cursor and change the selection of item.

we can use setOnItemViewSelectedListener(OnItemViewSelectedListener listener) function for this purpose. In the argument, you can put the listener class which should implement OnItemViewSelectedListener interface which is also provided by leanback library. Then, you can implement onItemSelected callback function, which is the function called when an item is selected.

We will proceed to implement background change function in the following. Here I arranged to create SimpleBackgroundManager, and PicassoBackgroundManager to handle background image (Android TV sample application is doing it inside MainFragment.java).

SimpleBackgroundManager

There is explanation in official developer’s site, see Update the Background for reference.
I wrote some test code below.

Right click package name → New → class → SimpleBackgroundManager

This SimpleBackgroundManager keeps a member mBackgroundManager of BackgroundManager class, which handles the actual background change. This BackgroundManager instance is a singleton instance which can be obtained via BackgroundManager.getInstance().

At first, instance of BackgroundManager is created in Constructor. It must be attached with Window before updating background, and these initialization is done in constructor. 

updateBackground method will change the background, and clearBackground method will update the background to default image. (I have added the res/drawable/default_background.xml and updated res/values/colors.xml.)

Modification of MainFragment is small.

Build and run!

We can check background is updated depending on the selection of the row. Also the Background will be back to default when you go back to GridItemPresenter row.

background1-2015-07-03-193733

 

Source code is on github.

PicassoBackgroundManager

Let’s improve SimpleBackgroundManager implementation. What we will improve is following.

  1. Delay updating background
    In the previous implementation, main thread will always try to update background when user is moving their cursor, and changing select item. It is busy, and may cause bad performance. Below we will implement TimerTask to wait certain period of time from updating background image.
  2. Image handling by using Picasso library
    Picasso library is “A powerful image downloading and caching library for Android“. We will use it for more easier image resource handling.

Create new class PicassoBackgroundManager, implement as follows

Now, we will replace from SimpleBackgroundManager to PicassoBackgroundManager in MainFragment.java

Build and run!

We can check background is updated after 500 ms thanks to the timer task. And we are getting the background image from web by using Picasso.

picassobackgroundmanager-2015-07-03-235505

Source code is on github.

Until now, we cannot click item/card. Next chapter, DetailsOverviewRowPresenter & FullWidthDetailsOverviewRowPresenter – Android TV application hands on tutorial 5, is to implement a onClickListener and show content details by DetailFragment.

Sponsored Links

4 responses

  1. Hi
    You mention that SimpleBackgroundManager is a singleton while is not the case.
    By the way, I love your tutorials on Android TV !

    • Hi, yes you are correct. It seems I updated source code but I didn’t update the blog.
      Thank you for the comment. I will modify wrong part.

  2. I already follow tutorial until this part :
    private final class ItemViewSelectedListener implements OnItemViewSelectedListener {
    @Override
    public void onItemSelected(Presenter.ViewHolder itemViewHolder, Object item,
    RowPresenter.ViewHolder rowViewHolder, Row row) {
    // each time the item is selected, code inside here will be executed.
    if (item instanceof String) { // GridItemPresenter row
    simpleBackgroundManager.clearBackground();
    } else if (item instanceof Movie) { // CardPresenter row
    simpleBackgroundManager.updateBackground(getActivity().getDrawable(R.drawable.movie));
    }
    }
    }
    in this part it seems this method is fired multiple times, in makes my bg looks like blinking. is it normal behaviour and will be fixed in next tutorial or it’s my own error ?

    • Hi Lia, yes this behavior is ok. onItemSelected will be called every time you select some item. So it is updating background very frequently when you keep changing the selection of the item (card).
      Of course, this behavior is not acceptable, and you can see next section “PicassoBackgroundManager” for how to cooperate to update background in more smooth way.

Leave a Reply

Your email address will not be published.