I have this 2 arrays :final int[] tabSelected = {        R.drawable.iconWhite0,        R.drawable.iconWhite1,        R.drawable.iconWhite2,        R.drawable.iconWhite3,        R.drawable.iconWhite4};final int[] tabsUnselected = {        R.drawable.iconBlack0,        R.drawable.iconBlack1,        R.drawable.iconBlack2,        R.drawable.iconBlack3,        R.drawable.iconBlack4};I just don't understand how I can simplify this, by making a for loop ..although I am sure it is very easy, I just don't see it.   @Override        public void onPageSelected(int position) {            switch (position) {                case 0:                    tabLayout.getTabAt(0).setIcon(tabSelected[0]);                    tabLayout.getTabAt(1).setIcon(tabsUnselected[1]);                    tabLayout.getTabAt(2).setIcon(tabsUnselected[2]);                    tabLayout.getTabAt(3).setIcon(tabsUnselected[3]);                    tabLayout.getTabAt(4).setIcon(tabsUnselected[4]);                    break;                case 1:                    tabLayout.getTabAt(0).setIcon(tabsUnselected[0]);                    tabLayout.getTabAt(1).setIcon(tabSelected[1]);                    tabLayout.getTabAt(2).setIcon(tabsUnselected[2]);                    tabLayout.getTabAt(3).setIcon(tabsUnselected[3]);                    tabLayout.getTabAt(4).setIcon(tabsUnselected[4]);                    break;                case 2:                    tabLayout.getTabAt(0).setIcon(tabsUnselected[0]);                    tabLayout.getTabAt(1).setIcon(tabsUnselected[1]);                    tabLayout.getTabAt(2).setIcon(tabSelected[2]);                    tabLayout.getTabAt(3).setIcon(tabsUnselected[3]);                    tabLayout.getTabAt(4).setIcon(tabsUnselected[4]);                    break;                case 3:                    tabLayout.getTabAt(0).setIcon(tabsUnselected[0]);                    tabLayout.getTabAt(1).setIcon(tabsUnselected[1]);                    tabLayout.getTabAt(2).setIcon(tabsUnselected[2]);                    tabLayout.getTabAt(3).setIcon(tabSelected[3]);                    tabLayout.getTabAt(4).setIcon(tabsUnselected[4]);                    break;                default:                    tabLayout.getTabAt(0).setIcon(tabsUnselected[0]);                    tabLayout.getTabAt(1).setIcon(tabsUnselected[1]);                    tabLayout.getTabAt(2).setIcon(tabsUnselected[2]);                    tabLayout.getTabAt(3).setIcon(tabsUnselected[3]);                    tabLayout.getTabAt(4).setIcon(tabSelected[4]);                    break;            }        }
Setting icons when a tab is selected
java;array;android
You check whether the index of the tab equals the position. If this is the case, use the selected icon, otherwise use the unselected one:for(int i = 0; i < 5; i++) {  // maybe replace 5 with a constant declaration...    if(i == position)        tabLayout.getTabAt(i).setIcon(tabSelected[i]);    else        tabLayout.getTabAt(i).setIcon(tabsUnselected[i]);}