Search code examples
androidandroid-fragmentsandroid-tablayoutandroid-tabs

Set Fragment on Tab with multi line title


I'm working with two fragments that I'm setting on the Tabs.

enter image description here

I have two issues:

  1. I want to set the title of the fragment in multi line instead of "..."

here's the code to add fragment and set its title

ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
adapter.addFragment(new OneFragment(), "Current\nSubscription");
  1. I want to remove the extra space from around the icon and text on the tab to make it more compact and save some space.

Solution

  • Try this set Your TabLayout title using below code

    TextView tabOne = new TextView(this);
    tabOne.setText("Current"+ System.getProperty("line.separator") +"Subscription");
    tabOne.setCompoundDrawablesWithIntrinsicBounds(0, R.drawable.ic_tab_favourite, 0, 0);
    tabLayout.getTabAt(0).setCustomView(tabOne);