title: how to change the color of the toolbar and status bar colors according to image color in android material design?
body: <p>I started learning Android material design. I learnt some new 
  features in the lollipop version. Today I am trying to change the color of the tool bar and status bar according to an image's color. Can anyone guide me or provide any links for how to do this?</p>
  
  <p><img src="https://i.stack.imgur.com/bVYF9.png" alt="screenshot"></p>
  
  <p>For example, here the color of the status bar and toolbar changes when changing view pager tabs. Thank you in advance.
  <img src="https://i.stack.imgur.com/wCPBB.png" alt="screenshot"></p>
  
  <p>styles.xml:</p>
  
  <pre><code>   &lt; style name="MyMaterialTheme.Base" parent="Theme.AppCompat.Light.DarkActionBar"&gt;
              &lt;item name="windowNoTitle"&gt;true&lt;/item&gt;
              &lt;item name="windowActionBar"&gt;false&lt;/item&gt;
              &lt;item name="colorPrimary"&gt;@color/colorPrimary&lt;/item&gt;
              &lt;item name="colorPrimaryDark"&gt;@color/colorPrimaryDark&lt;/item&gt;
              &lt;item name="colorAccent"&gt;@color/colorAccent&lt;/item&gt;
          &lt;/style&gt;
  </code></pre>
  
  <p>MainActivity:</p>
  
  <pre><code>public class MainActivity extends AppCompatActivity implements FragmentDrawer.FragmentDrawerListener,FragmentDrawerRight.FragmentDrawerListener1 {
  
      private static String TAG = MainActivity.class.getSimpleName();
  
  
      private Toolbar mToolbar;
      ViewPager viewPager;
      TabLayout tabLayout;
      private FragmentDrawer drawerFragment;
      private FragmentDrawerRight  drawerFragmentRight;
      private ImageView pone,ptwo;
      DrawerLayout drawer_layout;
      int mutedColor;
      private CollapsingToolbarLayout collapsing_toolbar;
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
  
          collapsing_toolbar  = (CollapsingToolbarLayout)findViewById(R.id.collapsing_toolbar);
          mToolbar = (Toolbar) findViewById(R.id.toolbar);
  
          setSupportActionBar(mToolbar);
  
          ptwo=(ImageView)mToolbar.findViewById(R.id.ptwo);
  
  
          drawerFragment = (FragmentDrawer)
                  getSupportFragmentManager().findFragmentById(R.id.fragment_navigation_drawer);
          drawerFragmentRight = (FragmentDrawerRight)
                  getSupportFragmentManager().findFragmentById(R.id.fragment_navigation_drawer1);
  
          drawer_layout = (DrawerLayout) findViewById(R.id.drawer_layout);
  
          drawerFragment.setUp(R.id.fragment_navigation_drawer, drawer_layout, mToolbar);
          drawerFragmentRight.setUp(R.id.fragment_navigation_drawer1, drawer_layout, mToolbar);
          drawerFragment.setDrawerListener(this);
          drawerFragmentRight.setDrawerListener(this);
  
          viewPager = (ViewPager) findViewById(R.id.viewpager);
          setupViewPager(viewPager);
  
          tabLayout = (TabLayout) findViewById(R.id.tabs);
          tabLayout.setupWithViewPager(viewPager);
  
          ptwo.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View v) {
                  if(!drawer_layout.isDrawerOpen(GravityCompat.END)){
                      drawer_layout.openDrawer(GravityCompat.END);
                      drawer_layout.closeDrawer(GravityCompat.START);
                  }
              }
          });
  
  
      }
  
      private void setupViewPager(ViewPager viewPager) {
          ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
          adapter.addFrag(new OneFragment(), "ONE");
          adapter.addFrag(new TwoFragment(), "TWO");
          adapter.addFrag(new ThreeFragment(), "THREE");
          adapter.addFrag(new FourFragment(), "FOUR");
          adapter.addFrag(new FiveFragment(), "FIVE");
          adapter.addFrag(new SixFragment(), "SIX");
          adapter.addFrag(new SevenFragment(), "SEVEN");
          adapter.addFrag(new EightFragment(), "EIGHT");
          adapter.addFrag(new NineFragment(), "NINE");
          adapter.addFrag(new TenFragment(), "TEN");
          viewPager.setAdapter(adapter);
      }
  
      class ViewPagerAdapter extends FragmentPagerAdapter {
          private final List&lt;Fragment&gt; mFragmentList = new ArrayList&lt;&gt;();
          private final List&lt;String&gt; mFragmentTitleList = new ArrayList&lt;&gt;();
  
          public ViewPagerAdapter(FragmentManager manager) {
              super(manager);
          }
  
          @Override
          public Fragment getItem(int position) {
              return mFragmentList.get(position);
          }
  
          @Override
          public int getCount() {
              return mFragmentList.size();
          }
  
          public void addFrag(Fragment fragment, String title) {
              mFragmentList.add(fragment);
              mFragmentTitleList.add(title);
          }
  
          @Override
          public CharSequence getPageTitle(int position) {
              return mFragmentTitleList.get(position);
          }
      }
  
      @Override
      public void onDrawerItemSelected(View view, int position) {
          displayView(position);
      }
  
      private void displayView(int position) {
          viewPager.setCurrentItem(position);
          getSupportActionBar().setTitle(getResources().getStringArray(R.array.nav_drawer_labels)[position]);
      }
  
      private void displayView1(int position) {
          Fragment fragment = null;
          String title = getString(R.string.app_name);
          switch (position) {
              case 0:
                  fragment = new OneFragment();
                  title = getString(R.string.title_home);
                  break;
              case 1:
                  fragment = new TwoFragment();
                  if (Build.VERSION.SDK_INT &gt;= 21) {
                      getWindow().setNavigationBarColor(getResources().getColor(R.color.colorAccent));
  
                  }
                  title = getString(R.string.title_friends);
  
  
                  break;
              case 2:
                  fragment = new ThreeFragment();
                  title = getString(R.string.title_messages);
                  break;
              case 3:
                  fragment = new FourFragment();
                  title = getString(R.string.title_home);
                  break;
              case 4:
                  fragment = new FiveFragment();
                  title = getString(R.string.title_friends);
                  break;
              case 5:
                  fragment = new SixFragment();
                  title = getString(R.string.title_messages);
                  break;
              default:
                  break;
          }
  
          if (fragment != null) {
  
              getSupportActionBar().setTitle(title);
          }
      }
  }
  </code></pre>
  
  <p>activty_main:</p>
  
  <pre><code>&lt;android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:app="http://schemas.android.com/apk/res-auto"
      xmlns:tools="http://schemas.android.com/tools"
      android:id="@+id/drawer_layout"
      android:layout_width="match_parent"
      android:layout_height="match_parent"&gt;
  
  
      &lt;LinearLayout
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:orientation="vertical"&gt;
          &lt;android.support.design.widget.AppBarLayout
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"&gt;
  
              &lt;android.support.design.widget.CollapsingToolbarLayout
                  android:id="@+id/collapsing_toolbar"
                  android:layout_width="match_parent"
                  android:layout_height="match_parent"
                  android:fitsSystemWindows="true"
                  app:contentScrim="?attr/colorPrimary"
                  app:expandedTitleMarginBottom="32dp"
                  app:expandedTitleMarginEnd="64dp"
                  app:expandedTitleMarginStart="48dp"
                  app:layout_scrollFlags="scroll|exitUntilCollapsed"&gt;
                  &lt;ImageView
                      android:id="@+id/header"
                      android:layout_width="match_parent"
                      android:layout_height="match_parent"
                      android:background="@drawable/ic_profile"
                      android:fitsSystemWindows="true"
                      android:scaleType="centerCrop"
                      app:layout_collapseMode="parallax" /&gt;
                  &lt;android.support.v7.widget.Toolbar
                      android:id="@+id/toolbar"
                      android:layout_width="match_parent"
                      android:layout_height="?attr/actionBarSize"
                      android:background="?attr/colorPrimary"
                      app:layout_scrollFlags="scroll|enterAlways"
                      app:popupTheme="@style/ThemeOverlay.AppCompat.Light" &gt;
                      &lt;LinearLayout
                          android:layout_width="match_parent"
                          android:layout_height="match_parent"
                          android:paddingRight="15dp"&gt;
  
                          &lt;TextView
                              android:layout_width="wrap_content"
                              android:layout_height="wrap_content"
                              android:layout_gravity="center_vertical"
                              android:gravity="center"
                              android:visibility="invisible"
                              android:textSize="20sp"
                              android:textColor="@android:color/white"
                              android:layout_weight="1"
                              android:text="Reload"
                              /&gt;
                          &lt;ImageView
                              android:id="@+id/ptwo"
                              android:layout_width="25dp"
                              android:layout_height="25dp"
                              android:layout_gravity="right|center_vertical"
                              android:src="@drawable/ic_profile"/&gt;
                      &lt;/LinearLayout&gt;
  
                  &lt;/android.support.v7.widget.Toolbar&gt;
              &lt;/android.support.design.widget.CollapsingToolbarLayout&gt;
  
  
  
  
              &lt;android.support.design.widget.TabLayout
                  android:id="@+id/tabs"
                  android:layout_width="match_parent"
                  android:layout_height="wrap_content"
                  app:tabMode="scrollable"/&gt;
          &lt;/android.support.design.widget.AppBarLayout&gt;
  
          &lt;android.support.v4.view.ViewPager
              android:id="@+id/viewpager"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              app:layout_behavior="@string/appbar_scrolling_view_behavior" /&gt;
          &lt;!--&lt;LinearLayout
              android:id="@+id/container_toolbar"
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:orientation="vertical"&gt;
  
              &lt;include
                  android:id="@+id/toolbar"
                  layout="@layout/toolbar" /&gt;
          &lt;/LinearLayout&gt;
  
          &lt;FrameLayout
              android:id="@+id/container_body"
              android:layout_width="fill_parent"
              android:layout_height="0dp"
              android:layout_weight="1" /&gt;
  --&gt;
  
      &lt;/LinearLayout&gt;
  
  
      &lt;fragment
          android:id="@+id/fragment_navigation_drawer"
          android:name="info.androidhive.materialdesign.activity.FragmentDrawer"
          android:layout_width="@dimen/nav_drawer_width"
          android:layout_height="match_parent"
          android:layout_gravity="start"
          app:layout="@layout/fragment_navigation_drawer"
          tools:layout="@layout/fragment_navigation_drawer" /&gt;
      &lt;fragment
          android:id="@+id/fragment_navigation_drawer1"
          android:name="info.androidhive.materialdesign.activity.FragmentDrawerRight"
          android:layout_width="@dimen/nav_drawer_width"
          android:layout_height="match_parent"
          android:layout_gravity="end"
          app:layout="@layout/fragment_navigation_drawer_right"
          tools:layout="@layout/fragment_navigation_drawer_right" /&gt;
  
  
  
  
  
  
  &lt;/android.support.v4.widget.DrawerLayout&gt;
  </code></pre>
  
  <p>OneFragment:</p>
  
  <p>public class OneFragment extends Fragment {</p>
  
  <pre><code> public HomeFragment() {
          // Required empty public constructor
      }
  
      @Override
      public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
  
      }
  
      @Override
      public View onCreateView(LayoutInflater inflater, ViewGroup container,
                               Bundle savedInstanceState) {
          View rootView = inflater.inflate(R.layout.fragment_home, container, false);
  
  
          // Inflate the layout for this fragment
          return rootView;
      }
  
      @Override
      public void onAttach(Activity activity) {
          super.onAttach(activity);
      }
  
      @Override
      public void onDetach() {
          super.onDetach();
      }
  }
  </code></pre>
  
  <p>fragment_home.xml:</p>
  
  <pre><code>&lt;RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:tools="http://schemas.android.com/tools"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:orientation="vertical"
      tools:context="info.androidhive.materialdesign.activity.HomeFragment"&gt;
  
  
      &lt;TextView
          android:id="@+id/label"
          android:layout_alignParentTop="true"
          android:layout_marginTop="100dp"
          android:layout_width="fill_parent"
          android:layout_height="wrap_content"
          android:gravity="center_horizontal"
          android:textSize="45dp"
          android:text="HOME"
          android:textStyle="bold"/&gt;
  
      &lt;TextView
          android:layout_below="@id/label"
          android:layout_centerInParent="true"
          android:layout_width="fill_parent"
          android:layout_height="wrap_content"
          android:textSize="12dp"
          android:layout_marginTop="10dp"
          android:gravity="center_horizontal"
          android:text="Edit fragment_home.xml to change the appearance" /&gt;
  
  &lt;/RelativeLayout&gt;
  </code></pre>
  
  <p>colors.xml:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;resources&gt;
      &lt;color name="colorPrimary"&gt;#303f95&lt;/color&gt;
      &lt;color name="colorPrimaryDark"&gt;#3f5185&lt;/color&gt;
      &lt;color name="textColorPrimary"&gt;#FFFFFF&lt;/color&gt;
      &lt;color name="windowBackground"&gt;#FFFFFF&lt;/color&gt;
      &lt;color name="navigationBarColor"&gt;#000000&lt;/color&gt;
      &lt;color name="colorAccent"&gt;#FF80AB&lt;/color&gt;
      &lt;color name="colorAccentt"&gt;#b6b6b6&lt;/color&gt;
      &lt;color name="primary"&gt;@color/blue_500&lt;/color&gt;
      &lt;color name="primaryDark"&gt;@color/blue_700&lt;/color&gt;
      &lt;color name="textPrimary"&gt;@color/text_white_text_icons_100&lt;/color&gt;
  &lt;/resources&gt;
  </code></pre>