I am using TabLayout with ViewPager. I have 3 fragments and 3 tabs. I want to access the FloatingActionButton in activitymain.xml file.
In every fragment I write a code like this:
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
val fab :View= (requireActivity() as MainActivity).findViewById(R.id.fab_main)
fab.setOnClickListener {
Toast.makeText(context,"Fragment1",Toast.LENGTH_SHORT).show()
}
}
But when I click the FloatingActionButton in every fragment it gives me the same Toast message which is related to the last fragment. I want to perform different actions in every fragment when I clicked the button.