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

2
  • It's usually better to just keep the Toolbar in the activity rather than the fragment. Also have a look at this: stackoverflow.com/questions/38189198/… Commented Dec 5, 2021 at 16:58
  • I have seen sth similar, but the problem is: Required: Toolbar? Found: ToolbarBinding Commented Dec 5, 2021 at 17:07

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

4 Comments

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 just do below (requireActivity() as YourActivity).setSupportActionBar(toolbarBinding.toolbar) or (requireActivity() as AppCompatActivity).setSupportActionBar(toolbarBinding.toolbar)
@chrisu.chrisu Did it work? If so can mark my comment as answer?
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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.