Closed yleyvag closed 4 years ago
There is no extra attribute to set the default item via XML.
Update your library version to 1.1.2
and call menu.setItemSelected(R.id.your_menu_id)
in your Activity/Fragment onCreate with the default item.
Let me know if this solves your problem
Thank you very much. It works.
Where I update the library? I'm using Android Studio in Java.
I use library version 1.3.1
and call menu.setItemSelected(R.id.my_menu_id)
in my Activity onCreate, not work for me
@mandanai Are you using java? 🤔 I think it's missing a JvmOverloads on the setItemSelected
For now, please try:
menu.setItemSelected(R.id.my_menu_id, true)
I using Kotlin. I using both not work for me. 😥
menu.setItemSelected(R.id.my_menu_id, true)
and
menu.setItemSelected(R.id.my_menu_id)
Please share your setup (layout, menu, activity)
class MainActivity : AppCompatActivity() {
private val binding: ActivityMainBinding by lazy { ActivityMainBinding.inflate(layoutInflater) }
private val viewModel: MainViewModel = MainViewModel()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
loadFragment(R.id.menu_map)
binding.menuBottom.setItemSelected(R.id.menu_map, true) **_// not work_**
binding.menuBottom.setOnItemSelectedListener {
**_// below not work_**
Log.d(MainActivity::class::simpleName.toString(), "Item selected")
loadFragment(it)
}
}
private fun loadFragment(itemId: Int) {
....
...
}
}
Layout:: activity_main
<?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"
android:layout_height="match_parent"
android:layout_width="match_parent">
<FrameLayout
android:id="@+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="0dp"
app:layout_constraintBottom_toTopOf="@+id/menu_bottom"
app:layout_constraintTop_toTopOf="parent" />
<com.ismaeldivita.chipnavigation.ChipNavigationBar
android:id="@+id/menu_bottom"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:cnb_menuResource="@menu/bottom_menu" />
</androidx.constraintlayout.widget.ConstraintLayout>
Menu: bottom_menu
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/menu_nearby"
android:title="@string/menu_nearby"
android:icon="@drawable/ic_location_on_black"
app:cnb_iconColor="@color/color_menu_home"/>
<item
android:id="@+id/menu_map"
android:title="@string/menu_map"
android:icon="@drawable/ic_map"
app:cnb_iconColor="@color/color_menu_map"/>
<item
android:id="@+id/menu_favorite"
android:title="@string/menu_favorite"
android:icon="@drawable/ic_favorite"
app:cnb_iconColor="@color/color_menu_favorite"/>
</menu>
@mandanai The problem doesn't seem to be in the library. Maybe you're using the view binding incorrectly.
Try something like this:
private lateinit var binding: ActivityMainBinding
...
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)
loadFragment(R.id.menu_map)
binding.menuBottom.setItemSelected(R.id.menu_map)
...
Thank you very much. It works :)
for JAVA use this:
mBottomNavigationBar.setItemSelected(R.id.home, true);
R.id.home - itemId from Menu
How i change item label position, bottom of icon and label is always show.
I need to set one of the items as default selected when the view is shown. Right now all items are unselected. How can I make this?