My full solution, I think will be usefull for juniors:
So, we have MainActivity:
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
class MainMenuActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val bottomNav: BottomNavigationView = findViewById(R.id.bottom_naviagtion)
bottomNav.setOnNavigationItemSelectedListener(navListener)
if (savedInstanceState == null) {
supportFragmentManager.beginTransaction().replace(
R.id.fragment_container,
HomeFragment()
).commit()
}
}
private val navListener: BottomNavigationView.OnNavigationItemSelectedListener =
BottomNavigationView.OnNavigationItemSelectedListener { item ->
var selectedFragment: Fragment? = null
when (item.itemId) {
R.id.bottom_home -> selectedFragment =
HomeFragment()
R.id.bottom_events -> selectedFragment =
EventFragment()
R.id.bottom_contacts -> selectedFragment =
ContactsFragment()
R.id.bottom_menu -> selectedFragment =
MenuFragment()
}
supportFragmentManager.beginTransaction().replace(
R.id.fragment_container,
selectedFragment!!
).commit()
true
}
}
activity_main.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"
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:id="@+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@id/bottom_naviagtion"/>
<View
android:layout_width="match_parent"
android:layout_height="10dp"
android:layout_above="@id/bottom_naviagtion"
android:background="@drawable/shadow"/>
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/bottom_naviagtion"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
app:menu="@menu/bottom_menu"
android:background="?android:attr/windowBackground" />
</RelativeLayout>
Each fragment class looks like:
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
class HomeFragment : Fragment() {
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
return inflater.inflate(R.layout.fragment_home, container, false)
}
}
.xml file for HomeFragment (fragment_home.xml) looks like (other fragments looks the same):
<?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="@string/home"
android:textSize="30sp"
android:layout_centerInParent="true"/>
</RelativeLayout>
NavigationBottomMenu .xml looks (bottom_menu.xml):
<?xml version="1.0" encoding="utf-8"?>
<menu
xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/bottom_home"
android:icon="@drawable/home_selector"
android:title="Home" />
<item
android:id="@+id/bottom_events"
android:icon="@drawable/events_selector"
android:title="Events" />
<item
android:id="@+id/bottom_contacts"
android:icon="@drawable/contacts_selector"
android:title="Contacts"/>
<item
android:id="@+id/bottom_menu"
android:icon="@drawable/menu_selector"
android:title="Menu" />
</menu>
Used icons from drawble folder were imported like Vector Asset