Tabbed with ViewPager in Android


Tabbed with View Pager in Android

Output Screen




add dependency in Gradle
implementation 'com.google.android.material:material:1.1.0'

fragment_gallery.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<com.google.android.material.appbar.AppBarLayout 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="wrap_content">

<androidx.appcompat.widget.Toolbar
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:title="Whats App"
app:titleTextColor="#fff" />

<com.google.android.material.tabs.TabLayout
android:id="@+id/mytab"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabTextColor="#fff" />

</com.google.android.material.appbar.AppBarLayout>

<androidx.viewpager.widget.ViewPager
android:id="@+id/viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent" />

</LinearLayout>


GalleryFragment .java
package com.example.mynavigation.ui.gallery;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProviders;
import androidx.viewpager.widget.ViewPager;

import com.example.mynavigation.Call;
import com.example.mynavigation.Chat;
import com.example.mynavigation.Contact;
import com.example.mynavigation.R;
import com.google.android.material.tabs.TabLayout;

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

public class GalleryFragment extends Fragment {

public View onCreateView(@NonNull LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
View root = inflater.inflate(R.layout.fragment_gallery, container, false);
return root;
}

@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
TabLayout tabs = view.findViewById(R.id.mytab);
ViewPager viewPager = view.findViewById(R.id.viewpager);
setUpViewPager(viewPager);
tabs.setupWithViewPager(viewPager);
}

private void setUpViewPager(ViewPager viewPager) {
MyTabs myTabs = new MyTabs(getActivity().getSupportFragmentManager());
myTabs.addTab(new Chat(), "Chat");
myTabs.addTab(new Contact(), "Contact");
myTabs.addTab(new Call(), "Call");
viewPager.setAdapter(myTabs);
}

class MyTabs extends FragmentPagerAdapter {
private final List<Fragment> fragmentList = new ArrayList<>();
private final List<String> fragmentTitleList = new ArrayList<>();

public MyTabs(@NonNull FragmentManager fm) {
super(fm);
}

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

@Override
public int getCount() {
return fragmentList.size();
}

public void addTab(Fragment fragment, String title) {
fragmentList.add(fragment);
fragmentTitleList.add(title);
}

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

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

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Call"
android:textSize="30sp"
android:layout_centerInParent="true"
android:gravity="center"/>

</RelativeLayout>

Call.java
package com.example.mynavigation;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

public class Call extends Fragment {
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater,
     @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.call, container, false);
}
}
contact.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android">

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Contact"
android:textSize="30sp"
android:layout_centerInParent="true"
android:gravity="center"/>

</RelativeLayout>

Contact.java
package com.example.mynavigation;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

public class Contact extends Fragment {
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater,
     @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.contact, container, false);
}
}

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

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Chat"
android:textSize="30sp"
android:layout_centerInParent="true"
android:gravity="center"/>

</RelativeLayout>

Chat.java
package com.example.mynavigation;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

public class Chat extends Fragment {
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater,
     @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.chat, container, false);
}
}