Counter + Tablayout text

Asked

Viewed 198 times

1

I have a Tablayout with 4 options, the notification option is for notifications received via Firebase.

What I need is to add an unread notification counter. The code for counting unread notifications is already working perfectly. however the counter I would like to add above the Tab "Notifications".

Conforme Print.

inserir a descrição da imagem aqui

I tried to use tabLayout.getTabAt(0).setIcon(), but the result was not as expected.

Someone can help out?

Below my Main Activity

public class MainActivity extends AppCompatActivity {
private SharedPreferences settings;

String teste = "Tabelas";

private ViewPager mViewPager;
private CriaBanco banco;
private TabLayout tabLayout;
private ViewPager viewPager;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
   // mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());

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

    //mViewPager.setAdapter(mSectionsPagerAdapter);

    tabLayout = (TabLayout) findViewById(R.id.tabs);
    tabLayout.setupWithViewPager(viewPager);
    //tabLayout.setTabMode(TabLayout.MODE_SCROLLABLE); // Torna o Tabs em scroll
    //setupTabIcons();
}

private void setupTabIcons() {
    tabLayout.getTabAt(0).setIcon(R.drawable.ic_star_border_black_12dp);

}

private void createViewPager(ViewPager viewPager) {
    ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
    adapter.addFrag(new TabEntrada(), "MOINHO");
    adapter.addFrag(new TabTabelas(), "TABELAS");
    adapter.addFrag(new TabEstoque(), "ESTOQUE");
    adapter.addFrag(new TabNotificacao(), "NOTIFICAÇÕES");
    viewPager.setAdapter(adapter);
}




class ViewPagerAdapter extends FragmentPagerAdapter {
    private final List<Fragment> mFragmentList = new ArrayList<>();
    private final List<String> mFragmentTitleList = new ArrayList<>();

    public ViewPagerAdapter(FragmentManager manager) {
        super(manager);
    }

    @Override
    public Fragment getItem(int position) {
        return mFragmentList.get(position);
    }

    @Override
    public int getCount() {
        return mFragmentList.size();
    }

    public void addFrag(Fragment fragment, String title) {
        mFragmentList.add(fragment);
        mFragmentTitleList.add(title);
    }

    @Override
    public CharSequence getPageTitle(int position) {
        return mFragmentTitleList.get(position);
    }
}

}


So if I use tabLayout.getTabAt().setIcon() the result is the print Below.

inserir a descrição da imagem aqui

The icon is in the center, and pushes the title down.

If possible I’d like it to stay that way:

inserir a descrição da imagem aqui

  • If I understand your problem, this thread can solve it: http://stackoverflow.com/questions/31968162/android-tablayout-tabs-with-notification-badge-like-whatsapp

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.