25

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.

user
  • 86,916
  • 18
  • 197
  • 190
Peri Hartman
  • 19,314
  • 18
  • 55
  • 101

5 Answers5

57
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;
}
chris-tulip
  • 1,840
  • 1
  • 15
  • 22
  • 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. – dennisdrew Sep 21 '12 at 18:51
  • 1
    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 – chris-tulip Sep 21 '12 at 18:55
  • 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? – Spinner Oct 07 '13 at 10:34
  • this is a typo, v should be `myFragmentView`. Will change now – chris-tulip Oct 07 '13 at 16:12
24

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);
Michael Celey
  • 12,645
  • 6
  • 57
  • 62
  • 2
    Sometimes getView() is null, so then what would you do? – StackOverflowed Sep 25 '12 at 23:23
  • 3
    `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. – Michael Celey Sep 26 '12 at 12:42
  • @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. – StackOverflowed Sep 26 '12 at 13:03
  • 1
    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](http://developer.android.com/guide/components/fragments.html#Creating) – Michael Celey Sep 26 '12 at 13:31
6

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

500865
  • 6,920
  • 7
  • 44
  • 87
1

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

slezadav
  • 6,104
  • 7
  • 40
  • 61
0
  public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View rootview=null;

             rootview=inflater.inflate(R.layout.fragment_web_view, container, false);
           ListView lv = (ListView)rootview.findViewById(android.R.id.list);

return rootview;
        // Inflate the layout for this fragment
        //return inflater.inflate(R.layout.fragment_web_view, container, false);
    }
sms247
  • 4,404
  • 5
  • 35
  • 45