findViewById within fragment
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(...);
Related videos on Youtube
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, 2022Comments
-
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 over 11 yearsAdd your code, it will be alot easier to answer.
-
Sufian over 7 yearsPossible duplicate of findViewById in Fragment
-
-
dennisdrew over 11 yearsMrFox, 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 over 11 yearsSorry! 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 over 11 yearsSometimes getView() is null, so then what would you do?
-
Michael Celey over 11 years
getView()
is only null beforeonCreateView()
is called by the fragment. At that point there is no view to be found usingfindViewById()
because no view has been created. -
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 over 11 yearsWhen a fragment is detached
getView()
should also returnnull
.getView()
will only return a non-null value afteronCreateView
and beforeonDestroyView
. You can see the full lifecycle here -
Spinner over 10 yearsI 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 over 10 yearsthis is a typo, v should be
myFragmentView
. Will change now