Search code examples
androidandroid-viewpagerandroid-tablayout

Extra tab in the scrollable tablayout


I have problem in tablayout section. i created a scrollable tablayout with view pager like in http://www.androidhive.info/2015/09/android-material-design-working-with-tabs/ and the i have seen an empty tab in the last. i can click that tab but nothing shows. But when i swipe the view pager it never enters in that tab.See there is an empty tab after the register tab

if this question already solved please give me a link in the comment... Thanks in ADVANCE...

Here is my code

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.event_home);
    final Loading loading = new Loading(this);
    loading.show();
    new Handler().postDelayed(new Runnable() {
        @Override
        public void run() {
            loading.cancel();
        }
    }, Constants.WAIT_TIME);
    toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);

    viewPager = (ViewPager) findViewById(R.id.viewpager);
    viewPager.setOffscreenPageLimit(7);
    Interpolator sInterpolator = new AccelerateInterpolator();
    try {
        Field mScroller;
        mScroller = ViewPager.class.getDeclaredField("mScroller");
        mScroller.setAccessible(true);
        DecelerateInterpolator decelerateInterpolator = new DecelerateInterpolator();
        FixedSpeedScroller scroller = new FixedSpeedScroller(viewPager.getContext(), decelerateInterpolator);
        scroller.SetDuration(500);
        mScroller.set(viewPager, scroller);
    } catch (NoSuchFieldException e) {
    } catch (IllegalArgumentException e) {
    } catch (IllegalAccessException e) {
    }
    setupViewPager(viewPager);
    tabLayout = (TabLayout) findViewById(R.id.tabs);
    tabLayout.setupWithViewPager(viewPager);
    setupTabIcons();

  private void setupTabIcons() {
    tabLayout.addTab(tabLayout.newTab());
    tabLayout.getTabAt(0).setCustomView(R.layout.event_tablayout);
    tabLayout.getTabAt(1).setCustomView(R.layout.event_tablayout);
    tabLayout.getTabAt(2).setCustomView(R.layout.event_tablayout);
    tabLayout.getTabAt(3).setCustomView(R.layout.event_tablayout);
    tabLayout.getTabAt(4).setCustomView(R.layout.event_tablayout);
    tabLayout.getTabAt(5).setCustomView(R.layout.event_tablayout);
    tabLayout.getTabAt(6).setCustomView(R.layout.event_tablayout);
    View tab1_view = tabLayout.getTabAt(0).getCustomView();
    View tab2_view = tabLayout.getTabAt(1).getCustomView();
    View tab3_view = tabLayout.getTabAt(2).getCustomView();
    View tab4_view = tabLayout.getTabAt(3).getCustomView();
    View tab5_view = tabLayout.getTabAt(4).getCustomView();
    View tab6_view = tabLayout.getTabAt(5).getCustomView();
    View tab7_view = tabLayout.getTabAt(6).getCustomView();
    TextView tab1_title = (TextView) tab1_view.findViewById(R.id.tabtitle);
    ImageView img1 = (ImageView) tab1_view.findViewById(R.id.tabicon);
    TextView tab2_title = (TextView) tab2_view.findViewById(R.id.tabtitle);
    ImageView img2 = (ImageView) tab2_view.findViewById(R.id.tabicon);
    TextView tab3_title = (TextView) tab3_view.findViewById(R.id.tabtitle);
    ImageView img3 = (ImageView) tab3_view.findViewById(R.id.tabicon);
    TextView tab4_title = (TextView) tab4_view.findViewById(R.id.tabtitle);
    ImageView img4 = (ImageView) tab4_view.findViewById(R.id.tabicon);
    TextView tab5_title = (TextView) tab5_view.findViewById(R.id.tabtitle);
    ImageView img5 = (ImageView) tab5_view.findViewById(R.id.tabicon);
    TextView tab6_title = (TextView) tab6_view.findViewById(R.id.tabtitle);
    ImageView img6 = (ImageView) tab6_view.findViewById(R.id.tabicon);
    TextView tab7_title = (TextView) tab7_view.findViewById(R.id.tabtitle);
    ImageView img7 = (ImageView) tab7_view.findViewById(R.id.tabicon);
    tab1_title.setText("DASHBOARD");
    img1.setImageResource(R.drawable.ic_home);
    tab2_title.setText("ABOUT");
    img2.setImageResource(R.drawable.ic_information);
    tab3_title.setText("QR CODE");
    img3.setImageResource(R.drawable.ic_qrcode);
    tab4_title.setText("UPDATES");
    img4.setImageResource(R.drawable.ic_announcement);
    tab5_title.setText("TEAM");
    img5.setImageResource(R.drawable.ic_team);
    tab6_title.setText(" CONTACT US");
    img6.setImageResource(R.drawable.ic_mobile_phone);
    tab7_title.setText("REGISTER");
    img7.setImageResource(R.drawable.ic_register);
}


public void setupViewPager(ViewPager viewPager) {
    Adapter_pager adapter = new Adapter_pager(getSupportFragmentManager());
    adapter.addFrag(new Dashboard(), "Dashboard");
    adapter.addFrag(new Details(), "Details");
    adapter.addFrag(new Qrcodedisplay(), "Qr codes");
    adapter.addFrag(new Update(), "Update");
    adapter.addFrag(new Teams(), "Team");
    adapter.addFrag(new Contact_Us(), "Contact");
    adapter.addFrag(new Event_register(), "Register");
    viewPager.setAdapter(adapter);
}

Solution

  • You can try to add this to your tabLayout

    tabLayout.setTabMode(TabLayout.MODE_FIXED); 
    

    And if this dont work please post ur xml of that activity and be sure u have set the width to tabLayout to match_parent.

    And i am not sure but give this a try in your code

     private void setupTabIcons() {
        tabLayout.addTab(tabLayout.newTab());
    .....
    }
    

    Delete that line, because its looks like u are adding a empty tab