Java Code Examples for android.app.ActionBar#addTab()

The following examples show how to use android.app.ActionBar#addTab() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MainActivity.java    From pixate-freestyle-android with Apache License 2.0 6 votes vote down vote up
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    ActionBar actionBar = getActionBar();
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
    Tab tab = actionBar.newTab().setText(R.string.tab1_title)
            .setTabListener(new TabListener(this, Fragment1.class.getName()));
    actionBar.addTab(tab);
    tab = actionBar.newTab().setText(R.string.tab2_title)
            .setTabListener(new TabListener(this, Fragment2.class.getName()));
    actionBar.addTab(tab);
    tab = actionBar.newTab().setText(R.string.tab3_title)
            .setTabListener(new TabListener(this, Fragment3.class.getName()));
    actionBar.addTab(tab);

    // Initiate Pixate
    PixateFreestyle.init(this);
}
 
Example 2
Source File: ActivityMain.java    From android with GNU General Public License v2.0 6 votes vote down vote up
private void cargaPestanas() {
	
	Resources res = getResources();
    
    ActionBar actionBar = getActionBar();
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
    
    // Crea las tabs
    ActionBar.Tab tab1 = actionBar.newTab().setText(res.getString(R.string.listado_gasolineras_title));
    ActionBar.Tab tab2 = actionBar.newTab().setText(res.getString(R.string.misubicaciones_title));
    
    // Crea cada Fragment para luego asociarla con la pestaña que corresponda
    Fragment fragmentoTab1 = new ListaGasolineras();
    Fragment fragmentoTab2 = new MisUbicaciones();
    
    // Asocia cada Fragment con su tab
    tab1.setTabListener(new TabsListener(fragmentoTab1));
    tab2.setTabListener(new TabsListener(fragmentoTab2));
    
    // Añade las tabs a la ActionBar
    actionBar.addTab(tab1);
    actionBar.addTab(tab2);
}
 
Example 3
Source File: ActivityMain.java    From android with GNU General Public License v2.0 6 votes vote down vote up
private void cargaPestanas() {
	
	Resources res = getResources();
    
    ActionBar actionBar = getActionBar();
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
    
    // Crea las tabs
    ActionBar.Tab tab1 = actionBar.newTab().setText(res.getString(R.string.listado_gasolineras_title));
    ActionBar.Tab tab2 = actionBar.newTab().setText(res.getString(R.string.misubicaciones_title));
    
    // Crea cada Fragment para luego asociarla con la pestaña que corresponda
    Fragment fragmentoTab1 = new ListaGasolineras();
    Fragment fragmentoTab2 = new MisUbicaciones();
    
    // Asocia cada Fragment con su tab
    tab1.setTabListener(new TabsListener(fragmentoTab1));
    tab2.setTabListener(new TabsListener(fragmentoTab2));
    
    // Añade las tabs a la ActionBar
    actionBar.addTab(tab1);
    actionBar.addTab(tab2);
}
 
Example 4
Source File: TabbedActivity.java    From holoaccent with Apache License 2.0 6 votes vote down vote up
protected void configureTabs(final ActionBar actionBar, ViewPager viewPager) {
	
	actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

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

	// For each of the sections in the app, add a tab to the action bar.
	for (int i = 0; i < mSectionsPagerAdapter.getCount(); i++) {
		Tab newTab = actionBar.newTab()
				.setText(mSectionsPagerAdapter.getPageTitle(i))
				.setTabListener(this);
		actionBar.addTab(newTab);
	}
}
 
Example 5
Source File: MainActivity.java    From mOrgAnd with GNU General Public License v2.0 6 votes vote down vote up
private void initViewPager(final ActionBar actionBar) {
    mSectionsPagerAdapter = new MainPagerAdapter(getSupportFragmentManager());
    mViewPager = (ViewPager) findViewById(R.id.pager);
    mViewPager.setAdapter(mSectionsPagerAdapter);

    mViewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
        @Override
        public void onPageSelected(int position) {
            actionBar.setSelectedNavigationItem(position);
        }
    });

    for (int i = 0; i < mSectionsPagerAdapter.getCount(); i++) {
        actionBar.addTab(
                actionBar.newTab()
                        .setText(mSectionsPagerAdapter.getPageTitle(i))
                        .setTabListener(this));
    }
}
 
