Advanced Android - Tabs



Switching between different views in your app via tabs is not a new concept to material design and they are equally at home as a top level navigation pattern

Example

This example demostrate about how to integrate Android tabs.

Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project.

Step 2 − Add the following code in build.gradle.

dependencies {
   compile fileTree(dir: 'libs', include: ['*.jar'])
   testCompile 'junit:junit:4.12'
   compile 'com.android.support:appcompat-v7:24.2.0'
   compile 'com.android.support:design:23.2.0'
}

Step 3 − Add the following code to res/layout/activity_main.xml.

<?xml version = "1.0" encoding = "utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android = "
   http://schemas.android.com/apk/res/android"
   xmlns:app = "http://schemas.android.com/apk/res-auto"
   android:layout_width = "match_parent"
   android:layout_height = "match_parent">
   <android.support.design.widget.AppBarLayout
      android:layout_width = "match_parent"
      android:layout_height = "wrap_content"
      android:theme = "@style/ThemeOverlay.AppCompat.Dark.ActionBar">
      <android.support.v7.widget.Toolbar
         android:id = "@+id/toolbar"
         android:layout_width = "match_parent"
         android:layout_height = "?attr/actionBarSize"
         android:background = "?attr/colorPrimary"
         app:layout_scrollFlags = "scroll|enterAlways"
         app:popupTheme = "@style/ThemeOverlay.AppCompat.Light" />
      <android.support.design.widget.TabLayout
         android:id = "@+id/tabs"
         android:layout_width = "match_parent"
         android:layout_height = "wrap_content"
         app:tabMode = "fixed"
         app:tabGravity = "fill"/>
   </android.support.design.widget.AppBarLayout>
   <android.support.v4.view.ViewPager
      android:id = "@+id/viewpager"
      android:layout_width = "match_parent"
      android:layout_height = "match_parent"
      app:layout_behavior = "@string/appbar_scrolling_view_behavior" />
</android.support.design.widget.CoordinatorLayout>

Step 4 − Add the following code to src/MainActivity.java

package myapplication.example.com.myapplication;

import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {
   private Toolbar toolbar;
   private TabLayout tabLayout;
   private ViewPager viewPager;
   
   @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      toolbar = (Toolbar) findViewById(R.id.toolbar);
      
      setSupportActionBar(toolbar);
      getSupportActionBar().setDisplayHomeAsUpEnabled(true);

      viewPager = (ViewPager) findViewById(R.id.viewpager);
      setupViewPager(viewPager);

      tabLayout = (TabLayout) findViewById(R.id.tabs);
      tabLayout.setupWithViewPager(viewPager);
   } 
   
   private void setupViewPager(ViewPager viewPager) {
      ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
      adapter.addFragment(new OneFragment(), "ONE");
      adapter.addFragment(new TwoFragment(), "TWO");
      adapter.addFragment(new ThreeFragment(), "THREE");
      viewPager.setAdapter(adapter); 
   } 
   
   class ViewPagerAdapter extends FragmentPagerAdapter {
      private final List<Fragment> mFragmentList = new ArrayList<>();
      private final List<String> mFragmentTitleList = new ArrayList<>();
      
      public ViewPagerAdapter(FragmentManager manager) {
         super(manager);
      } 
      
      @Override
      public Fragment getItem(int position) {
         return mFragmentList.get(position);
      } 
      
      @Override
      public int getCount() {
         return mFragmentList.size();
      } 
      
      public void addFragment(Fragment fragment, String title) { 
         mFragmentList.add(fragment);
         mFragmentTitleList.add(title);
      } 
      
      @Override
      public CharSequence getPageTitle(int position) {
         return mFragmentTitleList.get(position);
      } 
   }
}

Step 5 − Add the following code to src/OneFragment.java

package myapplication.example.com.myapplication;

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

public class OneFragment extends Fragment { 
   public OneFragment() { 
      // Required empty public constructor
   } 
   
   @Override
   public void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState);
   } 
   
   @Override 
   public View onCreateView(LayoutInflater inflater, ViewGroup container, 
      Bundle savedInstanceState) {
      
      // Inflate the layout for this fragment
      return inflater.inflate(R.layout.fragment_one, container, false); 
   }
}

Step 6 − Add the following code to res/layout/fragment_one.xml.

