Reputation: 769
I have created a FloatingActionButton to switch the view between gridView
and listView
of the recyclerView
in an fragment
. I just wanted to make sure that the FloatingActionButton works correctly and So I just added a `toast. However, the toast doesn't show up when I hit the button.
fragment_dashboard.xml
<?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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/colorOffWhite"
tools:context=".ui.fragments.DashboardFragment">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/rv_dashboard_items"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="gone"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="@+id/fb_dashboard"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="10dp"
android:layout_marginBottom="60dp"
android:backgroundTint="@color/colorAccent"
android:clickable="true"
android:focusable="true"
android:src="@drawable/ic_grid_list_view"
app:layout_anchorGravity="bottom|right|end"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent" />
<TextView
android:id="@+id/tv_no_dashboard_items_found"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:text="@string/no_dashboard_item_found"
android:textAlignment="center"
android:textSize="@dimen/no_data_found_textSize"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
DashboardFragment.kt
class DashboardFragment : BaseFragment() {
private var binding: FragmentDashboardBinding? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setHasOptionsMenu(true)
}
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
binding?.fbDashboard?.setOnClickListener { view ->
Toast.makeText(requireContext(), "You clicked FA Button", Toast.LENGTH_LONG).show()
}
val root = inflater.inflate(R.layout.fragment_dashboard, container, false)
return root
}
}
I get the following error when I use the code in the first answer.
Upvotes: 0
Views: 434
Reputation: 769
This is what I used and resolved the issue.
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
binding = FragmentDashboardBinding.inflate(inflater, container, false)
binding.fbDashboard.setOnClickListener {
Toast.makeText(requireContext(), "You clicked FA Button", Toast.LENGTH_LONG).show()
}
return binding.root
}
Upvotes: 0
Reputation: 106
You should assign the binding variable using the DatabindingUtil inflate
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
binding = DataBindingUtil.inflate(
inflater, R.layout.fragment_dashboard, container, false
)
binding?.fbDashboard?.setOnClickListener { view ->
Toast.makeText(requireContext(), "You clicked FA Button", Toast.LENGTH_LONG).show()
}
return binding.root
}
Also, wrap the layout file fragment_dashboard in a layout tag.
<layout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
>
</layout>
Upvotes: 1