package swiswiswift.com.swiswiswiftandroid import android.support.v7.app.AppCompatActivity import android.os.Bundle import android.support.design.widget.BottomNavigationView import android.support.v4.app.Fragment import android.view.MenuItem class MainActivity : AppCompatActivity(), BottomNavigationView.OnNavigationItemSelectedListener { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) //loading the default fragment loadFragment(HomeFragment()) //getting bottom navigation view and attaching the listener val navigation = findViewById<BottomNavigationView>(R.id.navigation) navigation.setOnNavigationItemSelectedListener(this) } override fun onNavigationItemSelected(item: MenuItem): Boolean { var fragment: Fragment? = null when (item.getItemId()) { R.id.navigation_home -> fragment = HomeFragment() R.id.navigation_dashboard -> fragment = DashboardFragment() R.id.navigation_notifications -> fragment = NotificationsFragment() R.id.navigation_profile -> fragment = ProfileFragment() } return loadFragment(fragment) } private fun loadFragment(fragment: Fragment?): Boolean { //switching fragment if (fragment != null) { supportFragmentManager .beginTransaction() .replace(R.id.fragment_container, fragment!!) .commit() return true } return false } }