Name: Shubhasmita S Sahu
Class: TYIT
Seat No: 2013263
Practical No-5
Create an android app that demonstrates the use of Tablayout using pageadapter and
fragment.
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
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_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<com.google.android.material.tabs.TabLayout
android:id="@+id/tablayout1"
android:layout_width="424dp"
android:layout_height="58dp"
android:background="#1B6B6B"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.4"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:tabTextAppearance="@style/TextAppearance.AppCompat.Medium"
app:tabTextColor="#43A047">
<com.google.android.material.tabs.TabItem
android:id="@+id/tab1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:foreground="@android:color/holo_green_dark"
android:text="CHAT" />
<com.google.android.material.tabs.TabItem
android:id="@+id/tab2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="STATUS" />
<com.google.android.material.tabs.TabItem
android:id="@+id/tab3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="CALLS" />
</com.google.android.material.tabs.TabLayout>
ADVANCED MOBILE PROGRAMMING
Name: Shubhasmita S Sahu
Class: TYIT
Seat No: 2013263
<androidx.viewpager.widget.ViewPager
android:id="@+id/vpager"
android:layout_width="0dp"
android:layout_height="0dp"
android:background="#FAFCFC"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.515"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/tablayout1"
app:layout_constraintVertical_bias="0.0" />
</androidx.constraintlayout.widget.ConstraintLayout>
Output:
Main_Activity.java:
package com.example.tab;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import androidx.viewpager.widget.ViewPager;
import com.google.android.material.tabs.TabItem;
import com.google.android.material.tabs.TabLayout;
public class MainActivity extends AppCompatActivity {
TabItem tabItem1,tabItem2,tabItem3;
TabLayout tabLayout;
ViewPager viewPager;
pageadapter pageAdapter;
ADVANCED MOBILE PROGRAMMING
Name: Shubhasmita S Sahu
Class: TYIT
Seat No: 2013263
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tabLayout =(TabLayout)findViewById(R.id.tablayout1);
viewPager =(ViewPager) findViewById(R.id.vpager);
tabItem1 = (TabItem)findViewById(R.id.tab1);
tabItem2 = (TabItem)findViewById(R.id.tab2);
tabItem3 = (TabItem)findViewById(R.id.tab3);
//pageadapter supports data set change,removed changing possition//
//fragment represents UI manages layout//
pageAdapter = new pageadapter(getSupportFragmentManager(),
tabLayout.getTabCount());
//view pager is a layout manager that allows the user to flip// //
left and right through pages of data//
viewPager.setAdapter(pageAdapter);
tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
@Override
public void onTabSelected(TabLayout.Tab tab)
{
viewPager.setCurrentItem(tab.getPosition());
if(tab.getPosition()==0|| tab.getPosition()==1|| tab.getPosition()==2)
//adapter is changed//
pageAdapter.notifyDataSetChanged();
}
@Override
public void onTabUnselected(TabLayout.Tab tab) {
}
@Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
viewPager.addOnPageChangeListener(new
TabLayout.TabLayoutOnPageChangeListener(tabLayout));
}
}
pageadapter.java:
//pageadapter supports data set change,removed changing possition//
package com.example.tab;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
ADVANCED MOBILE PROGRAMMING
Name: Shubhasmita S Sahu
Class: TYIT
Seat No: 2013263
import androidx.fragment.app.FragmentPagerAdapter;
public class pageadapter extends FragmentPagerAdapter {
int tabcount;
public pageadapter( FragmentManager fm, int behavior) {
super(fm, behavior);
tabcount=behavior;
}
@Override
public Fragment getItem(int position)
{
switch (position)
{
case 0:
return new ftab1();
case 1:
return new ftab2();
case 2:
return new ftab3();
default:
return null;
}
}
@Override
public int getCount()
{
return tabcount;
}
}
fragment_ftab1:
//for each tab required fragment layout//
//fragment represents UI manages layout//
//to add fragment right click on project main source file -new-fragment-fragmnt blank//
ADVANCED MOBILE PROGRAMMING
Name: Shubhasmita S Sahu
Class: TYIT
Seat No: 2013263
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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_parent"
android:layout_height="match_parent"
tools:context=".ftab1">
<!-- TODO: Update blank fragment layout -->
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#175555"
android:gravity="center|top"
android:text="CHATS"
android:textColor="#26B62C"
android:textSize="24sp"
android:textStyle="bold|italic"
app:fontFamily="@font/berkshire_swash" />
<ImageView
android:id="@+id/imageView3"
android:layout_width="381dp"
android:layout_height="250dp"
android:layout_gravity="center"
android:drawingCacheQuality="auto"
android:src="@drawable/chats" />
</FrameLayout>
ftab1.java:
ADVANCED MOBILE PROGRAMMING
Name: Shubhasmita S Sahu
Class: TYIT
Seat No: 2013263
package com.example.tab;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.fragment.app.Fragment;
public class ftab1 extends Fragment {
public ftab1() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_ftab1, container, false); }
}
fragment_ftab2.xml:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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_parent"
android:layout_height="match_parent"
tools:context=".ftab2">
<!-- TODO: Update blank fragment layout -->
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#175555"
android:gravity="center|top"
android:text="STATUS"
android:textColor="#26B62C"
android:textSize="24sp"
android:textStyle="bold|italic"
app:fontFamily="@font/berkshire_swash" />
<ImageView
android:id="@+id/imageView2"
android:layout_width="343dp"
android:layout_height="248dp"
android:layout_gravity="center"
android:src="@drawable/status" />
ADVANCED MOBILE PROGRAMMING
Name: Shubhasmita S Sahu
Class: TYIT
Seat No: 2013263
</FrameLayout>
ftab2.java:
package com.example.tab;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.fragment.app.Fragment;
public class ftab2 extends Fragment {
public ftab2() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_ftab2, container, false); }
}
fragment_ftab3.xml:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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_parent"
android:layout_height="match_parent"
tools:context=".ftab3">
<!-- TODO: Update blank fragment layout -->
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="top"
android:background="#175555"
android:gravity="center|top"
android:text="CALLS"
android:textColor="#26B62C"
android:textSize="24sp"
android:textStyle="bold|italic"
app:fontFamily="@font/berkshire_swash" />
<ImageView
android:id="@+id/imageView"
android:layout_width="wrap_content"
ADVANCED MOBILE PROGRAMMING
Name: Shubhasmita S Sahu
Class: TYIT
Seat No: 2013263
android:layout_height="wrap_content"
android:layout_gravity="center"
android:src="@drawable/calls" />
</FrameLayout>
ftab3.java:
package com.example.tab;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.fragment.app.Fragment;
public class ftab3 extends Fragment {
public ftab3() {
// Required empty public constructor
}
@Override
//reading a XML Layout//
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_ftab3, container, false); }
}
ADVANCED MOBILE PROGRAMMING
Name: Shubhasmita S Sahu
Class: TYIT
Seat No: 2013263
Output:
ADVANCED MOBILE PROGRAMMING