Skip to content

Instantly share code, notes, and snippets.

@c4software
Last active January 25, 2021 15:46
Show Gist options
  • Save c4software/aebb8f467c229e186d88a04b13a3f406 to your computer and use it in GitHub Desktop.
Save c4software/aebb8f467c229e186d88a04b13a3f406 to your computer and use it in GitHub Desktop.
La version à l'ancienne
<?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=".ui.bottom.BottomActivity">
<androidx.fragment.app.FragmentContainerView
android:id="@+id/frame"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toTopOf="@id/bottom_navigation"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/bottom_navigation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:menu="@menu/bottom_navigation_menu" />
</androidx.constraintlayout.widget.ConstraintLayout>
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/page_1"
android:enabled="true"
android:icon="@drawable/ic_baseline_all_inclusive_24"
android:title="@string/app_name"/>
<item
android:id="@+id/page_2"
android:enabled="true"
android:icon="@drawable/ic_baseline_all_inclusive_24"
android:title="@string/app_name"/>
</menu>
class BottomActivity : AppCompatActivity() {
companion object {
fun getStartIntent(context: Context): Intent {
return Intent(context, BottomActivity::class.java)
}
}
private var page1Instance = Page1.newInstance()
private var page2Instance = Page2.newInstance()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_bottom)
showFragment(page1Instance)
bottom_navigation.setOnNavigationItemSelectedListener { item ->
when (item.itemId) {
R.id.page_1 -> {
// Item 1 action
showFragment(page1Instance)
true
}
R.id.page_2 -> {
// Item 1 action
showFragment(page2Instance)
true
}
else -> false
}
}
}
private fun showFragment(fragment: Fragment) {
supportFragmentManager.commit {
setReorderingAllowed(true)
replace(R.id.frame, fragment, fragment.tag)
}
}
}
class Page1 : Fragment() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
}
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_page1, container, false)
}
companion object {
fun newInstance() = Page1()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment