1190

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(R.id.my_image);
        return inflater.inflate(R.layout.testclassfragment, container, false);
    }
}

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

3
  • Use ButterKnife viewbinding library for android. Also demonstrate how it’s work, how to integrate and use in your android app development to make your development faster.
    – Shomu
    Oct 30, 2018 at 4:37
  • 'Some' time has passed but you still haven't accepted an answer. Can you select the answer that helped you most so this can be marked as answered?
    – David3103
    Apr 9, 2019 at 8:45
  • It is encourages to use Data Binding orang View Binding instead manual findViewById
    – mochadwi
    Jan 10, 2020 at 11:51

37 Answers 37

1607

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().

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

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

20
  • 340
    Note: it's works only after onCreateView(). So, you can't use this in onCreate() Jan 14, 2012 at 19:58
  • 7
    so what is the function I can override to implement this if onCreate is not the right place ?
    – Nico AD
    Jul 24, 2012 at 11:47
  • 17
    This doesn't help if the ImageView is coming from the inflated layout - see my answer for details. Otherwise, onCreateView is the right place to do this @N-AccessDev
    – MattJenko
    Apr 11, 2013 at 10:48
  • 25
    The best place to do this the onViewCreated method
    – advantej
    Jul 11, 2013 at 15:20
  • 37
    The documentation states that onActivityCreated() is the recommended place to find and store references to your views. You must clean up these stored references by setting them back to null in onDestroyView() or you will leak the Activity.
    – Monstieur
    Jun 5, 2014 at 15:15
667

You need to inflate the Fragment's view and call findViewById() on the View it returns.

public View onCreateView(LayoutInflater inflater, 
                         ViewGroup container, 
                         Bundle savedInstanceState) {
     View view = inflater.inflate(R.layout.testclassfragment, container, false);
     ImageView imageView = (ImageView) view.findViewById(R.id.my_image);
     return view;
}
11
  • 4
    When you do V.findViewById(R.id.someid),surely that will only work for all the widgets that are in the inflated view. What if the imageView he is trying to inflate is outside the inflated view?
    – Raunak
    Oct 2, 2011 at 0:33
  • 21
    Then the class that "owns" and inflated the imageView needs to provide public access to it. That is very bad practice though. Fragments should only have access to the UI elements present in their layout. Oct 14, 2011 at 6:18
  • 1
    Looks like there's something wrong in your code (updating UI from a background thread), not mine. Sep 13, 2014 at 18:58
  • 1
    Thanks, it was useful. As unrelated comment: try to stick to Java naming conventions in your code. "V" does not look like a variable name in Java.
    – altumano
    Oct 12, 2014 at 12:09
  • 4
    This should be the right answer. The accepted one leaves you with NullPointerException.
    – Machado
    Feb 19, 2015 at 16:09
155

Inside Fragment class you will get onViewCreated() override method where you should always initialize your views as in this method you get view object using which you can find your views like :

@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    view.findViewById(R.id.yourId).setOnClickListener(this);

    // or
    getActivity().findViewById(R.id.yourId).setOnClickListener(this);
}

Always remember in case of Fragment that onViewCreated() method will not called automatically if you are returning null or super.onCreateView() from onCreateView() method. It will be called by default in case of ListFragment as ListFragment return FrameLayout by default.

Note: you can get the fragment view anywhere in the class by using getView() once onCreateView() has been executed successfully. i.e.

getView().findViewById("your view id");
10
  • 7
    I am surprised not a lot of people have upvoted this response--this is the correct way to setup listeners on fragments... Perhaps this was a new development in the API.
    – Sonny
    Oct 8, 2014 at 22:11
  • Using view passed to onViewCreated still causes NullPointerException but using getActivity() is fine. Any ideas why?
    – Dmitry
    Aug 28, 2015 at 4:20
  • @dVaffection It may be that you are not returning a non null view in onCreateView() lifecycle method of fragment. Now in case of getActivity you are getting views from your activity rather than fragment main view depends upon what id you are passing. Please check are you returning a non null view from onCreateView or not? Then let me know. Aug 28, 2015 at 9:08
  • @AnkurChaudhary I return view from onCreateView method. I've just debugged and it turns out there is a layout (in my case FrameLayout). That being said when I try to find an element it returns null. Why is it happening?
    – Dmitry
    Sep 4, 2015 at 2:12
  • @dVaffection can you please share your class of fragment and corresponding layout. Sep 4, 2015 at 4:55
70

What is required of imageView - are we passing it back as the view, or merely saving a reference for later?

Either way, if the ImageView is coming from the inflated layout, the correct way to do this would be:

