Bottom Navigation semplifica il passaggio da una schermata all’ altra e viene utilizzato quando un applicazione ha da tre a cinque destinazioni di primo livello. Quando si tocca l’icona di navigazione in basso l’utente accede alla vista associata:
menu_navigation.xml
<?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_launcher_background”
android:title=”Home”/>
<item
android:id=”@+id/setting”
android:icon=”@drawable/ic_launcher_background”
android:title=”Setting”/>
<item
android:id=”@+id/camera”
android:icon=”@drawable/ic_launcher_background”
android:title=”Camera”/>
</menu>
activity_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”
tools:context=”.MainActivity”>
<com.google.android.material.bottomnavigation.BottomNavigationView
android:layout_width=”0dp”
android:layout_height=”wrap_content”
android:id=”@+id/bottomNavigation”
app:layout_constraintBottom_toBottomOf=”parent”
app:layout_constraintLeft_toLeftOf=”parent”
app:layout_constraintRight_toRightOf=”parent”
app:menu=”@menu/menu_navigation”>
</com.google.android.material.bottomnavigation.BottomNavigationView>
<FrameLayout
android:layout_width=”0dp”
android:layout_height=”0dp”
android:id=”@+id/frame_layout”
app:layout_constraintBottom_toTopOf=”@+id/bottomNavigation”
app:layout_constraintLeft_toLeftOf=”parent”
app:layout_constraintRight_toRightOf=”parent”
app:layout_constraintTop_toTopOf=”parent”/>
</androidx.constraintlayout.widget.ConstraintLayout>
home_layout.xml
<LinearLayout 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”
android:orientation=”vertical”
android:background=”#CD30C8″>
</LinearLayout>
setting_layout.xml
<LinearLayout 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”
android:orientation=”vertical”
android:background=”#2F3CD8″>
</LinearLayout>
camera_layout.xml
<LinearLayout 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”
android:orientation=”vertical”
android:background=”#25CC14″>
</LinearLayout>
FragmentHome.java
package com.example.bottomNavigation;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.fragment.app.Fragment;
public class FragmentHome extends Fragment{
public FragmentHome(){
}
@Override
public View onCreateView(LayoutInflater inflater,
ViewGroup container,Bundle savedInstanceState){
return
inflater.inflate(R.layout.home_layout,container,false);
}
}
FragmentSetting.java
package com.example.bottomNavigation;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.fragment.app.Fragment;
public class FragmentSetting extends Fragment{
public FragmentSetting(){
}
@Override
public View onCreateView(LayoutInflater inflater,
ViewGroup container,Bundle savedInstanceState){
return
inflater.inflate(R.layout.setting_layout,container,false);
}
}
FragmentCamera.java
package com.example.bottomNavigation;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.fragment.app.Fragment;
public class FragmentCamera extends Fragment{
public FragmentCamera(){
}
@Override
public View onCreateView(LayoutInflater inflater,
ViewGroup container,Bundle savedInstanceState){
return
inflater.inflate(R.layout.camera_layout,container,false);
}
}
MainActivity.java
package com.example.bottomNavigation;
import android.view.MenuItem;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import androidx.fragment.app.Fragment;
import com.google.android.material.bottomnavigation.BottomNavigationView;
import com.google.android.material.navigation.NavigationBarView;
import com.google.android.material.navigation.NavigationView;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
BottomNavigationView bottomNavigation=
findViewById(R.id.bottomNavigation);
bottomNavigation.setOnItemSelectedListener
(new NavigationBarView.OnItemSelectedListener(){
@Override
public boolean
OnNavigationItemSelected(MenuItem item){
switch (item.getItemId()){
case R.id.home:
loadFragment(new FragmentHome());
break ;
case R.id.setting:
loadFragment(new FragmentSetting());
break;
case R.id.camera:
loadFragment(new FragmentCamera());
break;
}
return true;
}
});
}
private void loadFragment(Fragment fragment){
getSupportFragmentManager()
.beginTransaction()
.replace(R.id.frame_layout,fragment)
.commit();
}
}