DEVELOPMENT

Today, i'm going to share the code which will teach you how to implement a tab layout with swipe views for android application. Hope, you'll find the tutorial easy.

swipe tab views android

build.gradle
apply plugin: 'com.android.application'

android {
    compileSdkVersion 22
    buildToolsVersion "22.0.1"

    defaultConfig {
        applicationId "com.example.tut.tut_7_9"
        minSdkVersion 15
        targetSdkVersion 22
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:appcompat-v7:22.2.0'
}
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
 package="com.example.tut.tut_7_9" >

 <application
 android:allowBackup="true"
 android:icon="@mipmap/ic_launcher"
 android:label="@string/app_name" >
 <activity
 android:name=".MainActivity"
 android:label="@string/app_name" >
 <intent-filter>
 <action android:name="android.intent.action.MAIN" />
 <category android:name="android.intent.category.LAUNCHER" />
 </intent-filter>
 </activity>
 </application>

</manifest>
cAdapter.java - Create a new class cAdapter.java that extends FragmentStatePagerAdapter class. Here, it should be noted that we're making use of android support library package.
package com.example.tut.tut_7_9;


import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;


public  class cAdapter extends FragmentStatePagerAdapter{
    Fragment fragment;
    public cAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int i) {

        switch(i)
        {
            case 0 :fragment = new Fragment1();
                     break;
            case 1 : fragment = new Fragment2();
                     break;
            case 2 : fragment = new Fragment3();
                     break;

            default:break;

        }

      return fragment;
    }

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

  
}
activity_main.xml - In this xml file, add <ViewPager> element.
<?xml version="1.0" encoding="utf-8"?>
<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" />

MainActivity.java
package com.example.tut.tut_7_9;
package com.example.tut.tut_7_9;

import android.app.ActionBar;
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 {

    cAdapter cAdapter;
    ViewPager mViewPager;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        cAdapter =
                new cAdapter(
                        getSupportFragmentManager());
        mViewPager =  (ViewPager) findViewById(R.id.pager);
        mViewPager.setAdapter(cAdapter);
        mViewPager.setOnPageChangeListener(
                new ViewPager.SimpleOnPageChangeListener() {
                    @Override
                    public void onPageSelected(int position) {
                        getActionBar().setSelectedNavigationItem(position);
                    }
                });
       ActionBar actionBar = getActionBar();

        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

        ActionBar.TabListener tabListener = new ActionBar.TabListener() {
            public void onTabSelected(ActionBar.Tab tab, FragmentTransaction ft) {
               mViewPager.setCurrentItem(tab.getPosition());
            }

            public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction ft) {

            }

            public void onTabReselected(ActionBar.Tab tab, FragmentTransaction ft) {

            }
        };

        for (int i = 0; i < 3; i++) {
            actionBar.addTab(
                    actionBar.newTab()
                            .setText("Tab " + (i + 1))
                            .setTabListener(tabListener));
        }

    }
}
Now last step, create three classes and corresponding XML layout file for the 3 pages.

Fragment1.java
package com.example.tut.tut_7_9;

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



public  class Fragment1 extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater,
                             ViewGroup container, Bundle savedInstanceState) {

        View rootView = inflater.inflate(
                R.layout.activity_fragment1, container, false);
        return rootView;
    }
}
Fragment2.java
package com.example.tut.tut_7_9;

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


public  class Fragment2 extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater,
                             ViewGroup container, Bundle savedInstanceState) {

        View rootView = inflater.inflate(
                R.layout.activity_fragment2, container, false);
        return rootView;
    }
}
Fragment3.java
package com.example.tut.tut_7_9;

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



public  class Fragment3 extends Fragment {
      @Override
    public View onCreateView(LayoutInflater inflater,
                             ViewGroup container, Bundle savedInstanceState) {

        View rootView = inflater.inflate(
                R.layout.activity_fragment3, container, false);
        return rootView;
    }
}


activity_fragment1.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:tools="http://schemas.android.com/tools" 
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 tools:context="com.example.tut.tut_7_9.Fragment1">

 <TextView android:text="fragment1" 
 android:layout_width="wrap_content"
 android:layout_height="wrap_content" />

</RelativeLayout>

activity_fragment2.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:tools="http://schemas.android.com/tools" 
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 tools:context="com.example.tut.tut_7_9.Fragment2">

 <TextView android:text="fragment2" 
 android:layout_width="wrap_content"
 android:layout_height="wrap_content" />
</RelativeLayout>
activity_fragment3.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:tools="http://schemas.android.com/tools"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 tools:context="com.example.tut.tut_7_9.Fragment3">

 <TextView android:text="fragment3"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content" />

</RelativeLayout>


Leave a Comment  
Download Code(54)





All rights reserved, the content is copyrighted to the author.

RELATED POST