Tuesday, 28 October 2014

Android Swipe Views with Tabs

In this post we are going to learn about how to integrate the android tab view with the fragments using ViewPager and ActionBar class. For displaying the tabs on the top of the screen you need to interact with the android action bar, this is because the tab views is connected with the action bar.
Ads by Google



In this example application we make three tabs called "java", "php" and ".Net" and there are three seperate fragement view for each of these tabs.

First you need to add the ViewPager into the activity_main.xml file.
 <android.support.v4.view.ViewPager  
   xmlns: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> 

Now create three layout files for the fragments.

1. java_layout.xml
 <?xml version="1.0" encoding="utf-8"?>  
 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
   android:layout_width="match_parent"  
   android:layout_height="match_parent"  
   android:background="#D881DA"  
    >  
   <TextView  
     android:id="@+id/textView1"  
     android:layout_width="wrap_content"  
     android:layout_height="wrap_content"  
     android:layout_alignParentTop="true"  
     android:layout_centerHorizontal="true"  
     android:layout_marginTop="58dp"  
     android:text="Welcome to java page"  
     android:textAppearance="?android:attr/textAppearanceLarge" />  
 </RelativeLayout>  

2. php_layout.xml
 <?xml version="1.0" encoding="utf-8"?>  
 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
   android:layout_width="match_parent"  
   android:layout_height="match_parent"   
   android:background="#0B9AE2"  
   >  
   <TextView  
     android:id="@+id/textView1"  
     android:layout_width="wrap_content"  
     android:layout_height="wrap_content"  
     android:layout_alignParentTop="true"  
     android:layout_centerHorizontal="true"  
     android:layout_marginTop="97dp"  
     android:text="Welcome to php page"  
     android:textAppearance="?android:attr/textAppearanceLarge" />  
 </RelativeLayout>  


3. dotnet_layout.xml
 <?xml version="1.0" encoding="utf-8"?>  
 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
   android:layout_width="match_parent"  
   android:layout_height="match_parent"  
   android:background="#E2D40B"  
    >  
   <TextView  
     android:id="@+id/textView1"  
     android:layout_width="wrap_content"  
     android:layout_height="wrap_content"  
     android:layout_alignParentTop="true"  
     android:layout_centerHorizontal="true"  
     android:layout_marginTop="42dp"  
     android:text="Welcome to .Net page"  
     android:textAppearance="?android:attr/textAppearanceLarge" />  
 </RelativeLayout>  


Now we need to inflate each of these layouts using the inflate() method.

1. JavaFragment.java
 package com.tabdemo;  
 import android.os.Bundle;  
 import android.support.v4.app.Fragment;  
 import android.view.LayoutInflater;  
 import android.view.View;  
 import android.view.ViewGroup;  
 public class JavaFragment extends Fragment {  
      @Override  
      public View onCreateView(LayoutInflater inflater, ViewGroup container,  
                Bundle savedInstanceState) {  
           // TODO Auto-generated method stub  
           return inflater.inflate(R.layout.java_layout, container,false);  
      }  
 }  

2.  PhpFragment.java
 package com.tabdemo;  
 import android.os.Bundle;  
 import android.support.v4.app.Fragment;  
 import android.view.LayoutInflater;  
 import android.view.View;  
 import android.view.ViewGroup;  
 public class PhpFragment extends Fragment {  
      @Override  
      public View onCreateView(LayoutInflater inflater, ViewGroup container,  
                Bundle savedInstanceState) {  
           // TODO Auto-generated method stub  
           return inflater.inflate(R.layout.php_layout, container,false);  
      }  
 }  


3. DotNetFragment.java
 package com.tabdemo;  
 import android.os.Bundle;  
 import android.support.v4.app.Fragment;  
 import android.view.LayoutInflater;  
 import android.view.View;  
 import android.view.ViewGroup;  
 public class DotnetFragment extends Fragment {  
      @Override  
      public View onCreateView(LayoutInflater inflater, ViewGroup container,  
                Bundle savedInstanceState) {  
           // TODO Auto-generated method stub  
           return inflater.inflate(R.layout.dotnet_layout, container,false);  
      }  
 }  


Now create an adapter class that extends FragmentPagerAdapter and define override method getItem. The getItem() method will return an object of fragment.

FragmentPageAdapter.java
 package com.tabdemo;  
 import android.support.v4.app.Fragment;  
 import android.support.v4.app.FragmentManager;  
 import android.support.v4.app.FragmentPagerAdapter;  
 public class FragmentPageAdapter extends FragmentPagerAdapter {  
      public FragmentPageAdapter(FragmentManager fm) {  
           super(fm);  
      }  
      @Override  
      public Fragment getItem(int arg0) {  
           // TODO Auto-generated method stub  
           switch (arg0) {  
           case 0:  
                  return new JavaFragment();  
           case 1:  
                  return new PhpFragment();  
           case 2:  
                 return new DotnetFragment();  
           default:  
                break;  
           }  
           return null;  
      }  
      @Override  
      public int getCount() {  
           // TODO Auto-generated method stub  
           return 3;  
      }  
 }  

MainActivity.java
 package com.tabdemo;  
 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{  
      ActionBar actionbar;  
      ViewPager viewpager;  
      FragmentPageAdapter ft;  
      @Override  
      protected void onCreate(Bundle savedInstanceState) {  
           super.onCreate(savedInstanceState);  
           setContentView(R.layout.activity_main);  
           viewpager = (ViewPager) findViewById(R.id.pager);  
           ft = new FragmentPageAdapter(getSupportFragmentManager());  
           actionbar = getActionBar();  
           viewpager.setAdapter(ft);  
           actionbar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);  
           actionbar.addTab(actionbar.newTab().setText("Java").setTabListener(this));  
           actionbar.addTab(actionbar.newTab().setText("Php").setTabListener(this));  
           actionbar.addTab(actionbar.newTab().setText(".Net").setTabListener(this));  
           viewpager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {  
                @Override  
                public void onPageSelected(int arg0) {  
                actionbar.setSelectedNavigationItem(arg0);  
                }  
                @Override  
                public void onPageScrolled(int arg0, float arg1, int arg2) {  
                     // TODO Auto-generated method stub  
                }  
                @Override  
                public void onPageScrollStateChanged(int arg0) {  
                     // TODO Auto-generated method stub  
                }  
           });  
      }  
      @Override  
      public void onTabReselected(Tab tab, FragmentTransaction ft) {  
           // TODO Auto-generated method stub  
      }  
      @Override  
      public void onTabSelected(Tab tab, FragmentTransaction ft) {  
           viewpager.setCurrentItem(tab.getPosition());  
      }  
      @Override  
      public void onTabUnselected(Tab tab, FragmentTransaction ft) {  
           // TODO Auto-generated method stub  
      }  
 }  


2 comments:

  1. Hi Prabeesh,

    Thank you for the video. It was very well explained and comprehensive. I followed it word to word to develop my app for a college project. The only issue is that I came across a null pointer exception and i am not able to implement the app on my emulator. Please suggest how I can solve this issue.

    ReplyDelete
  2. sir i have a problem that TabListener is getting cutted and my app is no opening to run

    ReplyDelete