0

I checked the following questions and not solved my problem:

How to add Run Time Tabs into TabHost in Android?

Create TabView at runtime?

adding tabs dynamically in android

// MainActivity.java

@TargetApi(Build.VERSION_CODES.HONEYCOMB)
public class MainActivity extends ActionBarActivity implements
        android.support.v7.app.ActionBar.TabListener {
    ViewPager viewPager;

    // Using appcompat action bar
    private android.support.v7.app.ActionBar actionBar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        viewPager = (ViewPager) findViewById(R.id.pager);
        FragmentManager fragmnetManager = getSupportFragmentManager();
        viewPager.setAdapter(new MyAdapter(fragmnetManager));

        viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener()
        {
            @Override
            public void onPageSelected(int pos)
            {
                actionBar.setSelectedNavigationItem(pos);
            }

            @Override
            public void onPageScrolled(int arg0, float arg1, int arg2)
            {

            }

            @Override
            public void onPageScrollStateChanged(int arg0) {

            }
        });

        // Getting actionbar
        actionBar = getSupportActionBar();

        // Setting navigation mode to actionbar
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

        // Now adding a new tab to action bar and setting title, icon and
        // implementing listener
        android.support.v7.app.ActionBar.Tab tab1 = actionBar.newTab();
        tab1.setText("TAB1");
        // tab1.setIcon(R.drawable.ic_launcher);
        tab1.setTabListener(this);

        android.support.v7.app.ActionBar.Tab tab2 = actionBar.newTab();
        tab2.setText("TAB2");
        tab2.setTabListener(this);

        android.support.v7.app.ActionBar.Tab tab3 = actionBar.newTab();
        tab3.setText("TAB3");
        tab3.setTabListener(this);

        // Now finally adding all tabs to actionbar
        actionBar.addTab(tab1);
        actionBar.addTab(tab2);
        actionBar.addTab(tab3);

    }

    @Override
    public void onTabReselected(android.support.v7.app.ActionBar.Tab arg0,
                                FragmentTransaction arg1)
    {

    }

    @Override
    public void onTabSelected(android.support.v7.app.ActionBar.Tab tab,
                              FragmentTransaction arg1)
    {

        // Setting current position of tab to view pager
        viewPager.setCurrentItem(tab.getPosition());
    }

    @Override
    public void onTabUnselected(android.support.v7.app.ActionBar.Tab arg0,
                                FragmentTransaction arg1) {

    }

}

// My adapter i.e. custom adapter for displaying fragments over view pager
class MyAdapter extends FragmentPagerAdapter {
    public MyAdapter(FragmentManager fm) {
        super(fm);

    }

    @Override
    public Fragment getItem(int i) {

        // Getting fragments according to selected position
        Fragment fragment = null;
        if (i == 0) {
            fragment = new FragmentA();
        }
        if (i == 1) {
            fragment = new FragmentB();
        }
        if (i == 2) {
            fragment = new FragmentC();
        }

        // and finally returning fragments
        return fragment;
    }

    @Override
    public int getCount() {

        // Returning no. of counts of fragments
        return 3;
    }
}

This code can provide 3 tabs with Fragments, I can add more tabs as well. But my problem is the number of tabs should depend on user input. Then how can I add or remove the tabs? Is it possible to add more tabs at runtime ?

Community
  • 1
  • 1
Sigma Equties
  • 67
  • 2
  • 10

1 Answers1

0

I solved my problem new code :

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    TabLayout tabLayout = (TabLayout) findViewById(R.id.tab_layout);
    tabLayout.addTab(tabLayout.newTab().setText("Tab 1"));
    tabLayout.addTab(tabLayout.newTab().setText("Tab 2"));
    tabLayout.addTab(tabLayout.newTab().setText("Tab 3"));
    tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);

    final ViewPager viewPager = (ViewPager) findViewById(R.id.pager);
    final PagerAdapter adapter = new PagerAdapter
            (getSupportFragmentManager(), tabLayout.getTabCount());
    viewPager.setAdapter(adapter);
    viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
    tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
        @Override
        public void onTabSelected(TabLayout.Tab tab) {
            viewPager.setCurrentItem(tab.getPosition());
        }

        @Override
        public void onTabUnselected(TabLayout.Tab tab) {

        }

        @Override
        public void onTabReselected(TabLayout.Tab tab) {

        }
    });
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}

}

Sigma Equties
  • 67
  • 2
  • 10