IN THIS BLOG I GIVE YOU BOTTOM BAR NAVIGATION
THIS IS JAVA CODE
package com.example.a44444444; import android.annotation.SuppressLint; import android.os.Bundle; import android.widget.Toast; import androidx.appcompat.app.AppCompatActivity; import androidx.fragment.app.FragmentTransaction; import com.example.a44444444.Fragment1.AddFragment; import com.example.a44444444.Fragment1.HomeFragment; import com.example.a44444444.Fragment1.NotificationFragment; import com.example.a44444444.Fragment1.Profile2Fragment; import com.example.a44444444.Fragment1.SearchFragment; import com.example.a44444444.databinding.ActivityMainBinding; import com.google.android.material.bottomnavigation.BottomNavigationView; public class MainActivity extends AppCompatActivity { ActivityMainBinding binding; BottomNavigationView bottomNavigationView; @SuppressLint ( "NonConstantResourceId" ) @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate ( savedInstanceState ); setContentView ( R.layout.activity_main ); // setContentView ( binding.getRoot () ); binding = ActivityMainBinding.inflate ( getLayoutInflater () ); bottomNavigationView = findViewById ( R.id.readableBottomBar ); bottomNavigationView.setOnItemSelectedListener ( item -> { FragmentTransaction fragmentTransaction = getSupportFragmentManager ().beginTransaction (); fragmentTransaction.replace ( R.id.content,new HomeFragment () ); fragmentTransaction.commit (); switch (item.getItemId ()){ case R.id.home: fragmentTransaction.replace ( R.id.content,new HomeFragment () ); break; case R.id.search: fragmentTransaction.replace ( R.id.content, new SearchFragment () ); break; case R.id.post: fragmentTransaction.replace ( R.id.content, new AddFragment () ); break; case R.id.notification: fragmentTransaction.replace ( R.id.content, new NotificationFragment () ); break; case R.id.Profile: fragmentTransaction.replace ( R.id.content, new Profile2Fragment () ); break; } return true; } ); } } | |
<?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" tools:context=".MainActivity"> <FrameLayout android:id="@+id/content" android:layout_width="match_parent" android:layout_height="0dp" app:layout_constraintBottom_toTopOf="@+id/readableBottomBar" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent"> </FrameLayout> <com.google.android.material.bottomnavigation.BottomNavigationView android:id="@+id/readableBottomBar" android:layout_width="match_parent" android:layout_height="@dimen/_45mdp" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:menu="@xml/nav_item" /> </androidx.constraintlayout.widget.ConstraintLayout> | |
THIS IS XML CODE
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/home"
android:icon="@drawable/ic_home"
android:title="@string/home" />
<item
android:id="@+id/search"
android:icon="@drawable/ic_magnifier"
android:title="@string/search" />
<item
android:id="@+id/post"
android:icon="@drawable/ic_add"
android:title="post" />
<item
android:id="@+id/notification"
android:icon="@drawable/ic_notification"
android:title="@string/notification" />
<item
android:id="@+id/Profile"
android:icon="@drawable/ic_user"
android:title="@string/profile" />
</menu>
|
|
THIS IS MENU ITEM CODE
No comments:
Post a Comment