I have a small app, where i have a fragment AchievementFragment
and in there i have a few imageButton
s. I want to make it so that when i click on one of them, a toast appears on the screen, but i have a problem with just the imageButton
itself. I tried following a few online tutorials like this one: https://www.geeksforgeeks.org/imagebutton-in-kotlin/, but when i try to use
val imgbtn = findViewById<ImageButton>(R.id.imageBtn)
i get unresolved findViewById reference error.
2
Answers
You can’t use directly findViewById in fragments, you have to use it with root view, in your onCreateView you are returning the root view. Your other views are inside the root view. So if you want to access a view inside root you should use like this before returning the root view
replace "activity name" with your activity.
OR
Use View Binding instead of findViewById.
https://developer.android.com/topic/libraries/view-binding