Search code examples
androidtabsandroid-viewpagerandroid-tablayout

Android - onTabSelected not called at first time with customView


I am using a TabLayout combined with a ViewPager and i have set a custom view for my Tabs. Everything is working as expected when tabs are selected and unselected (the custom view is changed depending on the selected/unselected tab).
The problem is at first launch, the onTabSelected method is never called.
Here is my activity :

setupViewPager(viewPager);    
tabLayout.setupWithViewPager(viewPager);     

for (int i = 0; i < tabLayout.getTabCount(); i++) {
    tabLayout.getTabAt(i).setCustomView(getViewAt(i));
}

tabLayout.addOnTabSelectedListener(this);    

This is the setupViewPager and other methods :

private void setupViewPager(ViewPager viewPager) {
    MyFragmentPagerAdapter adapter = new MyFragmentPagerAdapter(getChildFragmentManager(), getContext(), startDate, endDate);
    viewPager.setAdapter(adapter);
}    

private View getViewAt(int i){
    final Date currentDate = CalendarUtility.addDay(startDate, i);
    View v = LayoutInflater.from(getContext()).inflate(R.layout.layout_custom_tab, null);
    TextView day = (TextView) v.findViewById(R.id.day);
    TextView num = (TextView) v.findViewById(R.id.num);
    day.setText(CalendarUtility.getDayOfWeek(currentDate));
    num.setText(CalendarUtility.getDayOfMonth(currentDate));
    return v;
}

@Override
public void onTabSelected(TabLayout.Tab tab) {
    Toast.makeText(getContext(), "Tab #"+tab.getPosition(), Toast.LENGTH_LONG).show();
    TextView num = (TextView)tab.getCustomView().findViewById(R.id.num);
    num.getBackground().setColorFilter(textDarkColor, PorterDuff.Mode.SRC_IN);
}

@Override
public void onTabUnselected(TabLayout.Tab tab) {
    TextView num = (TextView)tab.getCustomView().findViewById(R.id.num);
    num.getBackground().setColorFilter(pinkDarkColor, PorterDuff.Mode.SRC_IN);
}

Finally here is the behaviour when navigating through the tabs
https://i.sstatic.net/Scm9P.png

The tabs on first launch, no tab is selected
https://i.sstatic.net/a6Xgd.png

Can somebody help please ?


Solution

  • In your adapter class make two methods.

    public void setOnSelectedView(TabLayout tabLayout, int position) {
        TabLayout.Tab tab = tabLayout.getTabAt(position);
        if (tab != null) {
            View selected = tab.getCustomView();
            TextView num = (TextView)selected.findViewById(R.id.num);
    num.getBackground().setColorFilter(textDarkColor, PorterDuff.Mode.SRC_IN);
            tab.select();
        }
    }
    
    public void setunSelectedView(TabLayout tabLayout, int position) {
        TabLayout.Tab tab = tabLayout.getTabAt(position);
        if (tab != null) {
            View selected = tab.getCustomView();
            TextView num = (TextView)selected.findViewById(R.id.num);
    num.getBackground().setColorFilter(pinkDarkColor, PorterDuff.Mode.SRC_IN);
            tab.select();
        }
    }
    

    Now in onTabSelected and onTabUnselected method call these methods.

    And if you want to launch first selected tab then in onCreate() method call

    setOnSelectedView(0,tabLayout);
    

    Note : Another way is in getViewAt() method just check it position

       if(i==0){
         num.getBackground().setColorFilter(textDarkColor, PorterDuff.Mode.SRC_IN);
       }else{
         num.getBackground().setColorFilter(pinkDarkColor, PorterDuff.Mode.SRC_IN);
       }