<?xml version = "1.0" encoding = "utf-8"?>
<LinearLayout xmlns:android = "http://schemas.android.com/apk/res/android"
   android:orientation = "vertical" android:layout_width = "match_parent"
   android:layout_height = "match_parent">
   <TextView
      android:layout_width = "wrap_content"
      android:layout_height = "wrap_content"
      android:text = "First Fragment"
      android:textSize = "40dp"
      android:textStyle = "bold"
      android:layout_centerInParent = "true"/>
</LinearLayout>

Step 7 − Add the following code to src/TwoFragment.java

package myapplication.example.com.myapplication;

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

public class TwoFragment extends Fragment { 
   public TwoFragment() {
      // Required empty public constructor
   } 
   
   @Override
   public void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState);
   } 
   
   @Override
   public View onCreateView(LayoutInflater inflater, ViewGroup container, 
      Bundle savedInstanceState) {
      
      // Inflate the layout for this fragment
      return inflater.inflate(R.layout.fragment_two, container, false);
   }
}

Step 8 − Add the following code to res/layout/fragment_two.xml.

<?xml version = "1.0" encoding = "utf-8"?>
<LinearLayout xmlns:android = "http://schemas.android.com/apk/res/android"
   android:orientation = "vertical" android:layout_width = "match_parent"
   android:layout_height = "match_parent">
   <TextView
      android:layout_width = "wrap_content"
      android:layout_height = "wrap_content"
      android:text = "Second Fragment"
      android:textSize = "40dp"
      android:textStyle = "bold"
      android:layout_centerInParent = "true"/>
</LinearLayout>

Step 9 − Add the following code to src/ThreeFragment.java

package myapplication.example.com.myapplication;

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

public class ThreeFragment extends Fragment { 
   public ThreeFragment() { 
      // Required empty public constructor
   } 
   
   @Override
   public void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 
   } 
   
   @Override
   public View onCreateView(LayoutInflater inflater, ViewGroup container,
      Bundle savedInstanceState) {
      
      // Inflate the layout for this fragment
      return inflater.inflate(R.layout.fragment_three, container, false);
   }
}

Step 10 − Add the following code to res/layout/fragment_three.xml.

<?xml version = "1.0" encoding = "utf-8"?>
<LinearLayout xmlns:android = "http://schemas.android.com/apk/res/android"
   android:orientation = "vertical" android:layout_width = "match_parent"
   android:layout_height = "match_parent">
   <TextView
      android:layout_width = "wrap_content"
      android:layout_height = "wrap_content"
      android:text = "Third Fragment"
      android:textSize = "40dp"
      android:textStyle = "bold"
      android:layout_centerInParent = "true"/>
</LinearLayout>

Step 11 − Add the following code to styles.xml.

<resources>
   <style name = "MyMaterialTheme" parent = "MyMaterialTheme.Base"></style>
   
   <style name = "MyMaterialTheme.Base" parent = "Theme.AppCompat.Light.DarkActionBar">
      <item name = "windowNoTitle">true</item>
      <item name = "windowActionBar">false</item>
      <item name = "colorPrimary">@color/colorPrimary</item>
      <item name = "colorPrimaryDark">@color/colorPrimaryDark</item>
      <item name = "colorAccent">@color/colorAccent</item>
   </style>

</resources>

Step 12 − Add the following code to AndroidManifest.xml.

<?xml version = "1.0" encoding = "utf-8"?>
<manifest xmlns:android = "http://schemas.android.com/apk/res/android"
   package = "myapplication.example.com.myapplication">
   
   <application
      android:allowBackup = "true"
      android:icon = "@mipmap/ic_launcher"
      android:label = "@string/app_name"
      android:supportsRtl = "true"
      android:theme = "@style/MyMaterialTheme">
      <activity android:name = ".MainActivity">
         <intent-filter>
            <action android:name = "android.intent.action.MAIN" />
            <category android:name = "android.intent.category.LAUNCHER" />
         </intent-filter>
      </activity>
   </application>

</manifest>

Let's try to run your application. I assume you have connected your actual Android Mobile device with your computer. To run the app from android studio, open one of your project's activity files and click Run Eclipse Run Icon icon from the toolbar. Select your mobile device as an option and then check your mobile device which will display your default screen −

Tabs
Advertisements