Wednesday, April 16, 2014

Android Custom Theme ActionBar with Tab example

Android Custom Theme ActionBar with Tab example

This article about Custom Theme Action Bar and Tab  with swipe view.
Create Viewpage view in xml .


ActivityMain.XML

<android.support.v4.view.ViewPagerxmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent">
</android.support.v4.view.ViewPager>







Create Fragment Page Adapter for handling multiple fragment


Fragment Page Adapter
publicclassTapFragmentManagerextendsFragmentPagerAdapter {

              publicTapFragmentManager(FragmentManagerfm) {
                     super(fm);
                     // TODO Auto-generated constructor stub
              }

              @Override
              public Fragment getItem(inttab_position) {
                     // TODO Auto-generated method stub
                    
                     if (tab_position == 0) {
                    
                     returnnewNewsFragment();
                       }
                     else { returnnewTopNewsFragment();
                       }
                    
                    

              }

              @Override
              publicintgetCount() {
                     // TODO Auto-generated method stub
                     returntabsName.length;
              }

       }


Create ViewPager and add the tab item to ActionBar



publicclassMainActivityextendsFragmentActivityimplementsTabListener {

       ViewPagerviewPager;

       TapFragmentManagerfragmentManger;
       android.app.ActionBaractionBar;

       String[] tabsName = { "News", "Top News" };

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

              viewPager = (ViewPager) findViewById(R.id.pager);
              actionBar = getActionBar();
              fragmentManger = newTapFragmentManager(getSupportFragmentManager());
              viewPager.setAdapter(fragmentManger);
              actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
              for (String str : tabsName) {
                     actionBar.addTab(actionBar.newTab().setText(str)
                                  .setTabListener(this));
              }

              viewPager.setOnPageChangeListener(newOnPageChangeListener() {

                     @Override
                     publicvoidonPageSelected(inttabPosition) {
                           actionBar.setSelectedNavigationItem(tabPosition);

                     }

                     @Override
                     publicvoidonPageScrolled(int arg0, float arg1, int arg2) {
                            // TODO Auto-generated method stub

                     }

                     @Override
                     publicvoidonPageScrollStateChanged(int arg0) {
                           // TODO Auto-generated method stub

                     }
              });
 

Set  current selected item to viewpager


@Override
       publicvoidonTabSelected(Tab tab, FragmentTransactionft) {
              // TODO Auto-generated method stub

              viewPager.setCurrentItem(tab.getPosition());

       }




Activity Full Source Code



importjava.util.ArrayList;
importjava.util.List;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.support.v7.app.ActionBar;
importandroid.app.ActionBar.Tab;
importandroid.app.ActionBar.TabListener;
importandroid.app.FragmentTransaction;
importandroid.os.Bundle;
importandroid.view.Menu;
importandroid.view.MenuItem;

publicclassMainActivityextendsFragmentActivityimplementsTabListener {

       ViewPagerviewPager;

       TapFragmentManagerfragmentManger;
       android.app.ActionBaractionBar;

       String[] tabsName = { "News", "Top News" };

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

              viewPager = (ViewPager) findViewById(R.id.pager);
              actionBar = getActionBar();
              fragmentManger = newTapFragmentManager(getSupportFragmentManager());
              viewPager.setAdapter(fragmentManger);
              actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
              for (String str : tabsName) {
                     actionBar.addTab(actionBar.newTab().setText(str)
                                  .setTabListener(this));
              }

              viewPager.setOnPageChangeListener(newOnPageChangeListener() {

                     @Override
                     publicvoidonPageSelected(inttabPosition) {
                           actionBar.setSelectedNavigationItem(tabPosition);

                     }

                     @Override
                     publicvoidonPageScrolled(int arg0, float arg1, int arg2) {
                           // TODO Auto-generated method stub

                     }

                     @Override
                     publicvoidonPageScrollStateChanged(int arg0) {
                            // TODO Auto-generated method stub

                     }
              });

       }

       @Override
       publicbooleanonCreateOptionsMenu(Menu menu) {

              // Inflate the menu; this adds items to the action bar if it is present.
              getMenuInflater().inflate(R.menu.main, menu);
              returntrue;
       }

       @Override
       publicbooleanonOptionsItemSelected(MenuItem item) {
              // Handle action bar item clicks here. The action bar will
              // automatically handle clicks on the Home/Up button, so long
              // as you specify a parent activity in AndroidManifest.xml.
              int id = item.getItemId();
              if (id == R.id.action_settings) {
                     returntrue;
              }
              returnsuper.onOptionsItemSelected(item);
       }

       @Override
       publicvoidonTabReselected(Tab tab, FragmentTransactionft) {
              // TODO Auto-generated method stub

       }

       @Override
       publicvoidonTabSelected(Tab tab, FragmentTransactionft) {
              // TODO Auto-generated method stub

              viewPager.setCurrentItem(tab.getPosition());

       }

       @Override
       publicvoidonTabUnselected(Tab tab, FragmentTransactionft) {
              // TODO Auto-generated method stub

       }

       publicclassTapFragmentManagerextendsFragmentPagerAdapter {

              publicTapFragmentManager(FragmentManagerfm) {
                     super(fm);
                     // TODO Auto-generated constructor stub
              }

              @Override
              public Fragment getItem(inttab_position) {
                     // TODO Auto-generated method stub

                     if (tab_position == 0) {

                           returnnewNewsFragment();
                     } else {
                           returnnewTopNewsFragment();
                     }

              }

              @Override
              publicintgetCount() {
                     // TODO Auto-generated method stub
                     returntabsName.length;
              }

       }
}


Fragment Source code
Create new fragment and set the view




importandroid.os.Bundle;
importandroid.view.LayoutInflater;
importandroid.view.View;
importandroid.view.ViewGroup;

publicclassNewsFragmentextends android.support.v4.app.Fragment{
      
       @Override
       public View onCreateView(LayoutInflaterinflater, ViewGroup container,
                     Bundle savedInstanceState) {
             
              View view = inflater.inflate(R.layout.fragment_main, container,false);
             
              return view;
       }

}


importandroid.os.Bundle;
import android.support.v4.app.Fragment;
importandroid.view.LayoutInflater;
importandroid.view.View;
importandroid.view.ViewGroup;

publicclassTopNewsFragmentextends Fragment {
      
       @Override
       public View onCreateView(LayoutInflaterinflater, ViewGroup container,
                     Bundle savedInstanceState) {
              View view = inflater.inflate(R.layout.fragment_main, container, false);
              return view;
       }

}

 





Download Source Code












Check out this may be help you

Related Posts Plugin for WordPress, Blogger...