Widget Recent Post No.

header ads

How to open fragment from activity on button click || LearnCode With RK



Step 1 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"

    android:background="@drawable/background1"

    tools:context=".main.MainActivity">




    <RelativeLayout

        android:layout_width="match_parent"

        android:layout_height="match_parent">


        <ImageView

            android:id="@+id/logo"

            android:layout_width="250dp"

            android:layout_height="250dp"

            android:layout_centerHorizontal="true"

            android:layout_centerVertical="true"

            android:src="@drawable/logo"/>




        <com.airbnb.lottie.LottieAnimationView

            android:id="@+id/animation_view"

            android:layout_width="80dp"

            android:layout_height="80dp"

            app:lottie_fileName="loading.json"

            app:lottie_loop="true"

            app:lottie_autoPlay="true"

            android:layout_gravity="center"

            android:layout_alignParentBottom="true"

            android:layout_centerHorizontal="true"

            android:layout_marginBottom="20dp"/>




    </RelativeLayout>


</androidx.constraintlayout.widget.ConstraintLayout>


Step 2 main.java

package in.xtremeworkplace.xtremeworkplace.home;


import androidx.annotation.NonNull;

import androidx.appcompat.app.AppCompatActivity;


import androidx.fragment.app.Fragment;



import android.os.Bundle;

import android.view.MenuItem;



import com.google.android.material.bottomnavigation.BottomNavigationView;



import in.xtremeworkplace.xtremeworkplace.R;

import in.xtremeworkplace.xtremeworkplace.fragment.homeFragment;

import in.xtremeworkplace.xtremeworkplace.fragment.how_to_Fragment;

import in.xtremeworkplace.xtremeworkplace.fragment.serviceFragment;

import in.xtremeworkplace.xtremeworkplace.fragment.web_design_Fragment;


public class Home_page extends AppCompatActivity implements BottomNavigationView.OnNavigationItemSelectedListener{



    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_home_page);




        //loading the default fragment

        loadFragment(new homeFragment());


        //getting bottom navigation view and attaching the listener

        BottomNavigationView navigation = findViewById(R.id.navigation);

        navigation.setOnNavigationItemSelectedListener(this);

    }



    @Override

    public boolean onNavigationItemSelected(@NonNull MenuItem item) {

        Fragment fragment = null;


        switch (item.getItemId()) {

            case R.id.home:

                fragment = new homeFragment();

                break;


            case R.id.how_to:

                fragment = new how_to_Fragment();

                break;



            case R.id.service:

                fragment = new serviceFragment();

                break;


            case R.id.web_design:

                fragment = new web_design_Fragment();

                break;

        }


        return loadFragment(fragment);

    }


    private boolean loadFragment(Fragment fragment) {

        //switching fragment

        if (fragment != null) {

            getSupportFragmentManager()

                    .beginTransaction()

                    .replace(R.id.fragment_container, fragment)

                    .commit();

            return true;

        }

        return false;

    }

}


GITHUB : https://github.com/LearncodeWithRk/BottomNavigationView-with-Fragments

 

Post a Comment

0 Comments