Example 6
Source File: MainActivity.java    From AnyTime with Apache License 2.0 5 votes vote down vote up
@Override
protected void onCreate(Bundle savedInstanceState) {
	super.onCreate(savedInstanceState);
	setContentView(R.layout.activity_main);
	activity = this;
	// Set up the action bar.
	final ActionBar actionBar = getActionBar();
	actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

	// Create the adapter that will return a fragment for each of the three
	// primary sections of the app.
	mSectionsPagerAdapter = new SectionsPagerAdapter(
			getSupportFragmentManager());

	// Set up the ViewPager with the sections adapter.
	mViewPager = (ViewPager) findViewById(R.id.pager);
	mViewPager.setAdapter(mSectionsPagerAdapter);

	// When swiping between different sections, select the corresponding
	// tab. We can also use ActionBar.Tab#select() to do this if we have
	// a reference to the Tab.
	mViewPager
			.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
				@Override
				public void onPageSelected(int position) {
					actionBar.setSelectedNavigationItem(position);
				}
			});

	// For each of the sections in the app, add a tab to the action bar.
	for (int i = 0; i < mSectionsPagerAdapter.getCount(); i++) {
		// Create a tab with text corresponding to the page title defined by
		// the adapter. Also specify this Activity object, which implements
		// the TabListener interface, as the callback (listener) for when
		// this tab is selected.
		actionBar.addTab(actionBar.newTab()
				.setText(mSectionsPagerAdapter.getPageTitle(i))
				.setTabListener(this));
	}
}
 
Example 7
Source File: MainActivity.java    From androidtestdebug with MIT License 5 votes vote down vote up
@Override
  public void onCreate(Bundle savedInstanceState) {		
StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
	.detectActivityLeaks()
	.penaltyLog().build());

      super.onCreate(savedInstanceState);

      if(savedInstanceState != null && savedInstanceState.getInt("theme", -1) != -1) {
          mThemeId = savedInstanceState.getInt("theme");
          this.setTheme(mThemeId);
      }
              
      setContentView(R.layout.main);

      Directory.initializeDirectory(new OnDirectoryChanged());
      
      ActionBar bar = getActionBar();

      int i;
      for (i = 0; i < Directory.getCategoryCount(); i++) {
          bar.addTab(bar.newTab().setText(Directory.getCategory(i).getName())
                  .setTabListener(this));
      }

      mActionBarView = getLayoutInflater().inflate(
              R.layout.action_bar_custom, null);

      bar.setCustomView(mActionBarView);
      bar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM | ActionBar.DISPLAY_USE_LOGO);
      bar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
      bar.setDisplayShowHomeEnabled(true);

      // If category is not saved to the savedInstanceState,
      // 0 is returned by default.
      if(savedInstanceState != null) {
          int category = savedInstanceState.getInt("category");
          bar.selectTab(bar.getTabAt(category));
      }
  }
 
Example 8
Source File: FragmentTabs.java    From codeexamples-android with Eclipse Public License 1.0 5 votes vote down vote up
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    final ActionBar bar = getActionBar();
    bar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
    bar.setDisplayOptions(0, ActionBar.DISPLAY_SHOW_TITLE);

    bar.addTab(bar.newTab()
            .setText("Simple")
            .setTabListener(new TabListener<FragmentStack.CountingFragment>(
                    this, "simple", FragmentStack.CountingFragment.class)));
    bar.addTab(bar.newTab()
            .setText("Contacts")
            .setTabListener(new TabListener<LoaderCursor.CursorLoaderListFragment>(
                    this, "contacts", LoaderCursor.CursorLoaderListFragment.class)));
    bar.addTab(bar.newTab()
            .setText("Apps")
            .setTabListener(new TabListener<LoaderCustom.AppListFragment>(
                    this, "apps", LoaderCustom.AppListFragment.class)));
    bar.addTab(bar.newTab()
            .setText("Throttle")
            .setTabListener(new TabListener<LoaderThrottle.ThrottledLoaderListFragment>(
                    this, "throttle", LoaderThrottle.ThrottledLoaderListFragment.class)));

    if (savedInstanceState != null) {
        bar.setSelectedNavigationItem(savedInstanceState.getInt("tab", 0));
    }
}
 
