So I'm building this calculator and I used activities but now I have to use fragments instead.
I had these buttons implemented but now that I've moved them in a fragment I cant use findViewById. How can I assign the id to my variables?
Asked
Active
Viewed 376 times
0

Martin Zeitler
- 1
- 19
- 155
- 216

Marius Anton
- 23
- 2
-
1Does this answer your question? [findViewById in Fragment](https://stackoverflow.com/questions/6495898/findviewbyid-in-fragment) – Martin Zeitler May 18 '22 at 13:45
2 Answers
0
Notice that onViewCreated
has a view
parameter. You can call it on that so like view.findViewById(R.id.text)

Ivo
- 18,659
- 2
- 23
- 35
0
I would suggest to use viewBinding, and if you are not comfortable with that then you should do it in onCreateView() method of your fragment. Like this,
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View {
view = inflater.inflate(R.layout.your_layout_file, container, false)
val imageView = view.findViewById<ImageView>(R.id.id_of_imageview)
return view
}

Bhavnik
- 2,020
- 14
- 21