Search code examples
androidandroid-fragmentsviewfindviewbyid

how to access views in fragments?


I am new in android and I have been created simple app which have 3 tabs that I build them with Fragment Activity .

in Activities we can use findViewById method . but Fragments don't have this method . how can I access to my views ?


Solution

  • You can call your getAcivity() method within your fragment class to call the context of your activity that is bounded in it and after you can call findViewById();

    sample:

    public void onViewCreated() 
        TextView txSample = (TextView)getView().findViewById(R.id.yourid);