Example 9
Source File: GreenDaoSampleActivity.java    From android-opensource-library-56 with Apache License 2.0 5 votes vote down vote up
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_green_dao_sample);
    // appcompatに対応すると2.1から利用出来る
    final ActionBar actionBar = getActionBar();
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

    mDaoSession = ((GreenDaoSampleApplication) getApplication())
            .getDaoSession();

    mSectionsPagerAdapter = new SectionsPagerAdapter(
            getSupportFragmentManager());

    mViewPager = (ViewPager) findViewById(R.id.pager);
    mViewPager.setAdapter(mSectionsPagerAdapter);

    mViewPager
            .setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
                @Override
                public void onPageSelected(int position) {
                    actionBar.setSelectedNavigationItem(position);
                }
            });
    for (int i = 0; i < mSectionsPagerAdapter.getCount(); i++) {
        actionBar.addTab(actionBar.newTab()
                .setText(mSectionsPagerAdapter.getPageTitle(i))
                .setTabListener(this));
    }

}
 
Example 10
Source File: HostActivity.java    From blog-nested-fragments-backstack with Apache License 2.0 5 votes vote down vote up
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_host);

    // Set up the action bar.
    final ActionBar actionBar = getActionBar();
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

    // Create the adapter that will return a fragment for each of the three
    // primary sections of the activity.
    mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());

    // Set up the ViewPager with the sections adapter.
    mViewPager = (ViewPager) findViewById(R.id.pager);
    mViewPager.setAdapter(mSectionsPagerAdapter);


    // When swiping between different sections, select the corresponding
    // tab. We can also use ActionBar.Tab#select() to do this if we have
    // a reference to the Tab.
    mViewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
        @Override
        public void onPageSelected(int position) {
            actionBar.setSelectedNavigationItem(position);
        }
    });

    // For each of the sections in the app, add a tab to the action bar.
    for (int i = 0; i < mSectionsPagerAdapter.getCount(); i++) {
        // Create a tab with text corresponding to the page title defined by
        // the adapter. Also specify this Activity object, which implements
        // the TabListener interface, as the callback (listener) for when
        // this tab is selected.
        actionBar.addTab(
                actionBar.newTab()
                        .setText(mSectionsPagerAdapter.getPageTitle(i))
                        .setTabListener(this));
    }
}
 
Example 11
Source File: ParallaxEverywhereSample.java    From ParallaxEverywhere with MIT License 5 votes vote down vote up
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_parallax_everywhere_sample);

    // Set up the action bar.
    final ActionBar actionBar = getActionBar();
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

    // Create the adapter that will return a fragment for each of the three
    // primary sections of the activity.
    mSectionsPagerAdapter = new SectionsPagerAdapter(getFragmentManager());

    // Set up the ViewPager with the sections adapter.
    mViewPager = (ViewPager) findViewById(R.id.pager);
    mViewPager.setAdapter(mSectionsPagerAdapter);

    // When swiping between different sections, select the corresponding
    // tab. We can also use ActionBar.Tab#select() to do this if we have
    // a reference to the Tab.
    mViewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
        @Override
        public void onPageSelected(int position) {
            actionBar.setSelectedNavigationItem(position);
        }
    });

    // For each of the sections in the app, add a tab to the action bar.
    for (int i = 0; i < mSectionsPagerAdapter.getCount(); i++) {
        // Create a tab with text corresponding to the page title defined by
        // the adapter. Also specify this Activity object, which implements
        // the TabListener interface, as the callback (listener) for when
        // this tab is selected.
        actionBar.addTab(
                actionBar.newTab()
                        .setText(mSectionsPagerAdapter.getPageTitle(i))
                        .setTabListener(this));
    }
}
 
