1

So I am "playing" around with some code i got from a tutorial on Tab layout with swipeable views..

My Problem is that I cannot figure out how to change the default tab it opens, when the app is launched.. Right now it starts in the 1st tab(Top Rated) but I want to have it start the second tab called Games?

I've browsed around and looked for solutions but doesn't seem to find one that fits my code..

Here is my main acitivity:

package info.androidhive.tabsswipe;

import info.androidhive.tabsswipe.adapter.TabsPagerAdapter;
import android.app.ActionBar;
import android.app.ActionBar.Tab;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;

public class MainActivity extends FragmentActivity implements
        ActionBar.TabListener {

    private ViewPager viewPager;
    private TabsPagerAdapter mAdapter;
    private ActionBar actionBar;
    // Tab titles
    private String[] tabs = { "Top Rated", "Games", "Movies" };

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

        // Initilization
        viewPager = (ViewPager) findViewById(R.id.pager);
        actionBar = getActionBar();
        mAdapter = new TabsPagerAdapter(getSupportFragmentManager());

        viewPager.setAdapter(mAdapter);
        actionBar.setHomeButtonEnabled(false);
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

        // Adding Tabs
        for (String tab_name : tabs) {
            actionBar.addTab(actionBar.newTab().setText(tab_name)
                    .setTabListener(this));

                }

        /**
         * on swiping the viewpager make respective tab selected
         * */
        viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {


            @Override
            public void onPageSelected(int position) {
                // on changing the page
                // make respected tab selected
                actionBar.setSelectedNavigationItem(position);
            }

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

            @Override
            public void onPageScrollStateChanged(int arg0) {
            }


        });


    }

    @Override
    public void onTabReselected(Tab tab, FragmentTransaction ft) {
    }

    @Override
    public void onTabSelected(Tab tab, FragmentTransaction ft) {
        // on tab selected
        // show respected fragment view
        viewPager.setCurrentItem(tab.getPosition());
    }

    @Override
    public void onTabUnselected(Tab tab, FragmentTransaction ft) {
    }

}
Bagge
  • 15
  • 3
  • My original answer did not use the override of addTab to specify the index of the tab itself and whether it is selected. Please refer to the most recent answer. – AmmarCSE Jul 21 '14 at 20:13

1 Answers1

0

In your addTab call, add the one you want set first followed by the rest. So change

// Adding Tabs
        for (String tab_name : tabs) {
            actionBar.addTab(actionBar.newTab().setText(tab_name)
                    .setTabListener(this));

                }

to

actionBar.addTab(actionBar.newTab().setText(tabs[1])
                    .setTabListener(this), 1, true);
actionBar.addTab(actionBar.newTab().setText(tabs[0])
                    .setTabListener(this), 0, false);
actionBar.addTab(actionBar.newTab().setText(tabs[2])
                    .setTabListener(this), 2, false);

See Android ActionBar tabs set initially selected tab

Community
  • 1
  • 1
AmmarCSE
  • 30,079
  • 5
  • 45
  • 53
  • Hi i see now. that i didn't ask the question right, because what i wanted was how to start the app in the middle tab.. But that error is on me.. is it possible you might know the answer to opening in the mid tab.. – Bagge Jul 21 '14 at 20:20
  • @Bagge Sure, how about you do viewPager.setCurrentItem(1); ? – AmmarCSE Jul 21 '14 at 20:32