android botom bar navigation android studio code
this is XML code of bottom bar navigation
<?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/frameLayout"
android:layout_width="399dp"
android:layout_height="565dp"
app:layout_constraintBottom_toTopOf="@+id/bottomnavigationview"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.363">
</FrameLayout>
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/bottomnavigationview"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="parent"
app:menu="@menu/bottom_nav_menu">
</com.google.android.material.bottomnavigation.BottomNavigationView>
</androidx.constraintlayout.widget.ConstraintLayout>
package com.example.a1kherwaraapp;
// this is java code for BOTTOM BAR NAVIGATION
import android.annotation.SuppressLint; import android.os.Bundle; import android.view.MenuItem; import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity; import androidx.fragment.app.Fragment; import androidx.fragment.app.FragmentTransaction;
import com.example.a1kherwaraapp.Fragment.Fragment_home; import com.example.a1kherwaraapp.Fragment.Fragment_post; import com.example.a1kherwaraapp.Fragment.Fragment_profile; import com.example.a1kherwaraapp.Fragment.Fragment_search; import com.example.a1kherwaraapp.databinding.ActivityMainBinding;
public class MainActivity extends AppCompatActivity { ActivityMainBinding binding;
@SuppressLint ( "NonConstantResourceId" ) @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate ( savedInstanceState ); //setContentView ( R.layout.activity_main ); binding = ActivityMainBinding.inflate ( getLayoutInflater () ); setContentView ( binding.getRoot () ); binding.bottomnavigationview.setOnItemSelectedListener ( this::onNavigationItemSelected ); replaceFragment ( new Fragment_home () );
} public void replaceFragment (Fragment fragment){ FragmentTransaction transaction = getSupportFragmentManager().beginTransaction(); transaction.replace(R.id.frameLayout, fragment); transaction.addToBackStack(null); transaction.commit(); }
private boolean onNavigationItemSelected(MenuItem item) { switch (item.getItemId ()) { case R.id.home: replaceFragment ( new Fragment_home () );
Toast.makeText ( MainActivity.this, "this is home", Toast.LENGTH_SHORT ).show (); break;
case R.id.search: replaceFragment ( new Fragment_search () ); Toast.makeText ( MainActivity.this, "this is search", Toast.LENGTH_SHORT ).show (); break;
case R.id.post:
replaceFragment ( new Fragment_post () ); Toast.makeText ( MainActivity.this, "this is post", Toast.LENGTH_SHORT ).show (); break;
case R.id.notification: replaceFragment ( new Fragment_post () ); Toast.makeText ( MainActivity.this, "this is notofication", Toast.LENGTH_SHORT ).show (); break;
case R.id.profile: replaceFragment ( new Fragment_profile () ); Toast.makeText ( MainActivity.this, "this is profle", Toast.LENGTH_SHORT ).show (); break; } return true; } }
|
No comments:
Post a Comment