Create Tablayout without Viewpager in Android. Tabs in Android with Fragments

Crate Tablayout with Viewpager and fragments :



In this we dont use a Viewpager , due to which we do not get the SLIDING EFFECT.


We use a lsitener on the Tablayout and get the Position of the selected tab using the getPosition() method on tab data passed to the onTabSelected() method.


Then we use fragmentsupportmanager to replace the fragments in the FRAMELAYOUT container.

Activity_main.xml : 


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:orientation="vertical"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context=".MainActivity">


<com.google.android.material.tabs.TabLayout
android:id="@+id/tablayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#D81B60"
app:tabIndicatorHeight="3dp"
app:tabSelectedTextColor="#ffff"
app:tabIndicatorColor="#ffff"
>

<com.google.android.material.tabs.TabItem
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/Calltab1"
android:text="Call"/>

<com.google.android.material.tabs.TabItem
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/Mailtab2"
android:text="Mail"/>

<com.google.android.material.tabs.TabItem
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/Smstab3"
android:text="SMS"/>

</com.google.android.material.tabs.TabLayout>

<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/containerframe">

</FrameLayout>
</LinearLayout>





MainActivity.java :



package com.deepesh.tabswithnoviewpagerapp;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import com.google.android.material.tabs.TabLayout;

public class MainActivity extends AppCompatActivity {

TabLayout tabLayout;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

tabLayout=findViewById(R.id.tablayout1);

//register the Listener
tabLayout.addOnTabSelectedListener(listener);

}

//Create Listener
TabLayout.OnTabSelectedListener listener = new TabLayout.OnTabSelectedListener() {
@Override
public void onTabSelected(TabLayout.Tab tab) {

//getPosition() gives out the Position number or
// the Rank in which we setup the TabItem in the Tablayout

if (tab.getPosition()==0){
calltab1 frag1=new calltab1();
getSupportFragmentManager().beginTransaction().add(R.id.containerframe,frag1).commit();
}else if (tab.getPosition()==1){
mailtab2 frag2=new mailtab2();
getSupportFragmentManager().beginTransaction().add(R.id.containerframe,frag2).commit();
}else if (tab.getPosition()==2){
smstab3 frag3 = new smstab3();
getSupportFragmentManager().beginTransaction().add(R.id.containerframe,frag3).commit();
}
}

@Override
public void onTabUnselected(TabLayout.Tab tab) {

}

@Override
public void onTabReselected(TabLayout.Tab tab) {

}
};

}


EXAMPLE 2 :

(Use "replace" incase of "add" else the fragments are added on top of each other)

activity_main.xml 

<androidx.constraintlayout.widget.ConstraintLayout 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="match_parent">

<com.google.android.material.tabs.TabLayout
android:id="@+id/tablayout1"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">

<com.google.android.material.tabs.TabItem
android:id="@+id/about_tab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="About" />

<com.google.android.material.tabs.TabItem
android:id="@+id/profile_tab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Profile" />

<com.google.android.material.tabs.TabItem
android:id="@+id/contact_tab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Contact" />

</com.google.android.material.tabs.TabLayout>

<FrameLayout
android:id="@+id/fragment_container"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/tablayout1" />


</androidx.constraintlayout.widget.ConstraintLayout>

MainActivity.java

package com.deepesh.exampleapp2;

import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.widget.Toast;

import com.google.android.material.tabs.TabLayout;

public class MainActivity extends AppCompatActivity {

Profile_fragment profile_fragment;
About_Fragment about_fragment;
Contact_fragment contact_fragment;
TabLayout tabLayout;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

profile_fragment = new Profile_fragment();
about_fragment = new About_Fragment();
contact_fragment = new Contact_fragment();
tabLayout = findViewById(R.id.tablayout1);

tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
@SuppressLint("NonConstantResourceId")
@Override
public void onTabSelected(TabLayout.Tab tab) {

switch (tab.getPosition()) {

case 0:
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, about_fragment).commit();
Toast.makeText(MainActivity.this, "about tab", Toast.LENGTH_SHORT).show();
break;
case 1:
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, profile_fragment).commit();
Toast.makeText(MainActivity.this, "profile tab", Toast.LENGTH_SHORT).show();
break;
case 2:
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, contact_fragment).commit();
Toast.makeText(MainActivity.this, "contact tab", Toast.LENGTH_SHORT).show();
break;

}

}

@Override
public void onTabUnselected(TabLayout.Tab tab) {

}

@Override
public void onTabReselected(TabLayout.Tab tab) {

}
});

}

}















































Comments

Popular posts from this blog

React Js + React-Redux (part-2)

React Js + CSS Styling + React Router (part-1)

ViteJS (Module Bundlers, Build Tools)