Android Open Source - Android-Fixed-Tabs Main Activity






From Project

Back to project page Android-Fixed-Tabs.

License

The source code is released under:

This is free and unencumbered software released into the public domain. Anyone is free to copy, modify, publish, use, compile, sell, or distribute this software, either in source code form or as a co...

If you think the Android project Android-Fixed-Tabs listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.example.fixedtabs;
/*from   w w w .  j ava2 s  . co m*/
import android.app.ActionBar;
import android.app.AlertDialog;
import android.support.v4.app.FragmentManager;
import android.app.FragmentTransaction;
import android.content.DialogInterface;
import android.app.ActionBar.TabListener;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;

public class MainActivity extends FragmentActivity implements TabListener {

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

        viewPager = (ViewPager) findViewById(R.id.pager);

        viewPager.setAdapter(new MyAdapter(getSupportFragmentManager()));
        viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int arg0, float arg1, int arg2) {
                Log.d("Debug Log:", "onPageSelected at " 
        + "position " 
        + arg0 
        + "from " 
        + arg1
        + "with number of pixels= " 
        + arg2);
            }

            @Override
            public void onPageSelected(int arg0) {
                actionBar.setSelectedNavigationItem(arg0);
                Log.d("Debug Log:", "onPageSelected at " 
        + "position " 
        + arg0);
            }

            @Override
            public void onPageScrollStateChanged(int arg0) {
                if (arg0 == ViewPager.SCROLL_STATE_IDLE) {
                    Log.d("Debug Log:", "onPageScrollStateChanged Idle");
                }
                if (arg0 == ViewPager.SCROLL_STATE_DRAGGING) {
                    Log.d("Debug Log:", "onPageScrollStateChanged Dragging");
                }
                if (arg0 == ViewPager.SCROLL_STATE_SETTLING) {
                    Log.d("Debug Log:", "onPageScrollStateChanged Settling");
                }
            }
        });
        actionBar = getActionBar();
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

        ActionBar.Tab tab1 = actionBar.newTab();
        tab1.setText("Tab 1");
        tab1.setTabListener(this);

        ActionBar.Tab tab2 = actionBar.newTab();
        tab2.setText("Tab 2");
        tab2.setTabListener(this);

        ActionBar.Tab tab3 = actionBar.newTab();
        tab3.setText("Tab 3");
        tab3.setTabListener(this);

        actionBar.addTab(tab1);
        actionBar.addTab(tab2);
        actionBar.addTab(tab3);
    }

    @Override
    public void onTabSelected(ActionBar.Tab tab, FragmentTransaction ft) {
        Log.d("Debug Log:", "onTabSelected at " 
    + " position" 
    + tab.getPosition() 
    + " name " 
    + tab.getText());
        viewPager.setCurrentItem(tab.getPosition());
    }

    @Override
    public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction ft) {
        Log.d("Debug Log:", "onTabUnselected at " 
    + " position" 
    + tab.getPosition() 
    + " name " 
    + tab.getText());
    }

    @Override
    public void onTabReselected(ActionBar.Tab tab, FragmentTransaction ft) {
        Log.d("Debug Log:", "onTabReselected at " 
    + " position" 
    + tab.getPosition() 
    + " name " 
    + tab.getText());
    }

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

    //Used to make action bar items
  @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        super.onOptionsItemSelected(item);

        switch (item.getItemId()) {
            case R.id.action_about:
        //Opens alert dialog box
                aboutMenuItem();
                break;
            case R.id.action_settings:
        //Opens new settings activity
                Intent intent = new Intent(getApplicationContext(), Settings.class);
                startActivity(intent);
                break;
        }

        return super.onOptionsItemSelected(item);
    }

    public void aboutMenuItem() {
        new AlertDialog.Builder(this)
            .setTitle("Sample Message")
                .setMessage("Sample Message Text")
                .setNeutralButton("Sample Message Button Text", new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface arg0, int arg1) {

                    }
                }).show();
    }
}

class MyAdapter extends FragmentPagerAdapter {

    public MyAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int arg0) {
        Fragment fragment = null;
        if (arg0 == 0) {
            fragment = new FragmentA();
        }
        if (arg0 == 1) {
            fragment = new FragmentB();
        }
        if (arg0 == 2) {
            fragment = new FragmentC();
        }
        return fragment;
    }

    @Override
    public int getCount() {
        return 3;
    }
}




Java Source Code List

com.example.fixedtabs.FragmentA.java
com.example.fixedtabs.FragmentB.java
com.example.fixedtabs.FragmentC.java
com.example.fixedtabs.MainActivity.java
com.example.fixedtabs.Settings.java