findViewById within fragment

58,108

Solution 1

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;
}

Solution 2

From inside the Fragment:

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

From the enclosing Activity:

getFragmentManager().findFragmentByTag("YourFragmentTag").getView().findViewById(R.id.your_view);

or

getFragmentManager().findFragmentById(R.id.your_fragment).getView().findViewById(R.id.your_view);

Solution 3

You can do it by getView().findViewById()

Solution 4

Yes, there is a way, you can find it through rootView. First find the rootView of your fragment rootView=getView(); and then use rootView.findViewById(...);

Share:
58,108

Related videos on Youtube

Peri Hartman
Author by

Peri Hartman

Software developer for many years, having done an OS for an early hand held in assembly; many projects in C and C++ mostly for Windows; front-end web server handling; in 2010 started with Java & Swing for a desktop app; currently developing an Android app.

Updated on July 09, 2022

Comments

  • Peri Hartman
    Peri Hartman almost 2 years

    Is there any way to find a view by id within the scope of a fragment? I'm using a series of fragments to render a specialized list. The fragments are loaded from a layout, so their widgets all have the same ids.

    I suppose I can figure out a way to give each widget a custom id during (or right after) creation. However, it would be a lot nicer if I could somehow limit the findViewById to the scope of the fragment.

    • tolgap
      tolgap over 11 years
      Add your code, it will be alot easier to answer.
    • Sufian
      Sufian over 7 years
      Possible duplicate of findViewById in Fragment
  • dennisdrew
    dennisdrew over 11 years
    MrFox, make sure you note that the view must be inflated. So, I would call inflater.inflate(v, container). That, or you could do MCeley's first suggestion, but make sure this is done in onActivityCreated so the View has been inflated and such. Otherwise, if you do it in onCreateView with no inflated view, you'll get a null pointer.
  • chris-tulip
    chris-tulip over 11 years
    Sorry! I totally missed that since I just pulled from some code that I had up and the inflate was in the super my bad
  • StackOverflowed
    StackOverflowed over 11 years
    Sometimes getView() is null, so then what would you do?
  • Michael Celey
    Michael Celey over 11 years
    getView() is only null before onCreateView() is called by the fragment. At that point there is no view to be found using findViewById() because no view has been created.
  • StackOverflowed
    StackOverflowed over 11 years
    @McCeley If getView() is called after the Fragment has been detached, does it also return null? I'm just curious as to when getView() call of a Fragment returns null.
  • Michael Celey
    Michael Celey over 11 years
    When a fragment is detached getView() should also return null. getView() will only return a non-null value after onCreateView and before onDestroyView. You can see the full lifecycle here
  • Spinner
    Spinner over 10 years
    I can't see any reference to the variable v other than in the return statement. Is this a typo, and if so what variable should it be?
  • chris-tulip
    chris-tulip over 10 years
    this is a typo, v should be myFragmentView. Will change now