Example 12
Source File: MainActivity.java    From androidtestdebug with MIT License 5 votes vote down vote up
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    if(savedInstanceState != null && savedInstanceState.getInt("theme", -1) != -1) {
        mThemeId = savedInstanceState.getInt("theme");
        this.setTheme(mThemeId);
    }
            
    setContentView(R.layout.main);

    Directory.initializeDirectory(new OnDirectoryChanged());
    
    ActionBar bar = getActionBar();

    int i;
    for (i = 0; i < Directory.getCategoryCount(); i++) {
        bar.addTab(bar.newTab().setText(Directory.getCategory(i).getName())
                .setTabListener(this));
    }

    mActionBarView = getLayoutInflater().inflate(
            R.layout.action_bar_custom, null);

    bar.setCustomView(mActionBarView);
    bar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM | ActionBar.DISPLAY_USE_LOGO);
    bar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
    bar.setDisplayShowHomeEnabled(true);

    // If category is not saved to the savedInstanceState,
    // 0 is returned by default.
    if(savedInstanceState != null) {
        int category = savedInstanceState.getInt("category");
        bar.selectTab(bar.getTabAt(category));
    }
}
 
Example 13
Source File: ViewPagerActivity.java    From SensorTag-CC2650 with Apache License 2.0 5 votes vote down vote up
public void addSection(Fragment fragment, String title) {
  final ActionBar actionBar = getActionBar();
  mFragmentList.add(fragment);
  mTitles.add(title);
  actionBar.addTab(actionBar.newTab().setText(title).setTabListener(tabListener));
  notifyDataSetChanged();
  // Log.d(TAG, "Tab: " + title);
}
 
Example 14
Source File: ResultActivity.java    From bankomatinfos with GNU General Public License v3.0 4 votes vote down vote up
@Override
protected void onCreate(Bundle savedInstanceState) {
	super.onCreate(savedInstanceState);
	setContentView(R.layout.activity_result);
	_showQuickLog = (_controller.getCardInfoNullSafe(this).getQuickLog()
			.size() > 0);
	_showEmvLog = _controller.getCardInfoNullSafe(this).isEmvCard();
	if (_showEmvLog && _showQuickLog) {
		_numLogTabs = 2;
	} else {
		_numLogTabs = 1;
	}
	_fragmentResultInfos = new ResultInfosListFragment();
	if (_showEmvLog) {
		_fragmentResultEmxTxList = new ResultEmvTxListFragment();
	}
	if (_showQuickLog) {
		_fragmentResultQuickTxList = new ResultQuickTxListFragment();
	}
	_fragmentResultLog = new ResultLogFragment();

	// Set up the action bar.
	final ActionBar actionBar = getActionBar();
	actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

	// Create the adapter that will return a fragment for each of the
	// primary sections of the app.
	_sectionsPagerAdapter = new SectionsPagerAdapter(
			getSupportFragmentManager());

	// Set up the ViewPager with the sections adapter.
	_viewPager = (ViewPager) findViewById(R.id.pager);
	_viewPager.setAdapter(_sectionsPagerAdapter);

	// When swiping between different sections, select the corresponding
	// tab. We can also use ActionBar.Tab#select() to do this if we have
	// a reference to the Tab.
	_viewPager
			.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
				@Override
				public void onPageSelected(int position) {
					actionBar.setSelectedNavigationItem(position);
				}
			});

	// For each of the sections in the app, add a tab to the action bar.
	for (int i = 0; i < _sectionsPagerAdapter.getCount(); i++) {
		// Create a tab with text corresponding to the page title defined by
		// the adapter. Also specify this Activity object, which implements
		// the TabListener interface, as the callback (listener) for when
		// this tab is selected.
		actionBar.addTab(actionBar.newTab()
				.setText(_sectionsPagerAdapter.getPageTitle(i))
				.setTabListener(this));
	}
}
 
