0

I have looked all round stackoverflow how i can add a badge in an icon of a tab in a tab layout, yet have no answer.

This is my code

//Get reference to your Tablayout
    TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
    tabLayout.setupWithViewPager(mViewPager);
     tabLayout.getTabAt(0).setIcon(ICONS[0]);
    tabLayout.getTabAt(1).setIcon(ICONS[1]);
    tabLayout.getTabAt(2).setIcon(ICONS[2]);

    BadgeView badge = new BadgeView(this, tabLayout.getTabAt(0).getCustomView());
    badge.setText("1"); //Whatever value you should add
    badge.show();


    BadgeView mMotification = new BadgeView(this, tabLayout.getChildAt(1));
    mMotification.setText("10");
    mMotification.show();

I have also tried many other alternatives but it seem like BadgeView takes only views

K.Os
  • 5,123
  • 8
  • 40
  • 95
Mcjohn Key
  • 15
  • 8

2 Answers2

0

You can create custom layout for the tabs and add them in TabLayout

Neha
  • 76
  • 11
0

This is what i did for it to work

TabLayout.Tab tab = tabLayout.getTabAt(0);
    ImageView imageView = new ImageView(this);
    imageView.setImageResource(R.drawable.ic_notifications);

    tab.setCustomView(imageView);
    BadgeView badge = new BadgeView(this, imageView);
    badge.setText("23");
    badge.setBadgeMargin(25,0);
    badge.show();
Mcjohn Key
  • 15
  • 8