0% found this document useful (0 votes)
6 views3 pages

LabPreppp 3

The document contains XML layout files for three fragments, each displaying a different text, and Kotlin classes for these fragments that extend the Fragment class. It also includes a ViewPagerAdapter to manage the fragments and an activity layout that integrates a TabLayout with a ViewPager2. Additionally, it provides the necessary dependencies for Material Design and ViewPager2 in a Gradle build file.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views3 pages

LabPreppp 3

The document contains XML layout files for three fragments, each displaying a different text, and Kotlin classes for these fragments that extend the Fragment class. It also includes a ViewPagerAdapter to manage the fragments and an activity layout that integrates a TabLayout with a ViewPager2. Additionally, it provides the necessary dependencies for Material Design and ViewPager2 in a Gradle build file.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Ex – 10: Tab Layout

fragment_tab1.xml
<?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:gravity="center"
android:orientation="vertical">

<TextView
android:id="@+id/text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Text 1"
android:textSize="24sp"/>
</LinearLayout>

fragment_tab2.xml
<?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:gravity="center"
android:orientation="vertical">

<TextView
android:id="@+id/text2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Text 2"
android:textSize="24sp"/>
</LinearLayout>

fragment_tab3.xml
<?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:gravity="center"
android:orientation="vertical">

<TextView
android:id="@+id/text3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Text 3"
android:textSize="24sp"/>
</LinearLayout>

Fragment1.kt
import androidx.fragment.app.Fragment

class Fragment1 : Fragment(R.layout.fragment_tab1) {


// You can add logic here if needed
}t

Fragment2.kt
package com.example.appdevlapprep

import androidx.fragment.app.Fragment

class Fragment2 : Fragment(R.layout.fragment_tab2) {


// You can add logic here if needed
}
Fragment3.kt
package com.example.appdevlapprep

import androidx.fragment.app.Fragment

class Fragment3 : Fragment(R.layout.fragment_tab3) {


// You can add logic here if needed
}

ViewPagerAdapter.kt

import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentActivity
import androidx.viewpager2.adapter.FragmentStateAdapter

class ViewPagerAdapter(fragmentActivity: FragmentActivity) :


FragmentStateAdapter(fragmentActivity) {

override fun getItemCount(): Int {


return 3
}

override fun createFragment(position: Int): Fragment {


return when (position) {
0 -> Fragment1() // Tab 1
1 -> Fragment2() // Tab 2
else -> Fragment3() // Tab 3
}
}
}

activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout
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:layout_width="match_ptcarent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<com.google.android.material.tabs.TabLayout
android:id="@+id/tabLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabMode="fixed"
app:tabGravity="fill" />

<androidx.viewpager2.widget.ViewPager2
android:id="@+id/viewPager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@id/tabLayout" />
</androidx.coordinatorlayout.widget.CoordinatorLayout>

MainActivity.kt
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.viewpager2.widget.ViewPager2
import com.google.android.material.tabs.TabLayout
import com.google.android.material.tabs.TabLayoutMediator

class MainActivity : AppCompatActivity() {

override fun onCreate(savedInstanceState: Bundle?) {


super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)

tabLayout = findViewById(R.id.tabLayout)
viewPager = findViewById(R.id.viewPager)

val adapter = ViewPagerAdapter(this)


viewPager.adapter = adapter

// Link TabLayout and ViewPager2


TabLayoutMediator(tabLayout, viewPager) { tab, position ->
tab.text = "Tab ${position + 1}" // Set Tab title
}.attach()
}
}

gradle.kts(:app)

dependencies {
implementation("com.google.android.material:material:1.8.0")
implementation("androidx.viewpager2:viewpager2:1.0.0")
implementation("androidx.fragment:fragment-ktx:1.5.6")
}

libs.version.toml

[versions]
viewpager2 = "1.0.0"
fragment_ktx = "1.5.6"

[libraries]
viewpager2 = { module = "androidx.viewpager2:viewpager2", version.ref = "viewpager2" }
fragment_ktx = { module = "androidx.fragment:fragment-ktx", version.ref =
"fragment_ktx" }

You might also like