Example 15
Source File: MainActivity.java    From androidtestdebug with MIT License 4 votes vote down vote up
@Override
public void addingNew(DirectoryCategory category) {
       ActionBar bar = getActionBar();
          bar.addTab(bar.newTab().setText(category.getName())
                  .setTabListener(getActivity()));
}
 
Example 16
Source File: MainActivity.java    From BTChat with GNU General Public License v3.0 4 votes vote down vote up
/*****************************************************
 *	 Overrided methods
 ******************************************************/

@Override
protected void onCreate(Bundle savedInstanceState) {
	super.onCreate(savedInstanceState);
	
	//----- System, Context
	mContext = this;	//.getApplicationContext();
	mActivityHandler = new ActivityHandler();
	AppSettings.initializeAppSettings(mContext);
	
	setContentView(R.layout.activity_main);

	// Set up the action bar.
	final ActionBar actionBar = getActionBar();
	actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

	// Create the adapter that will return a fragment for each of the primary sections of the app.
	mFragmentManager = getSupportFragmentManager();
	mSectionsPagerAdapter = new FragmentAdapter(mFragmentManager, mContext, this, mActivityHandler);

	// Set up the ViewPager with the sections adapter.
	mViewPager = (ViewPager) findViewById(R.id.pager);
	mViewPager.setAdapter(mSectionsPagerAdapter);

	// When swiping between different sections, select the corresponding tab.
	mViewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
		@Override
		public void onPageSelected(int position) {
			actionBar.setSelectedNavigationItem(position);
		}
	});

	// For each of the sections in the app, add a tab to the action bar.
	for (int i = 0; i < mSectionsPagerAdapter.getCount(); i++) {
		// Create a tab with text corresponding to the page title defined by the adapter.
		actionBar.addTab(actionBar.newTab()
				.setText(mSectionsPagerAdapter.getPageTitle(i))
				.setTabListener(this));
	}
	
	// Setup views
	mImageBT = (ImageView) findViewById(R.id.status_title);
	mImageBT.setImageDrawable(getResources().getDrawable(android.R.drawable.presence_invisible));
	mTextStatus = (TextView) findViewById(R.id.status_text);
	mTextStatus.setText(getResources().getString(R.string.bt_state_init));
	
	// Do data initialization after service started and binded
	doStartService();
}
 
Example 17
Source File: MainActivity.java    From BLEChat with GNU General Public License v3.0 4 votes vote down vote up
/*****************************************************
 *	 Overrided methods
 ******************************************************/

@Override
protected void onCreate(Bundle savedInstanceState) {
	super.onCreate(savedInstanceState);
	
	//----- System, Context
	mContext = this;	//.getApplicationContext();
	mActivityHandler = new ActivityHandler();
	AppSettings.initializeAppSettings(mContext);
	
	setContentView(R.layout.activity_main);

	// Set up the action bar.
	final ActionBar actionBar = getActionBar();
	actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

	// Create the adapter that will return a fragment for each of the primary sections of the app.
	mFragmentManager = getSupportFragmentManager();
	mSectionsPagerAdapter = new FragmentAdapter(mFragmentManager, mContext, this, mActivityHandler);

	// Set up the ViewPager with the sections adapter.
	mViewPager = (ViewPager) findViewById(R.id.pager);
	mViewPager.setAdapter(mSectionsPagerAdapter);

	// When swiping between different sections, select the corresponding tab.
	mViewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
		@Override
		public void onPageSelected(int position) {
			actionBar.setSelectedNavigationItem(position);
		}
	});

	// For each of the sections in the app, add a tab to the action bar.
	for (int i = 0; i < mSectionsPagerAdapter.getCount(); i++) {
		// Create a tab with text corresponding to the page title defined by the adapter.
		actionBar.addTab(actionBar.newTab()
				.setText(mSectionsPagerAdapter.getPageTitle(i))
				.setTabListener(this));
	}
	
	// Setup views
	mImageBT = (ImageView) findViewById(R.id.status_title);
	mImageBT.setImageDrawable(getResources().getDrawable(android.R.drawable.presence_invisible));
	mTextStatus = (TextView) findViewById(R.id.status_text);
	mTextStatus.setText(getResources().getString(R.string.bt_state_init));
	
	// Do data initialization after service started and binded
	doStartService();
}
 
