Kkk.
Kkk.

Reputation: 1

Java android tab

How to change a icon in selected tab? I do this for set icons in tab :

   private void createTabIcons() {
        TextView tabOne = (TextView) LayoutInflater.from(this).inflate(R.layout.custom_tab, null);
        tabOne.setCompoundDrawablesWithIntrinsicBounds(0, R.drawable.start_n, 0, 0);
        tabLayout.getTabAt(0).setCustomView(tabOne);
    }

But when I select or scroll tab I want to change a icon in selected tab and change to default icon in previesl tab

Upvotes: 1

Views: 66

Answers (1)

Ramana V V K
Ramana V V K

Reputation: 1251

if you are using material design then try this

       private int[] imageResIdcolor = {
                                    R.drawable.ic_dashboard,
                                    R.drawable.ic_chats,
                                    R.drawable.ic_friends,
                                    R.drawable.ic_contacts,
                            };

        private int[] imageResId = {
                            R.drawable.ic_dashbordblue,
                            R.drawable.ic_chatsblue,
                            R.drawable.ic_friendsblue,
                            R.drawable.ic_contactsblue,
                    };

        @Override
        public void onTabSelected(TabLayout.Tab tab) {

   tabLayout.getTabAt(tab.getPosition()).setIcon(imageResId[tab.getPosition()]);
              }

        @Override
        public void onTabUnselected(TabLayout.Tab tab) {
         tabLayout.getTabAt(tab.getPosition()).setIcon(imageResIdcolor[tab.getPosition()]);
             }

Upvotes: 1

Related Questions