You need to get a tab position in order to control the color. Here is an 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;
}
}
source
share