Monday 20 June 2016

How to implement TabLayout in android studio project?
TabLayout provides a horizontal layout to display tabs and user can swipe the tabs.

Open android studio and create a new project. 
File => New => New Project => Configure your new project => Select the form factor yours app will run on => Add an Activity to Mobile => Customize the Activity => Finish.

First we need to add Library to our project.
compile 'com.android.support:appcompat-v7:23.0.1'
compile 'com.android.support:design:23.0.1'


Create main xml file in project.  


<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.design.widget.TabLayout
            android:id="@+id/tabs"
            android:layout_width="match_parent"
            android:layout_height="@dimen/custom_tab_layout_height"
            app:tabGravity="fill"
            app:tabMode="fixed" />
    </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>


Create custom_tab.xml file in project.  

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/tab"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:fontFamily="@string/font_fontFamily_medium"
    android:textColor="@color/icons"
    android:textSize="@dimen/tab_label" />



Create fragment_diet.xml file in project. 

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
</LinearLayout>



Create fragment_commitment.xml file in project.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
</LinearLayout>   


Create fragment_support.xml file in project. 

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
</LinearLayout>



Create fragment_development.xml file in project. 

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
</LinearLayout>



Create color.xml file in Value folder.

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="primary">#673AB7</color>
    <color name="primary_dark">#512DA8</color>
    <color name="primary_light">#D1C4E9</color>
    <color name="accent">#536DFE</color>
    <color name="primary_text">#212121</color>
    <color name="secondary_text">#727272</color>
    <color name="icons">#FFFFFF</color>
    <color name="divider">#B6B6B6</color>
</resources>



Create dimens.xml file in Value folder.

<resources>
    <dimen name="activity_horizontal_margin">16dp</dimen>
    <dimen name="activity_vertical_margin">14dp</dimen>
    <dimen name="custom_tab_layout_height">72dp</dimen>
    <dimen name="tab_label">8sp</dimen>
</resources>


Create fonts.xml file in Value folder.

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="font_fontFamily_medium">sans-serif</string>
</resources>



Create the Main java file in project.

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.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.widget.TextView;
import com.example.tab.fragment.FragmentCommitment;
import com.example.tab.fragment.FragmentDevelopment;
import com.example.tab.fragment.FragmentDiet;
import com.example.tab.fragment.FragmentSupport;
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);


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

        tabLayout = (TabLayout) findViewById(R.id.tabs);
        tabLayout.setupWithViewPager(viewPager);
        setupTabIcons();
    }

    /**
     * Adding custom view to tab
     */
    private void setupTabIcons() {

        TextView tabdiet = (TextView) LayoutInflater.from(this).inflate(R.layout.custom_tab, null);
        tabdiet.setText("Diet");
        tabdiet.setCompoundDrawablesWithIntrinsicBounds(0, R.drawable.icon_calender, 0, 0);
        tabLayout.getTabAt(0).setCustomView(tabdiet);

        TextView tabcommitment = (TextView) LayoutInflater.from(this).inflate(R.layout.custom_tab, null);
        tabcommitment.setText("Commitment");
        tabcommitment.setCompoundDrawablesWithIntrinsicBounds(0, R.drawable.icon_commit, 0, 0);
        tabLayout.getTabAt(1).setCustomView(tabcommitment);

        TextView tabsupport = (TextView) LayoutInflater.from(this).inflate(R.layout.custom_tab, null);
        tabsupport.setText("Support");
        tabsupport.setCompoundDrawablesWithIntrinsicBounds(0, R.drawable.icon_support, 0, 0);
        tabLayout.getTabAt(2).setCustomView(tabsupport);

        TextView tabdevelopment = (TextView) LayoutInflater.from(this).inflate(R.layout.custom_tab, null);
        tabdevelopment.setText("Development");
        tabdevelopment.setCompoundDrawablesWithIntrinsicBounds(0, R.drawable.icon_developement, 0, 0);
        tabLayout.getTabAt(3).setCustomView(tabdevelopment);
    }

    /**
     * Adding fragments to ViewPager
     *
     * @param viewPager
     */
    private void setupViewPager(ViewPager viewPager) {
        ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
        adapter.addFrag(new FragmentDiet(), "Diet");
        adapter.addFrag(new FragmentCommitment(), "Commitment");
        adapter.addFrag(new FragmentSupport(), "Support");
        adapter.addFrag(new FragmentDevelopment(), "Development");
        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 addFrag(Fragment fragment, String title) {
            mFragmentList.add(fragment);
            mFragmentTitleList.add(title);
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return mFragmentTitleList.get(position);
        }
    }
}


Create the fragment in project.

    FragmentDiet

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.example.tab.R;

/**
 * Created by Laptop on 17-06-2016.
 */
public class FragmentDiet extends Fragment {

    public FragmentDiet() {
        // 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
        View view = inflater.inflate(R.layout.fragment_diet, container, false);
        return view;
    }

}


    FragmentCommitment

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.example.tab.R;

/**
 * Created by Laptop on 17-06-2016.
 */
public class FragmentCommitment extends Fragment {

    public FragmentCommitment() {
        // 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
        View view= inflater.inflate(R.layout.fragment_commitment, container, false);
        return view;
    }

}



    FragmentSupport

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.example.tab.R;


/**
 * Created by Laptop on 17-06-2016.
 */
public class FragmentSupport extends Fragment {

    public FragmentSupport() {
        // 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
        View view= inflater.inflate(R.layout.fragment_support, container, false);
        return view;
    }

}

    FragmentDevelopment

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.example.tab.R;

/**
 * Created by Laptop on 17-06-2016.
 */
public class FragmentDevelopment extends Fragment {

    public FragmentDevelopment() {
        // 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
        View view= inflater.inflate(R.layout.fragment_development, container, false);
        return view;
    }

}


May this code help you. Thanks!!!!!! 

No comments:

Post a Comment