Fragment, Navigation & Recycle View



FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fTransaction = fragmentManager.beginTransaction();
fTransaction.add(R.id.frameLayout,new FirstFragment());

fTransaction.commit(); 


XML Design

<FrameLayout
android:id="@+id/frameLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/topBar"
android:layout_above="@+id/button"/>

<LinearLayout
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_above="@+id/navBar"
android:orientation="horizontal"
>
<Button
android:id="@+id/fragment1"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:padding="5dp"
android:backgroundTint="#FF9800"
android:text="First"/>

<Button
android:id="@+id/fragment2"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:padding="5dp"
android:backgroundTint="#FFC107"
android:text="Second"/>

</LinearLayout>


Java Code

package com.example.superadminpanel;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;



public class MainActivity extends AppCompatActivity {

Button fragment1, fragment2;

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

fragment1 = findViewById(R.id.fragment1);
fragment2 = findViewById(R.id.fragment2);

fragment1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fTransaction = fragmentManager.beginTransaction();
fTransaction.add(R.id.frameLayout,new FragmentOne());
fTransaction.commit();

}
});

fragment2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fTransaction = fragmentManager.beginTransaction();
fTransaction.add(R.id.frameLayout,new FragmentTwo());
fTransaction.commit();

}
});

}
}


Bottom Navigation With Frame Layout in Fragment


Fragment View


XML Design

<?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:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#fdf9f8"
tools:context=".MainActivity">

<androidx.cardview.widget.CardView
android:id="@+id/topBar"
android:layout_width="match_parent"
android:layout_height="50dp"
android:elevation="10dp"
app:cardBackgroundColor="#fdf9f8">
<LinearLayout

android:layout_width="match_parent"
android:layout_height="50dp"
android:orientation="horizontal">
<TextView
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:textColor="#CEA66A"
android:textSize="20sp"
android:text="Super Admin Panel"
android:gravity="center_vertical"
android:padding="10dp"
android:textStyle="bold"
android:layout_weight="2"/>

<com.google.android.material.bottomnavigation.BottomNavigationView
android:layout_width="fill_parent"
android:layout_height="match_parent"
app:menu="@menu/top_menu"
app:compatShadowEnabled="false"
android:background="#fdf9f8"
android:layout_weight="5"/>
</LinearLayout>

</androidx.cardview.widget.CardView>

<FrameLayout
android:id="@+id/frameLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/topBar"
android:layout_above="@+id/navBar"/>



<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/navBar"
android:layout_width="match_parent"
android:layout_height="60dp"
app:menu="@menu/bottom_menu"
android:layout_alignParentBottom="true"/>

</RelativeLayout>

Java Code

package com.example.superadminpanel;

import android.os.Bundle;
import android.view.MenuItem;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
import com.google.android.material.bottomnavigation.BottomNavigationView;
import com.google.android.material.navigation.NavigationBarView;


public class MainActivity extends AppCompatActivity {

BottomNavigationView navBar;

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

navBar = findViewById(R.id.navBar);


navBar.setOnItemSelectedListener(new NavigationBarView.OnItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {

if (item.getItemId()==R.id.home){
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fTransaction = fragmentManager.beginTransaction();
fTransaction.add(R.id.frameLayout,new FragmentOne());
fTransaction.commit();
}

else if (item.getItemId()==R.id.addStudent){
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fTransaction = fragmentManager.beginTransaction();
fTransaction.add(R.id.frameLayout,new FragmentTwo());
fTransaction.commit();
}

return true;
}
});

}
}

Badge Create




Post a Comment

Post a Comment (0)

Previous Post Next Post