I have created a simple application which has a bottom navigation bar. This navigation bar has 3 items (dial pad, device, sync). And I have used fragments also. Basically when the user clicks on menu item (eg: device) application shows the device fragment. Please note that I have not implemented a bottom navigation bar before. However, when I run the application keep stopping. I could not find any solution. Can anyone help me with this?
// this is the main activity
package com.example.bottomnavigation;
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;
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(navListner);
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
new DialPadFragment()).commit();
}
private BottomNavigationView.OnNavigationItemSelectedListener navListner =
new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
Fragment selectedFragment = null;
switch (menuItem.getItemId()){
case R.id.nav_dial:
selectedFragment = new DialPadFragment();
break;
case R.id.nav_device:
selectedFragment = new DeviceFragment();
break;
case R.id.nav_sync:
selectedFragment = new SyncFragment();
break;
}
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
selectedFragment).commit();
return true;
}
};
}
// this is the main activity xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@id/bottom_navigation"/>
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/bottom_navigation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
app:menu="@menu/bottom_navigation"
app:itemTextColor="@color/colorBlack"
android:background="?android:attr/windowActionBar"/>
</RelativeLayout>
// This is one of the fragment I have created.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Device"
android:textSize="30sp"
android:layout_centerInParent="true"/>
</RelativeLayout>
// menu resource file
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/nav_dial"
android:icon="@drawable/ic_dialpad_black_24dp"
android:title="@string/dialPad"/>
<item
android:id="@+id/nav_device"
android:icon="@drawable/ic_device_black_24dp"
android:title="@string/device"/>
<item
android:id="@+id/nav_sync"
android:icon="@drawable/ic_sync_black_24dp"
android:title="@string/Sync"/>
</menu>