4

How does Android Google Play app change color dynamically on user clicks on AppBarlayout?

enter image description here API 21 Lollipop I have tried the following

tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener()
{
    @Override
    public void onTabSelected(TabLayout.Tab tab)
    {
        setTheme(R.style.AppTheme_2);
    }
}

<style name="AppTheme_2" parent="Theme.AppCompat.Light.DarkActionBar">
    <item name="colorPrimary">@color/colorPrimary_2</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark_2</item>
    <item name="colorAccent">@color/colorAccent_2</item>
</style>
Jai
  • 83
  • 7

1 Answers1

1

You need to get the tab position in order to manipulate the color. Here is the example.

 mTabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
        @Override
        public void onTabSelected(TabLayout.Tab tab) {
            Log.e(TAG, String.valueOf(tab.getPosition()));

            switch (tab.getPosition()){
                case 0:
                    mAppBar.setBackgroundResource(R.color.colorPrimary);
                    break;
                case 1:
                    mAppBar.setBackgroundResource(R.color.colorPrimary);
                    break;
                case 2:
                    mAppBar.setBackgroundColor(Color.BLACK);
                    break;
            }
        }
Nizzam
  • 720
  • 2
  • 11
  • 26
  • appreciated, will try once i have the settled the critical stuffs and will post a tick if it works. thanks a lot. – Jai Sep 15 '16 at 06:18
  • May i know why you use a mixture of setBackgroundColor and setBackgroundResource ? i tested both it seems to work for both. But under API 21, Google Play app has some kind of animation effect. Still not exactly the same. i think Google Play app achieved it via application android:theme="@style/xxx i think. – Jai Nov 04 '16 at 06:26