Tablayout With Fragment

 Tablayout With Fragment :


1. First Fragment Create :

2 . activity.main.xml


<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">
    
    <com.google.android.material.tabs.TabLayout
        android:id="@+id/tab_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <com.google.android.material.tabs.TabItem
            android:id="@+id/home_tab"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="HOME"/>
        <com.google.android.material.tabs.TabItem
            android:id="@+id/news_tab"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="NEWS"/>
        <com.google.android.material.tabs.TabItem
            android:id="@+id/person_tab"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="PERSON"/>

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

    <FrameLayout
        android:layout_below="@+id/tab_layout"
        android:id="@+id/frame_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>



</RelativeLayout>




2.Activity.java:



package com.abdulahad.tablayout;

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

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

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

public class MainActivity extends AppCompatActivity {

    TabLayout tab_layout;
    

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        
        //////////////////////////////////////////////
        tab_layout =findViewById(R.id.tab_layout);
        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        fragmentTransaction.add(R.id.frame_layout,new HomeFragment());
        fragmentTransaction.commit();

        tab_layout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {

                int position = tab.getPosition();

                if (position==0){

                    FragmentManager fragmentManager = getSupportFragmentManager();
                    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
                    fragmentTransaction.add(R.id.frame_layout,new HomeFragment());
                    fragmentTransaction.commit();

                    Toast.makeText(MainActivity.this, "Home", Toast.LENGTH_SHORT).show();
                } else if (position==1) {

                    FragmentManager fragmentManager = getSupportFragmentManager();
                    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
                    fragmentTransaction.add(R.id.frame_layout,new NewsFragment());
                    fragmentTransaction.commit();



                    Toast.makeText(MainActivity.this, "News", Toast.LENGTH_SHORT).show();
                }
                else {

                    FragmentManager fragmentManager = getSupportFragmentManager();
                    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
                    fragmentTransaction.add(R.id.frame_layout,new PersonFragment());
                    fragmentTransaction.commit();

                    Toast.makeText(MainActivity.this, "Person", Toast.LENGTH_SHORT).show();
                }
            }

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

            }

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

            }
        });
        //////////////////////////////////////////////


        
        
        
        
    
        

    }
}

Comments

Popular posts from this blog

Navigation And Bottom Font Style

Flutter Native Splash