0
  1. As bellow code indicate three Tab ,
  2. i want to change the tab colour when perticular tab click
  3. i tried all the setting background colour and also i follow all answer , but it is not helpfull
  4. please give me any solution for this. i want to show the tab colour should be different when clicking the tab

tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
      @Override
      public void onTabSelected(TabLayout.Tab tab) {
        if (tab.getPosition() == 0) {
          tabLayout.setSelectedTabIndicatorColor(Color.parseColor("#FFFFFF")); // setting colour but its not working 

          recyclerView.setBackgroundResource(R.drawable.bghomeback);
          titleText.setVisibility(View.GONE);
          titleText1.setVisibility(View.VISIBLE);

        
          ContentfulAdapter.getInstance().filter("");
          mSearchView.setVisibility(View.GONE);
          mSearchImage.setVisibility(View.GONE);
          mSearchText.setVisibility(View.GONE);
          mFavText.setVisibility(View.INVISIBLE);
          swipeRefresh.setOnRefreshListener(storyFragment::requestSync);
          swipeRefresh.setEnabled(true);
        } else if (tab.getPosition() == 1) {
          titleText.setVisibility(View.VISIBLE);
          titleText1.setVisibility(View.GONE);
         
         // mRelative.setBackgroundResource(Color.parseColor("#000000"));
          tabLayout.setSelectedTabIndicatorColor(Color.parseColor("#FFFFFF"));



          recyclerView.setBackgroundResource(R.drawable.seraback);
          ContentfulAdapter.getInstance().filter("");
          mSearchView.setVisibility(View.VISIBLE);

          mFavText.setVisibility(View.GONE);
          mSearchView.setQuery("", false);
          swipeRefresh.setRefreshing(false);
          swipeRefresh.setEnabled(false);
        } else if (tab.getPosition() == 2){
         
          recyclerView.setBackgroundResource(R.drawable.favback);
          tabLayout.setSelectedTabIndicatorColor(R.drawable.taitemselect1);
          titleText.setVisibility(View.VISIBLE);
          titleText1.setVisibility(View.GONE);
          //  mSearchImage.setVisibility(View.GONE);
          mSearchText.setVisibility(View.GONE);
          mSearchView.setVisibility(View.GONE);
//          ContentfulAdapter.getInstance().filter("");
//          swipeRefresh.setOnRefreshListener(storyFragment::requestSync);

          mFavText.setVisibility(View.VISIBLE);
          mFavText.setText("The books you mark as favourite will appear in this page.\n No favourite book added.");
          ContentfulAdapter.getInstance().showFavoriteItem();
          if (ContentfulAdapter.getInstance().getItemCount() < 1) {
            mFavText.setVisibility(View.VISIBLE);
            mFavText.setText("The books you mark as favourite will appear in this page.\n No favourite book added.");
          } else {
            mFavText.setVisibility(View.INVISIBLE);

          }

          swipeRefresh.setRefreshing(false);
          swipeRefresh.setEnabled(false);
        }
PRP
  • 101
  • 9

1 Answers1

3

ISSUE

The reason why this code isn't working is that actually you are not changing the tab background but the color of the indicator of the selected tab

SOLUTION

Since I'm not sure of what you're trying to achieve, I'll give you some useful information about TabLayout.

The method TabLayout.getTabCount() returns the number of tabs in your Layout;

the method TabLayout.getTabAt() returns an object of type TabLayout.Tab that represents the tab.

With these two methods you can iterate over all the tabs and use the method TabLayout.Tab.setCustomView(int) or TabLayout.Tab.setCustomView(View) to use a custom view for that tab, as explained here in the answer of MarcGV.

If you want a more dynamic solution you can use TabLayout.getSelectedTabPosition() to get the index of the selected tab and get the Tab object with the method mentioned before.

If you need to change just the color of each tab you can try to do the following:

for (int i = 0; i < tabLayout.getTabCount(); i++) {
   TabLayout.Tab tab = tabLayout.getTabAt(i);
   tab.getCustomView().setBackgroundColor(Color.parseColor(getRandomColor()));
}

I assume that the method getRandomColor() picks one value from 000000 to FFFFFF.

  • But please note that the third line really depends on what you have inside the Tabs. For example, if you're using TabLayout with fragments you have to get the view of the fragment. To achieve this take a look [here](https://stackoverflow.com/questions/31890843/get-fragment-by-tag-or-id-from-tablayout-android) – Andrea Biaggi Jan 23 '19 at 18:32
  • thanks it is working ,,, but after click another tab the colour is same for previous tabbut i want to remove that coulor – PRP Jan 24 '19 at 06:36
  • No,, its not working , My requirement is i have 4 tab i need to add different colour for each tab when i click – PRP Feb 05 '19 at 07:32
  • Ok so maybe you should try set background colour onCreateView() of your fragments. Have you already tried this? – Andrea Biaggi Feb 06 '19 at 08:07
  • To do this you have to do view.setBackgroundColor(Color) in fragment's onCreateView() after you have inflated your view of course – Andrea Biaggi Feb 08 '19 at 18:21
  • No its not working bro.....view.setBackgroundColor(Color) this is setting entire tab(4 tab) background color... – PRP Feb 11 '19 at 07:38