Skip to content

Instantly share code, notes, and snippets.

@amoozeshbebin
Created September 6, 2023 09:54
Show Gist options
  • Select an option

  • Save amoozeshbebin/fec3cb254a67389278bb7891846fac00 to your computer and use it in GitHub Desktop.

Select an option

Save amoozeshbebin/fec3cb254a67389278bb7891846fac00 to your computer and use it in GitHub Desktop.
NavigationBar Android/XML/Kotlin
MainActivity.kt:
package com.example.test
import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.ArrayAdapter
import android.widget.Button
import android.widget.ListView
import com.google.android.material.bottomnavigation.BottomNavigationView
class MainActivity : AppCompatActivity() {
lateinit var buttonNavigationView: BottomNavigationView
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
init()
}
fun init(){
bindView()
var transaction = supportFragmentManager.beginTransaction()
buttonNavigationView.setOnItemSelectedListener { item ->
when(item.itemId) {
R.id.item_1 -> {
var transaction = supportFragmentManager.beginTransaction()
transaction.replace(R.id.freame_layout,fragment1())
transaction.commit()
true
}
R.id.item_2 -> {
var transaction = supportFragmentManager.beginTransaction()
transaction.replace(R.id.freame_layout,fragment2())
transaction.commit()
true
}
else -> false
}
}
}
fun bindView(){
buttonNavigationView = findViewById(R.id.bottom_navigation)
}
}
________________________________________________________________________________________________________________________________
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
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"
android:orientation="vertical"
android:weightSum="10">
<FrameLayout
android:id="@+id/freame_layout"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="10"/>
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/bottom_navigation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:menu="@menu/bottom_navigation_menu" />
</LinearLayout>
________________________________________________________________________________________________________________________
create fragment1 and fragment2+
fragment1.xml:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".fragment1">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="fragment1"
android:textSize="30sp"
android:layout_gravity="center"
android:gravity="center" />
</FrameLayout>
___________________________________________________________________________________________
fragment2.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".fragment2">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="fragment2"
android:textSize="30sp"
android:layout_gravity="center"
android:gravity="center" />
</FrameLayout>
___________________________________________________________________________________________
fragment1.kt:
package com.example.test
import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
class fragment1 : Fragment() {
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment1, container, false)
}
}
___________________________________________________________________________________________
fragment2.kt:
package com.example.test
import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
class fragment2 : Fragment() {
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment2, container, false)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment