AndroidTabLayout



Library

implementation 'com.android.support:design:26.1.0'


TabLayoutActivity.java


 package hemanth.smartkit.com.Activity;  
 import android.app.Activity;  
 import android.content.Intent;  
 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;  
 import hemanth.smartkit.com.Fragment.HomeFragment;  
 import hemanth.smartkit.com.R;  
 public class TabLayoutActivity extends AppCompatActivity {  
   private TabLayout tabLayout;  
   public static ViewPager viewPager;  
   static Activity activity;  
   String WhichIntent;  
   public static Activity getInstance() {  
     return activity;  
   }  
   @Override  
   protected void onCreate(Bundle savedInstanceState) {  
     super.onCreate(savedInstanceState);  
     setContentView(R.layout.activity_tab_layout);  
     activity = this;  
     InItUi();  
     ActionUi();  
   }  
   private void InItUi() {  
     Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);  
     setSupportActionBar(toolbar);  
     getSupportActionBar().setTitle("Charity Pay");  
     toolbar.setTitleTextColor(getResources().getColor(android.R.color.white));  
     getSupportActionBar().setDisplayHomeAsUpEnabled(true);  
     getSupportActionBar().setDisplayShowHomeEnabled(true);  
     Intent intent = getIntent();  
     WhichIntent = intent.getStringExtra("Intent");  
     viewPager = (ViewPager) findViewById(R.id.viewpager);  
     setupViewPager(viewPager);  
     if (WhichIntent.equalsIgnoreCase("MainActivity")) {  
       viewPager.setCurrentItem(0);  
     } else if (WhichIntent.equalsIgnoreCase("CharityProceedToPayWebViewActivity")) {  
       viewPager.setCurrentItem(1);  
     }else if (WhichIntent.equalsIgnoreCase("CharityHistoryDetailViewActivity")) {  
       viewPager.setCurrentItem(2);  
     }  
     tabLayout = (TabLayout) findViewById(R.id.tabs);  
     tabLayout.setupWithViewPager(viewPager);  
     tabLayout.setTabTextColors(getResources().getColor(android.R.color.white), getResources().getColor(android.R.color.white));  
   }  
   private void setupViewPager(ViewPager viewPager) {  
     TabLayoutActivity.ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());  
     adapter.addFragment(new HomeFragment(), "Home");  
     adapter.addFragment(new Fragment1(), "History");  
     adapter.addFragment(new Fragment2(), "Favourites");  
     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);  
     }  
   }  
   private void ActionUi() {  
   }  
   @Override  
   public boolean onSupportNavigateUp() {  
     onBackPressed();  
     return true;  
   }  
 }  

HomeFragment.java




 package hemanth.smartkit.com.Fragment;  
 import android.os.Bundle;  
 import android.support.v4.app.Fragment;  
 import android.view.LayoutInflater;  
 import android.view.View;  
 import android.view.ViewGroup;  
 import hemanth.smartkit.com.R;  
 public class HomeFragment extends Fragment {  
   @Override  
   public View onCreateView(LayoutInflater inflater, ViewGroup container,  
                Bundle savedInstanceState) {  
     View view = inflater.inflate(R.layout.fragment_home, container, false);  
     return view;  
   }  
 }  



activity_tab_layout.xml


 <?xml version="1.0" encoding="utf-8"?>  
 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
   xmlns:app="http://schemas.android.com/apk/res-auto"  
   android:layout_width="fill_parent"  
   android:layout_height="fill_parent"  
   android:background="@color/colorPrimary">  
   <android.support.v7.widget.Toolbar  
     android:id="@+id/toolbar"  
     android:layout_width="match_parent"  
     android:layout_height="?attr/actionBarSize"  
     app:popupTheme="@style/AppTheme" />  
   <LinearLayout  
     android:layout_width="match_parent"  
     android:layout_height="match_parent"  
     android:layout_below="@+id/toolbar"  
     android:orientation="vertical">  
     <android.support.design.widget.AppBarLayout  
       android:layout_width="match_parent"  
       android:layout_height="wrap_content"  
       android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">  
       <View  
         android:layout_width="match_parent"  
         android:layout_height="2dp"  
         android:layout_below="@+id/toolbar"  
          />  
       <android.support.design.widget.TabLayout  
         android:id="@+id/tabs"  
         android:showDividers="middle"  
         android:layout_width="match_parent"  
         android:layout_height="wrap_content"  
         app:tabMode="fixed"  
         app:tabGravity="fill"/>  
     </android.support.design.widget.AppBarLayout>  
     <View  
       android:layout_width="match_parent"  
       android:layout_height="2dp"  
       android:layout_below="@+id/toolbar"  
        />  
     <android.support.v4.view.ViewPager  
       android:id="@+id/viewpager"  
       android:layout_width="match_parent"  
       android:layout_height="match_parent"  
        />  
   </LinearLayout>  
 </RelativeLayout>  


fragment_home.xml

 <?xml version="1.0" encoding="utf-8"?>  
 <FrameLayout 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=".Fragment.HomeFragment">  
   <!-- TODO: Update blank fragment layout -->  
   <TextView  
     android:layout_width="match_parent"  
     android:layout_height="match_parent"  
     android:text="@string/hello_blank_fragment" />  
 </FrameLayout>