0

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

enter image description here

chrisu.chrisu
  • 119
  • 2
  • 14
  • It's usually better to just keep the Toolbar in the activity rather than the fragment. Also have a look at this: https://stackoverflow.com/questions/38189198/how-to-use-setsupportactionbar-in-fragment – Daniel Nugent Dec 05 '21 at 16:58
  • I have seen sth similar, but the problem is: Required: Toolbar? Found: ToolbarBinding – chrisu.chrisu Dec 05 '21 at 17:07

1 Answers1

0

If you import external layout using <include /> tag, you need to set its own binding, as well. Change your code by;

  private lateinit binding: FragmentItemSecondBinding
  private lateinit toolbarBinding: ToolbarBinding

  override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        binding = FragmentItemSecondBinding.inflate(inflater, container, false)
        toolbarBinding = ToolbarBinding.bind(binding.toolbar)
        (requireActivity() as YourActivity).setSupportActionBar(toolbarBinding.toolbar) // in toolbar.xml set an ID to your component e.g. toolbar
        binding.setProduct(product);

        return binding.root
    }

Of course, I tried to keep code as short as possible. You should make your bindings nullable and dispose them in onDestroyView to avoid memory leaks.

nuhkoca
  • 1,777
  • 4
  • 20
  • 44
  • check my edit, this method doesn't work in fragment it works only in mainactivity I would like to cast it in fragment, is it possible/? – chrisu.chrisu Dec 05 '21 at 17:05
  • @chrisu.chrisu just do below `(requireActivity() as YourActivity).setSupportActionBar(toolbarBinding.toolbar)` or `(requireActivity() as AppCompatActivity).setSupportActionBar(toolbarBinding.toolbar)` – nuhkoca Dec 05 '21 at 17:52
  • @chrisu.chrisu Did it work? If so can mark my comment as answer? – nuhkoca Dec 07 '21 at 23:17
  • I had the other implementations to do and make it just by id. I will check it and write in 2-3 days for sure. greetings – chrisu.chrisu Dec 09 '21 at 10:50