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);
}
}
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);
© 2024 OneMinuteCode. All rights reserved.