Sunday 9 October 2016

Add Tabs in Android Studio | TabLayout


Requirements:-
1.MainActivity.java | activity_main.xml
2.OneFragment.java | fragment_one.xml
3.TwoFragment.java | fragment_two.xml
4.ThreeFragment.java | fragment_three.xml

Points nees to follow:-
1. open build_gradle-> add following
dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:24.1.1'
    compile 'com.android.support:design:24.1.1'
}

Step1:-activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
     android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto">

   <android.support.design.widget.TabLayout
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       app:tabMode="fixed"
       android:layout_alignParentBottom="true"
       app:tabGravity="fill"
       android:id="@+id/tabs"
       android:background="#FA5882"
       app:tabTextColor="#F6CED8"
       app:tabSelectedTextColor="#ffffff">
   </android.support.design.widget.TabLayout>

    <android.support.v4.view.ViewPager
        android:id="@+id/viewPager1"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

    </android.support.v4.view.ViewPager>
</android.support.design.widget.CoordinatorLayout>

Step2:-MainActivity.java
package supriya.com.tabsexample;

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 java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {
    TabLayout tabLayout;
    ViewPager viewPager;

    @Override    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        tabLayout=(TabLayout)findViewById(R.id.tabs);
        viewPager=(ViewPager) findViewById(R.id.viewPager1);
        tabLayout.setupWithViewPager(viewPager);

        /*methods..*/        setupViewPager(viewPager);
    }

    public void setupViewPager(ViewPager v){
        ViewPagerAdapter adapter=new ViewPagerAdapter(getSupportFragmentManager());
        adapter.addFragment(new OneFragment(),"One");
        adapter.addFragment(new TwoFragment(),"Two");
        adapter.addFragment(new ThreeFragment(),"Three");
        v.setAdapter(adapter);
    }

    /*class*/    class ViewPagerAdapter extends FragmentPagerAdapter{
        List<Fragment> listFragment=new ArrayList<>();
        List<String>listString=new ArrayList<>();
        public ViewPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override        public Fragment getItem(int position) {
            return listFragment.get(position);
        }

        @Override        public int getCount() {
            return listFragment.size();
        }
        public void addFragment(Fragment fragment, String title) {
            listFragment.add(fragment);
            listString.add(title);
        }
        public CharSequence getPageTitle(int position) {
            return listString.get(position);
        }
    }
}
Step3:-fragment_one
<?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"
    android:gravity="center_vertical">
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="One"
        android:textSize="40dp"
        android:textStyle="bold"
        android:textAlignment="center"/>
</LinearLayout>
step4:-OneFragment.java
package supriya.com.tabsexample;

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

/** * Created by Supriya on 10/9/2016. */public class OneFragment extends Fragment {

    public OneFragment() {
    }

    
    @Nullable    @Override    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_one,container,false);
    }
}
step5:-fragment_two
<?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"
    android:gravity="center_vertical">
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Page Two"
        android:textSize="40dp"
        android:textStyle="bold"
        android:textAlignment="center"/>
</LinearLayout>
Step6:-TwoFragment.java
package supriya.com.tabsexample;

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

/** * Created by Supriya on 10/9/2016. */
public class TwoFragment extends Fragment {

    public TwoFragment() {
    }
   

    @Nullable    @Override    
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_two,container,false);
    }
}
Step7:-fragment_three
<?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"
    android:gravity="center_vertical">
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Page Three"
        android:textStyle="bold"
        android:textSize="30sp"
        android:textAlignment="center"/>

</LinearLayout>
Step8:-ThreeFragment.java
package supriya.com.tabsexample;

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

/** * Created by Supriya on 10/9/2016. */public class ThreeFragment extends Fragment {

    public ThreeFragment() {
    }

    @Nullable    @Override    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_three,container,false);
    }
}
Output:-


No comments:

Post a Comment