I would like to implement a toolbar in the fragment. I am using binding to use elements from .xml. I implement in Kotlin, android studio.
I have seen: Unable to show toolbar while using databinding in Android and many other articles, documentation as well, but everywhere I cannot find the proper implementation with binding.
toolbar.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.appcompat.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/teal_700"
android:elevation="4dp">
</androidx.appcompat.widget.Toolbar>
in fragment.xml
<include
android:id="@+id/toolbar"
layout="@layout/toolbar" />
fragment.kt
Here I have tried many different implementations. The main issue is when I make it with documentation and instead of define toolbar using findById I define it by binding.toolbar where misstype appears where it wants toolbar? not binding toolbar.
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
// doesn't work setConentView and setSupportACtionBar on Red
binding = FragmentItemSecondBinding.setContentView(this, R.layout.fragment_item_second)
setSupportActionBar(binding.toolbar)
binding.setProduct(product);
binding = FragmentItemSecondBinding.inflate(layoutInflater)
return binding.root
}
In documentation and other videos it should work when I make code like below, but setSupportActionBar doesn't exist.
val toolbar = binding.toolbar
setSupportActionBar(toolbar)
What is difference between:
androidx.appcompat.widget.Toolbar and android.support.v7.widget.Toolbar
I use the first one. My goal is to have two buttons in toolbar to have possibility to back to previous fragment + onClickSecondButton make some action.
EDIT: TO Nukhoca