Android – findViewById in Fragment


I am trying to create an ImageView in a Fragment which will refer to the ImageView element which I have created in the XML for the Fragment. However, the findViewById method only works if I extend an Activity class. Is there anyway of which I can use it in Fragment as well?

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

The findViewById method has an error on it which states that the method is undefined.

Best Solution

Use getView() or the View parameter from implementing the onViewCreated method. It returns the root view for the fragment (the one returned by onCreateView() method). With this you can call findViewById().

public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
    ImageView imageView = (ImageView) getView().findViewById(;
    // or  (ImageView) view.findViewById(; 

As getView() works only after onCreateView(), you can't use it inside onCreate() or onCreateView() methods of the fragment .

Related Question