3
\$\begingroup\$

I am using Tablayout for an Activity in my app but for some reason icons for each tab using tabLayout.getTabAt(position).setIcon(icon) are getting small.
Additionally the text is always in uppercase. So I decided to implement custom views for each tab to get the desired result.

It did work but the code is not up to the mark. So how do I optimize the below code?

private int[] tabIcons = {
        R.drawable.ic_job_list,
        R.drawable.ic_my_cv,
        R.drawable.ic_dashboard,
        R.drawable.ic_networks,
        R.drawable.ic_more
};

private int[] tabIconSelected = {
        R.drawable.ic_job_list_selected,
        R.drawable.ic_mycv_selected,
        R.drawable.ic_dashboard_selected,
        R.drawable.ic_networks_selected,
        R.drawable.ic_more_selected
};

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_dashboard);

    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    // Create the adapter that will return a fragment for each of the three
    // primary sections of the activity.
    mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());

    // Set up the ViewPager with the sections adapter.
    mViewPager = (ViewPager) findViewById(R.id.container);
    mViewPager.setAdapter(mSectionsPagerAdapter);

    tabLayout = (TabLayout) findViewById(R.id.tabs);
    tabLayout.setupWithViewPager(mViewPager);

    setupTabIcons();
}


private void setupTabIcons() {

    /*tabLayout.getTabAt(0).setIcon(tabIcons[0]);
    tabLayout.getTabAt(1).setIcon(tabIcons[1]);
    tabLayout.getTabAt(2).setIcon(tabIcons[2]);
    tabLayout.getTabAt(3).setIcon(tabIcons[3]);
    tabLayout.getTabAt(4).setIcon(tabIcons[4]);*/

    TextView tabOne = (TextView) LayoutInflater.from(this).inflate(R.layout.custom_tab, null);
    tabOne.setText("Jobs");
    tabLayout.getTabAt(0).setCustomView(tabOne);

    TextView tabTwo = (TextView) LayoutInflater.from(this).inflate(R.layout.custom_tab, null);
    tabTwo.setText("MyCV");
    tabLayout.getTabAt(1).setCustomView(tabTwo);

    TextView tabThree = (TextView) LayoutInflater.from(this).inflate(R.layout.custom_tab, null);
    tabThree.setText("Dashboard");
    tabLayout.getTabAt(2).setCustomView(tabThree);

    TextView tabFour = (TextView) LayoutInflater.from(this).inflate(R.layout.custom_tab, null);
    tabFour.setText("Networks");
    tabLayout.getTabAt(3).setCustomView(tabFour);

    TextView tabFive = (TextView) LayoutInflater.from(this).inflate(R.layout.custom_tab, null);
    tabFive.setText("More");
    tabLayout.getTabAt(4).setCustomView(tabFive);


    for (int i = 0; i < tabLayout.getTabCount(); i++) {
        if (i == 0) {
            ((TextView) tabLayout.getTabAt(0).getCustomView().findViewById(R.id.tab)).setTextColor(ContextCompat.getColor(DashboardActivity.this, R.color.colorAccent));
            ((TextView) tabLayout.getTabAt(0).getCustomView().findViewById(R.id.tab)).setCompoundDrawablesWithIntrinsicBounds(0, tabIconSelected[0], 0, 0);
        } else {
            ((TextView) tabLayout.getTabAt(i).getCustomView().findViewById(R.id.tab)).setTextColor(ContextCompat.getColor(DashboardActivity.this, R.color.dark_gray));
            ((TextView) tabLayout.getTabAt(i).getCustomView().findViewById(R.id.tab)).setCompoundDrawablesWithIntrinsicBounds(0, tabIcons[i], 0, 0);
        }
    }

    tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
        @Override
        public void onTabSelected(TabLayout.Tab tab) {

            ((TextView) tab.getCustomView().findViewById(R.id.tab)).setTextColor(ContextCompat.getColor(DashboardActivity.this, R.color.colorAccent));
            ((TextView) tab.getCustomView().findViewById(R.id.tab)).setCompoundDrawablesWithIntrinsicBounds(0, tabIconSelected[tab.getPosition()], 0, 0);
        }

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

            ((TextView) tab.getCustomView().findViewById(R.id.tab)).setTextColor(ContextCompat.getColor(DashboardActivity.this, R.color.dark_gray));
            ((TextView) tab.getCustomView().findViewById(R.id.tab)).setCompoundDrawablesWithIntrinsicBounds(0, tabIcons[tab.getPosition()], 0, 0);

        }

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

        }
    });

}
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.