Skip to content

Instantly share code, notes, and snippets.

@emedinaa
Created November 23, 2019 22:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save emedinaa/e93180234bc86e5239bc86dee611ae8f to your computer and use it in GitHub Desktop.
Save emedinaa/e93180234bc86e5239bc86dee611ae8f to your computer and use it in GitHub Desktop.
BottomNavigation
```
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.fragment.app.Fragment
import com.emedinaa.kotlinapp.R
import com.emedinaa.kotlinapp.fragments.AFragment
import com.emedinaa.kotlinapp.fragments.BFragment
import com.emedinaa.kotlinapp.fragments.CFragment
import kotlinx.android.synthetic.main.activity_bottom_navigation.*
class BottomNavigationActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_bottom_navigation)
supportActionBar?.setDisplayHomeAsUpEnabled(true)
bottomNavigationView.setOnNavigationItemSelectedListener {
var fragment:Fragment?=null
when(it.itemId){
R.id.action_favorites -> fragment= AFragment.newInstance("","")
R.id.action_schedules -> fragment= BFragment.newInstance("","")
R.id.action_music -> fragment= CFragment.newInstance("","")
}
fragment?.let {
changeFragment(it)
}
return@setOnNavigationItemSelectedListener true
}
//first tab
changeFragment(AFragment.newInstance("",""))
}
private fun changeFragment(fragment:Fragment){
supportFragmentManager.beginTransaction().apply {
replace(R.id.frameLayout,fragment,null)
commit()
}
}
override fun onSupportNavigateUp(): Boolean {
onBackPressed()
return true
}
}
```
```
<?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">
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/bottomNavigationView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="0dp"
app:itemBackground="@color/colorPrimary"
app:itemIconTint="@color/white"
app:itemTextColor="@color/white"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/frameLayout"
app:layout_constraintVertical_bias="1.0"
app:menu="@menu/bottom_navigation_main" />
<FrameLayout
android:id="@+id/frameLayout"
android:layout_width="0dp"
android:layout_height="0dp"
android:background="#d4d4d4"
app:layout_constraintBottom_toTopOf="@+id/bottomNavigationView"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
</FrameLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment