1/ Create A New Project . activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/main"
android:layout_marginTop="24dp"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<com.google.android.material.tabs.TabLayout
android:id="@+id/tablayout"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<com.google.android.material.tabs.TabItem
android:text="Book1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<com.google.android.material.tabs.TabItem
android:text="Book2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<com.google.android.material.tabs.TabItem
android:text="Book3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</com.google.android.material.tabs.TabLayout>
<androidx.viewpager2.widget.ViewPager2
android:id="@+id/viewPager"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
2/ Create Fragment . fragment_tab1.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=".Tab1">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginStart="16dp"
android:layout_marginEnd="16dp"
android:textSize="30sp"
android:textColor="#E6000000"
android:textStyle="bold"
android:text="The Last Library" />
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginStart="16dp"
android:layout_marginEnd="16dp"
android:layout_marginTop="50dp"
android:textSize="16sp"
android:textStyle="normal"
android:text="In a future where books have been banned, a group of rebels known as the Librarians works to preserve knowledge. When a young boy named Leo stumbles upon their hidden library, he’s captivated by the stories and history locked away from society. As he becomes involved with the Librarians, Leo discovers that the government is planning to destroy their sanctuary. With the help of his new friends, he must find a way to protect the library and spark a revolution that reintroduces the joy of reading to a world lost to technology."/>
</FrameLayout>
3/ Create Fragment . fragment_tab2.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=".Tab2">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginStart="16dp"
android:layout_marginEnd="16dp"
android:textSize="30sp"
android:textColor="#E6000000"
android:textStyle="bold"
android:text="The Last Library" />
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginStart="16dp"
android:layout_marginEnd="16dp"
android:layout_marginTop="50dp"
android:textSize="16sp"
android:textStyle="normal"
android:text="In a future where books have been banned, a group of rebels known as the Librarians works to preserve knowledge. When a young boy named Leo stumbles upon their hidden library, he’s captivated by the stories and history locked away from society. As he becomes involved with the Librarians, Leo discovers that the government is planning to destroy their sanctuary. With the help of his new friends, he must find a way to protect the library and spark a revolution that reintroduces the joy of reading to a world lost to technology."/>
</FrameLayout>
4/ Create Fragment . fragment_tab3.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=".Tab3">
<!-- TODO: Update blank fragment layout -->
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginStart="16dp"
android:layout_marginEnd="16dp"
android:textSize="30sp"
android:textColor="#E6000000"
android:textStyle="bold"
android:text="The Timekeeper's Dilemma" />
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginStart="16dp"
android:layout_marginEnd="16dp"
android:layout_marginTop="90dp"
android:textSize="16sp"
android:textStyle="normal"
android:text="In a small village, a mysterious clock tower stands, said to control time itself. When a young girl named Elara accidentally discovers the tower's secret, she learns she can pause time for everyone but herself. At first, she uses this power for fun—skipping school and avoiding chores—but soon realizes the consequences: friends growing distant, seasons changing without her. Elara must decide whether to continue living in her own frozen world or return to the flow of time, embracing its beauty and unpredictability." />
</FrameLayout>
5/ Tab1.java
package com.example.imageslider;
import android.os.Bundle;
import androidx.fragment.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class Tab1 extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_tab1, container, false);
}
}
6/ Tab2.java .
package com.example.tab;
import android.os.Bundle;
import androidx.fragment.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class Tab2 extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_tab2, container, false);
}
}
7/ Tab3.java .
package com.example.tab;
import android.os.Bundle;
import androidx.fragment.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class Tab3 extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_tab3, container, false);
}
}
8/ Create A New java class . ViewPagerAdapter.java
package com.example.tab;
import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;
import androidx.viewpager2.adapter.FragmentStateAdapter;
public class ViewPagerAdapter extends FragmentStateAdapter {
public ViewPagerAdapter(@NonNull FragmentActivity fragmentActivity) {
super(fragmentActivity);
}
@NonNull
@Override
public Fragment createFragment(int position) {
switch (position){
case 0: return new Tab1();
case 1: return new Tab2();
case 2: return new Tab3();
default: return new Tab1();
}
}
@Override
public int getItemCount() {
return 3;
}
}
9/ MainActivity.java
package com.example.tab;
import android.os.Bundle;
import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.viewpager2.widget.ViewPager2;
import com.google.android.material.tabs.TabLayout;
public class MainActivity extends AppCompatActivity {
TabLayout tabLayout;
ViewPager viewPager2;
ViewPagerAdapter viewPagerAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
EdgeToEdge.enable(this);
setContentView(R.layout.activity_main);
tabLayout=findViewById(R.id.tablayout);
viewPager2=findViewById(R.id.viewPager);
viewPagerAdapter=new ViewPagerAdapter(this);
viewPager2.setAdapter(viewPagerAdapter);
tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
@Override
public void onTabSelected(TabLayout.Tab tab) {
viewPager2.setCurrentItem(tab.getPosition());
}
@Override
public void onTabUnselected(TabLayout.Tab tab) {
}
@Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
viewPager2.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {
@Override
public void onPageSelected(int position) {
super.onPageSelected(position);
tabLayout.getTabAt(position).select();
}
});
}
}