Bottom Bar with Fragments

Whatever Someone·2024년 10월 20일

To create a bottom bar with 5 buttons that load different fragments in your Android app, you can use a BottomNavigationView. Here’s how you can implement it step by step:

Step 1: Add BottomNavigationView to your layout

In your activity_main.xml (or whichever layout file is your main layout), include a BottomNavigationView and a FrameLayout to hold your fragments.

<androidx.coordinatorlayout.widget.CoordinatorLayout 
  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">
    
    <!-- Main content -->
    <FrameLayout
        android:id="@+id/container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="top"/>

	<!-- BottomAppBar -->
    <com.google.android.material.bottomappbar.BottomAppBar
        android:id="@+id/bottom_app_bar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom"
        android:backgroundTint="@color/colorPrimary"
        app:fabCradleMargin="10dp"
        app:fabCradleRoundedCornerRadius="10dp"
        app:fabCradleVerticalOffset="10dp"
        app:hideOnScroll="true"/>

    <!-- Optional FloatingActionButton (FAB) --><com.google.android.material.floatingactionbutton.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|center"
        android:src="@drawable/ic_add"
        android:tint="@android:color/white"
        app:backgroundTint="@color/colorAccent"/>
        </androidx.coordinatorlayout.widget.CoordinatorLayout>

Step 2: Create the menu for the BottomNavigationView

Create a menu resource file in res/menu/ called bottom_nav_menu.xml. This will define the five buttons for your bottom navigation.


<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:id="@+id/nav_home"
        android:icon="@drawable/ic_home"
        android:title="Home"/>

    <item
        android:id="@+id/nav_feature1"
        android:icon="@drawable/ic_feature1"
        android:title="Feature 1"/>

    <item
        android:id="@+id/nav_feature2"
        android:icon="@drawable/ic_feature2"
        android:title="Feature 2"/>

    <item
        android:id="@+id/nav_feature3"
        android:icon="@drawable/ic_feature3"
        android:title="Feature 3"/>

    <item
        android:id="@+id/nav_feature4"
        android:icon="@drawable/ic_feature4"
        android:title="Feature 4"/>
</menu>

Step 3: Create the fragments

For each of the five features, create a new Fragment class and associated layout. For example:

•	HomeFragment.java
•	Feature1Fragment.java
•	Feature2Fragment.java
•	Feature3Fragment.java
•	Feature4Fragment.java

Each fragment should have its layout file under res/layout/. For example:

<!-- res/layout/fragment_home.xml -->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Home Fragment"/>
</LinearLayout>

Repeat this process for each of the fragments you want to display.

Step 4: Handle navigation and fragment switching

In your main activity (e.g., MainActivity.java), set up the BottomNavigationView to switch fragments when a button is pressed.

import android.os.Bundle;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import com.google.android.material.bottomnavigation.BottomNavigationView;

public class MainActivity extends AppCompatActivity {

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

        BottomNavigationView bottomNav = findViewById(R.id.bottom_navigation);
        bottomNav.setOnNavigationItemSelectedListener(navListener);

        // Set the initial fragment to be the HomeFragment
        if (savedInstanceState == null) {
            getSupportFragmentManager().beginTransaction()
                .replace(R.id.fragment_container, new HomeFragment())
                .commit();
        }
    }

    private BottomNavigationView.OnNavigationItemSelectedListener navListener =
        new BottomNavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                Fragment selectedFragment = null;

                switch (item.getItemId()) {
                    case R.id.nav_home:
                        selectedFragment = new HomeFragment();
                        break;
                    case R.id.nav_feature1:
                        selectedFragment = new Feature1Fragment();
                        break;
                    case R.id.nav_feature2:
                        selectedFragment = new Feature2Fragment();
                        break;
                    case R.id.nav_feature3:
                        selectedFragment = new Feature3Fragment();
                        break;
                    case R.id.nav_feature4:
                        selectedFragment = new Feature4Fragment();
                        break;
                }

                getSupportFragmentManager().beginTransaction()
                    .replace(R.id.fragment_container, selectedFragment)
                    .commit();

                return true;
            }
        };
}

Step 5: Add icons for the menu items

Place icons for each button in the res/drawable/ directory (e.g., ic_home.xml, ic_feature1.xml, etc.). These icons will appear in your BottomNavigationView.

Here's some links:
Home Icon
Chat Icon
Map Icon or Target Icon
Settings Icon
Health Icon
Warning Icon
User Icon
업로드중.. use this in when showing chatgpt as well!

업로드중..

scanner

rectangle Icon

0개의 댓글