public class TestClass extends Fragment {
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.testclassfragment, container, false);
        ImageView imageView = (ImageView)v.findViewById(R.id.my_image);
        return v;
    }
}
3
  • 1
    How then would you update imageView from ListFragment? Would this require FragmentManager? In other words, how can you update the imageView in a detail fragment from a separate class's onListItemClick?
    – whyoz
    Apr 19, 2013 at 1:01
  • 1
    You would either save a reference to the imageView somewhere handy, or fragment.getView().findViewById(R.id.my_image) when you need it. In a ListFragment, assuming the image is in a list item, you would generally create a reference holder with the setTag/getTag methods of the list view item in your Adapter's getView method - there are many examples of how to do this.
    – MattJenko
    Oct 15, 2013 at 7:05
  • @MattJenko, can you share more details or sample how to do have working solution when the inflated layout is used? for instance, i have an viewpager2 with tablayout (4 tabs ) mediated, and I am inflating the edittext views in fragment, and I need to access the editext for each viewpager2's view from main activity. How to do it exactly? Feb 4, 2022 at 10:44
54

Get first the fragment view and then get from this view your ImageView.

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.testclassfragment, container, false);
    ImageView imageView = (ImageView) view.findViewById(R.id.my_image);
    return view;
}
4
  • So, does onCreate method is useful in a Fragment ?
    – Tsunaze
    Jun 28, 2011 at 15:59
  • onCreateView creates and returns the view hierarchy associated with the fragment.onCreate is called to do initial creation of the fragment. Indeed, it depends on what you write in these methods.
    – xevincent
    Jun 28, 2011 at 16:39
  • Okay, but how can i declare variable in the onCreate ? Because the View is inside the onCreateView method .
    – Tsunaze
    Jun 28, 2011 at 18:47
  • @Tsunaze did you ever find out how to do this from the onCreate method? Jun 25, 2012 at 15:27
33

Inside Fragment class we get onViewCreated() override method where we should always initialize our views because in this method we get view object. Using this object we can find our views like below:

class MyFragment extends Fragment {
    private ImageView imageView;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.my_fragment_layout, container, false);
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        //initialize your view here for use view.findViewById("your view id")
        imageView = (ImageView) view.findViewById(R.id.my_image);
    }
}
0
30

You could also do it in the onActivityCreated Method.

public void onActivityCreated(Bundle savedInstanceState) { 
      super.onActivityCreated(savedInstanceState);
}

Like they do here: http://developer.android.com/reference/android/app/Fragment.html (deprecated in API level 28)

getView().findViewById(R.id.foo);

and

getActivity().findViewById(R.id.foo);

are possible.

1
  • Note that API 28 introduces requiereActivity(), requireView(), requireViewById(). Feb 4, 2022 at 10:45
21

getView() will give the root view

View v = getView().findViewByID(R.id.x); 
19

You can override onViewCreated() which is called right after all views had been inflated. It's the right place to fill in your Fragment's member View variables. Here's an example:

class GalleryFragment extends Fragment {
    private Gallery gallery;

    (...)

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        gallery = (Gallery) view.findViewById(R.id.gallery);
        gallery.setAdapter(adapter);
        super.onViewCreated(view, savedInstanceState);
    }
}
17

The method getView() wont work on fragments outside OnCreate and similar methods.

You have two ways, pass the view to the function on the oncreate (what means you can only run your functions when the view is being created) or set the view as a variable:

private View rootView;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
    Bundle savedInstanceState) {
    rootView = inflater.inflate(R.layout.fragment_contatos, container, false);
}

public void doSomething () {
    ImageView thumbnail = (ImageView) rootView.findViewById(R.id.someId);
}
16

First inflate layout of Fragment then you can use findviewbyId.

View view = inflater.inflate(R.layout.testclassfragment, container, false);
             ImageView imageView = (ImageView) view.findViewById(R.id.my_image);
             return view;
0
12
EditText name = (EditText) getView().findViewById(R.id.editText1);
EditText add = (EditText) getView().findViewById(R.id.editText2);  
11

Note :

From API Level 26, you also don't need to specifically cast the result of findViewById as it uses inference for its return type.

So now you can simply do,

public View onCreateView(LayoutInflater inflater, 
                         ViewGroup container, 
                         Bundle savedInstanceState) {
     View view = inflater.inflate(R.layout.testclassfragment, container, false);
     ImageView imageView =  view.findViewById(R.id.my_image); //without casting the return type
     return view;
}
2
  • 1
    Without casting thing explained helped me. Nov 3, 2017 at 21:20
  • I suggest to combine this answer with one mentioning deprecation of getView() in API 28. Feb 4, 2022 at 10:58
11

Agreed with calling findViewById() on the View.

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View V = inflater.inflate(R.layout.testclassfragment, container, false);
    ImageView imageView = (ImageView) V.findViewById(R.id.my_image);

    return V;
}
10

Use

imagebutton = (ImageButton) getActivity().findViewById(R.id.imagebutton1);

imageview = (ImageView) getActivity().findViewById(R.id.imageview1);

it will work

9

According to the documentation on API level 11

Reference, in Back Stack http://developer.android.com/reference/android/app/Fragment.html

short code

/**
 * The Fragment's UI is just a simple text view showing its
 * instance number.
 */
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.hello_world, container, false);
    View tv = v.findViewById(R.id.text);
    ((TextView)tv).setText("Fragment #" + mNum);
    tv.setBackgroundDrawable(getResources().getDrawable(android.R.drawable.gallery_thumb));
    return v;
}
9

Using getView() returns the view of the fragment, then you can call findViewById() to access any view element in the fragment view.

1
  • 3
    the view is not the activity
    – Steve M
    Sep 25, 2013 at 2:24
9

Try this it works for me

public class TestClass extends Fragment {
    private ImageView imageView;

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.testclassfragment, container, false);
        findViews(view);
        return view;
    }

    private void findViews(View view) {
        imageView = (ImageView) view.findViewById(R.id.my_image);
    }
}
3
  • Yes, this code here is okay. Your edits to the other are not. Jun 11, 2017 at 20:15
  • @cricket_007 ok which one can you saw me? Jun 11, 2017 at 20:17
  • I'm sorry I can't quite understand your English, but everything is fixed now. I have nothing to show you. Jun 11, 2017 at 20:18
7
ImageView imageView;
public View onCreateView(LayoutInflater inflater, 
                     ViewGroup container, 
                     Bundle savedInstanceState) {
 View view = inflater.inflate(R.layout.testclassfragment, container, false);
 imageView = view.findViewById(R.id.my_image);
 return view;
}
6

1) Declare your layout file.

public View onCreateView(LayoutInflater inflater,ViewGroup container, 
                                 Bundle savedInstanceState) {
    return inflate(R.layout.myfragment, container, false);
}

2)Then, get the id of your view

@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    TextView nameView = (TextView) view.findViewById(R.id.textview1);
}
5

Try This:

View v = inflater.inflate(R.layout.testclassfragment, container, false);
ImageView img = (ImageView) v.findViewById(R.id.my_image);

return v;
5

Try

private View myFragmentView;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) 
{
myFragmentView = inflater.inflate(R.layout.myLayoutId, container, false);
myView = myFragmentView.findViewById(R.id.myIdTag)
return myFragmentView;
}
1
  • There's no reason for the field. You can always call getView() later Jun 8, 2017 at 12:14
5

The best way to implement this is as follows:

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

rootView = inflater.inflate(R.layout.testclassfragment, container, false);
        ImageView imageView = (ImageView) rootView.findViewById(R.id.my_image);
        return rootView
}

In this way, the rootView can be used for each control defined in the xml layout and the code is much cleaner in this way.

1
  • i thinked this could be posiible, but , it didn't work Dec 31, 2014 at 4:24
4

Use gradle skeleton plugin, it will automatically generate the view holder classes with the reference to your layout.

public class TestClass extends Fragment {
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        MyLayout myLayout = new MyLayout(inflater, container, false);
        myLayout.myImage.setImageResource(R.drawable.myImage);
        return myLayout.view;
    }
}

Now assuming you had an ImageView declared in your my_layout.xml file, it will automatically generate myLayout class for you.

4

I like everything to be structured. You can do in this way.

First initialize view

private ImageView imageView;

Then override OnViewCreated

@Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        findViews(view);
    }

Then add a void method to find views

private void findViews(View v) {
    imageView = v.findViewById(R.id.img);
}
4
//here you can do it by
   public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState)
  {
    // Inflate the layout for this fragment
    final View view =  inflater.inflate(R.layout.fragment_apple, container, 
 false);
    ist = view.findViewById(R.id.linearLink);
    second = view.findViewById(R.id.linearPhone);
    return view;
0
3

You can call findViewById() with the Activity Object you get inside your public void onAttach(Activity activity) method inside your Fragment.

Save the Activity into a variable for example:

In the Fragment class: private Activity mainActivity; In the onAttach() method: this.mainActivity=activity;

Finally execute every findViewById through the vairable: mainActivity.findViewById(R.id.TextView);

3

Inside onCreateView method

1) first you have to inflate the layout/view you want to add eg. LinearLayout

LinearLayout ll = inflater.inflate(R.layout.testclassfragment, container, false);

2) Then you can find your imageView id from layout

ImageView imageView = (ImageView)ll.findViewById(R.id.my_image);

3)return the inflated layout

return ll;
1
  • This is identical to LeffelMania's answer.
    – Reti43
    Dec 6, 2015 at 19:10
3

You have to inflate the view

public class TestClass extends Fragment {

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

There is one more method called onViewCreated.

@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    ImageView imageView = (ImageView) view.findViewById(R.id.imageview1);
}

Not the answer you're looking for? Browse other questions tagged or ask your own question.