How to do findViewById in Fragment

Asked 2 years ago, Updated 2 years ago, 95 views

I'm going to make ImageView in Fragment and use it, but the image view is made in xml. But you can't use the findViewById method when you access the image view in Fragment. I heard it's only available in activities. What should I do?

public class TestClass extends Fragment {
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    ImageView imageView = (ImageView)findViewById(R.id.my_image);
    return inflater.inflate(R.layout.testclassfragment, container, false);
    }
}

android android-fragments android-imageview

2022-09-22 22:35

1 Answers

Use getView You can get a view of the root of Fragment, and you can use findViewById() with it

Example.

ImageView imageView = (ImageView) getView().findViewById(R.id.foo);


2022-09-22 22:35

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.