Example 18
Source File: MainActivity.java    From remoteyourcam-usb with Apache License 2.0 4 votes vote down vote up
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    if (AppConfig.LOG) {
        Log.i(TAG, "onCreate");
    }

    getWindow().setFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON,
            WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

    if (!getResources().getConfiguration().isLayoutSizeAtLeast(Configuration.SCREENLAYOUT_SIZE_LARGE)) {
        getWindow()
                .setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
    } else {
        isLarge = true;
    }

    setContentView(R.layout.session);

    settings = new AppSettings(this);

    ActionBar bar = getActionBar();

    bar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
    bar.setDisplayHomeAsUpEnabled(false);
    bar.addTab(bar.newTab().setText("Session").setTabListener(new MyTabListener(new TabletSessionFragment())));
    bar.addTab(bar.newTab().setText("Gallery").setTabListener(new MyTabListener(new GalleryFragment())));

    int appVersionCode = -1;
    try {
        appVersionCode = getPackageManager().getPackageInfo(getPackageName(), 0).versionCode;
    } catch (NameNotFoundException e) {
        // nop
    }

    if (settings.showChangelog(appVersionCode)) {
        showChangelog();
    }

    ptp = PtpService.Singleton.getInstance(this);
}
 
Example 19
Source File: MainActivity.java    From androidtestdebug with MIT License 4 votes vote down vote up
@Override
public void addingNew(DirectoryCategory category) {
       ActionBar bar = getActionBar();
          bar.addTab(bar.newTab().setText(category.getName())
                  .setTabListener(getActivity()));
}
 
Example 20
Source File: MainActivity.java    From codeexamples-android with Eclipse Public License 1.0 4 votes vote down vote up
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    // Create the adapter that will return a fragment for each of the three primary sections
    // of the app.
    mAppSectionsPagerAdapter = new AppSectionsPagerAdapter(getSupportFragmentManager());

    // Set up the action bar.
    final ActionBar actionBar = getActionBar();

    // Specify that the Home/Up button should not be enabled, since there is no hierarchical
    // parent.
    actionBar.setHomeButtonEnabled(false);

    // Specify that we will be displaying tabs in the action bar.
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

    // Set up the ViewPager, attaching the adapter and setting up a listener for when the
    // user swipes between sections.
    mViewPager = (ViewPager) findViewById(R.id.pager);
    mViewPager.setAdapter(mAppSectionsPagerAdapter);
    mViewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
        @Override
        public void onPageSelected(int position) {
            // When swiping between different app sections, select the corresponding tab.
            // We can also use ActionBar.Tab#select() to do this if we have a reference to the
            // Tab.
            actionBar.setSelectedNavigationItem(position);
        }
    });

    // For each of the sections in the app, add a tab to the action bar.
    for (int i = 0; i < mAppSectionsPagerAdapter.getCount(); i++) {
        // Create a tab with text corresponding to the page title defined by the adapter.
        // Also specify this Activity object, which implements the TabListener interface, as the
        // listener for when this tab is selected.
        actionBar.addTab(
                actionBar.newTab()
                        .setText(mAppSectionsPagerAdapter.getPageTitle(i))
                        .setTabListener(this));
    }
}