Fragments With Google Material Tab Layout
Fragments With Google Material Tab Layout
Main Activity
<?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_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_margin="10dp"
tools:context=".MainActivity">
<com.google.android.material.tabs.TabLayout
android:id="@+id/tabLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabBackground="@color/white"
app:tabIndicatorColor="#f00"
app:tabSelectedTextColor="#f00"
app:tabTextColor="#000" >
<com.google.android.material.tabs.TabItem
android:id="@+id/itemHome"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="HOME"/>
<com.google.android.material.tabs.TabItem
android:id="@+id/itemProfile"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="PROFILE"/>
<com.google.android.material.tabs.TabItem
android:id="@+id/itemChat"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Chat"/>
</com.google.android.material.tabs.TabLayout>
<Button
android:id="@+id/buttonOne"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="First !"
android:textSize="20dp"
android:gravity="center"
/>
<Button
android:id="@+id/buttonTwo"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Secount Fragments!"
android:textSize="20dp"
android:gravity="center"
/>
<FrameLayout
android:id="@+id/framLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
MainActivity.java
package com.example.fragmentsexam;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.TableLayout;
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.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
import com.google.android.material.tabs.TabLayout;
public class MainActivity extends AppCompatActivity {
Button buttonOne,buttonTwo;
TabLayout tabLayout;
FrameLayout framLayout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
EdgeToEdge.enable(this);
setContentView(R.layout.activity_main);
ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
return insets;
});
tabLayout=findViewById(R.id.tabLayout);
buttonOne=findViewById(R.id.buttonOne);
buttonTwo=findViewById(R.id.buttonTwo);
framLayout=findViewById(R.id.framLayout);
buttonOne.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
FragmentManager fragmentManager=getSupportFragmentManager();
FragmentTransaction fragmentTransaction=fragmentManager.beginTransaction();
fragmentTransaction.add(R.id.framLayout,new Fastragment());
fragmentTransaction.commit();
}
});
buttonTwo.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
FragmentManager fragmentManager=getSupportFragmentManager();
FragmentTransaction fragmentTransaction=fragmentManager.beginTransaction();
fragmentTransaction.add(R.id.framLayout,new SecoundFragment());
fragmentTransaction.commit();
}
});
tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
@Override
public void onTabSelected(TabLayout.Tab tab) {
int tabPossition= tab.getPosition();
if (tabPossition==0){
FragmentManager fragmentManager=getSupportFragmentManager();
FragmentTransaction fragmentTransaction=fragmentManager.beginTransaction();
fragmentTransaction.add(R.id.framLayout,new Fastragment());
fragmentTransaction.commit();
} else if (tabPossition==1) {
FragmentManager fragmentManager=getSupportFragmentManager();
FragmentTransaction fragmentTransaction=fragmentManager.beginTransaction();
fragmentTransaction.add(R.id.framLayout,new SecoundFragment());
fragmentTransaction.commit();
}
else if (tabPossition==2) {
FragmentManager fragmentManager=getSupportFragmentManager();
FragmentTransaction fragmentTransaction=fragmentManager.beginTransaction();
fragmentTransaction.add(R.id.framLayout,new Fastragment());
fragmentTransaction.commit();
}
}
@Override
public void onTabUnselected(TabLayout.Tab tab) {
}
@Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
}
}
Fragment Create package name + New +Fragment + Blank Fragments + Finish ai vabe 2ta Fragment toiri korbo FastFragment akta ar 2 number SecoundFragment
Fast Figment
<?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"
android:background="#020A17"
tools:context=".Fastragment">
<!-- TODO: Update blank fragment layout -->
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textColor="@color/white"
android:textSize="30dp"
android:layout_gravity="center"
android:gravity="center"
android:text="FAST FRAGMENTS" />
</LinearLayout>
Fast Fragment Java
package com.example.fragmentsexam;
import android.os.Bundle;
import androidx.fragment.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
/**
* A simple {@link Fragment} subclass.
* create an instance of this fragment.
*/
public class Fastragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
// Inflate the layout for this fragment
View myView=inflater.inflate(R.layout.fastragment,container,false);
return myView;
}
}
Second Figment
<?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"
android:background="#0761FA"
tools:context=".SecoundFragment">
<!-- TODO: Update blank fragment layout -->
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textColor="@color/white"
android:textSize="30dp"
android:layout_gravity="center"
android:gravity="center"
android:text="FAST FRAGMENTS" />
</LinearLayout>
Second Java
package com.example.fragmentsexam;
import android.os.Bundle;
import androidx.fragment.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
/**
* A simple {@link Fragment} subclass.
* create an instance of this fragment.
*/
public class SecoundFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View myView=inflater.inflate(R.layout.fragment_secound,container,false);
return myView;
}
}
Fragments With Google Material Tab Layout