I am new to android programming i have confusion between View.findViewById() and Activity.findViewById(), when do we have to call these methods and in which class findViewById() method resides i.e in View class or Activity class.
Any help will be appreciated.
The View version searches the view you call it on and all subviews of it. The Activity one searches the top level view set in setContentView and all subviews of it. The Activity one is equivalent to calling View.findViewById on the contentView of the activity.
Its basically a convenience method to make it easier to search for an id within all the views of an activity without having to remember what the top view in it is.
Related
I keep reading everywhere that you can now use the activity/fragment constructor to define the layout id with AndroidX, but none answer my question.
What if you have others things to do in Activity's onCreate or Fragment's onCreateView ? Can you still use this new constructor ?
For Activities
Yes, it just means that the Activity will call setContentView() with the layout ID you provide as part of calling super.onCreate(savedInstanceState). Otherwise, you can proceed as normal in your onCreate().
For Fragments
Generally, if using the layout ID constructor, you'd want to move your code from onCreateView() to onViewCreated(), which is passed the View that was created by onCreateView().
Of course, if you do want to override onCreateView() for some reason, you'd want to call super.onCreateView() to retrieve the inflated layout corresponding with the layotu ID you provided to the constructor argument.
I realize views cannot be "found" if they're not a direct child of the layout you're looking from. I have an Activity, which has a ViewPager inside of it. The 0th tab or view inside of the ViewPager has a my_fragment.xml inflated in it. That fragment then has a TextView inside of it. How can i access it from my main/root activity. Also, in the onCreate method of the fragment i've put a line of code that just calls setText on the TextView, and that doesn't seem to be called at all. Any ideas on all of this?
To access a view you need not access every parent / super parent of it. The parent or any other parent up in the view hierarchy is enough. If you get the reference to any parent/super parent of the child view, you can call View.findViewById() to get the child.
View.findViewById() - Look for a child view with the given id. If this view has the given id, return this view.
Don't try to setText in the onCreate of Fragment. You need to do this after the fragment inflates the view. This happens only in the onCreateView. So you need to do the setText in the onCreateView method or in any other method after this one in the lifecycle (preferably onActivityCreated()).
To access the fragment class instance itself from the activity, you can refer this: Is it possible to access the current Fragment being viewed by a ViewPager?
What is the difference between
getActivity().findViewById(...)
and
View view = inflater.inflate(R.layout.fragment_fragment_v, null);
view.findViewById(...)
in Fragment (when converting Activity to Fragment)?
The difference is that with getActivty.findViewById(...) you are finding views in the scope of activity (activity's layout). With iflater.inflate(R.layout.fragment_fragment_v, null); view.findViewById(...) you are inflating the layout of your fragment and then finding view's in that layout.
But since your fragment is attached to the activity, you will find the view bothways, but I suggest you are finding view's for your fragment in your fragment's scope since there may be several fragments that have common layouts meaning there may be several view's associated with the same ID and that makes the getActivity().findBiewById(...) method unreliable
When you call findViewById with getActivity() you are telling Android to find the View within the Activity's layout, since Fragments are hosted inside Activities it will return you the correct View if its in the hierarchy.
By calling findViewById with the inflated View or in fact the getView() method provided by the framework you are essentially telling Android to find that View within the Fragment's view hierarchy, this should be your preferred approach as the findViewById algorithm will have to look for your View from a lesser set of View's and hence the search will be faster.
I'll leave you with this explanation about getView() from the developer docs.
getView()
Get the root view for the fragment's layout (the one returned by onCreateView(LayoutInflater, ViewGroup, Bundle)), if provided.
I have a custom view which overrides a method onLayout, and it is in one of my Fragment. I found when this view is created, the onLayout method just keeps being called. I think it's not my view's problem because I have tried several different custom views which extend ImageView or SurfaceView, all have the same problem. When I put my view in an Activity, the method onLayout is just called twice and I know it's ok. Anyone know why and how to fix it?
P.S. my Fragment is added using class FragmentTransaction in my code, is that the reason?
I basicall have an AsyncTask (run from main Activity) that populates a ViewPager inside a fragment. I'm inflating an xml layout file to populate the ViewPager.
The problem is that I can't get pointers to the views inside the layout (imageview, textview) so that I can change then at runtime. I know it's probably because getView() returns null. I've read that it's probably because onCreateView() hasn't been called, yet. Anybody know what I can do to solve this?
My code's a bit of a mess right now.
Here's a simpler explanation to what I'm doing:
MainActivity Asynctask populates a database and sends pointer to Fragment. Something like SendToFragement(db);
The Fragment method ReceiveFromActivity(db) receives db pointer and populates the ViewPager.
It works fine if I'm just creating TextViews, setting text, and adding TextViews to the Viewpager. But, of course, I want to make it look better so I've inflated an xml layout. The problem is that I can't change the contents of the xml layout because getView() is returning NULL so getView().findViewById() doesn't work.
Depending on the implementation of your PagerAdapter, it is possible that a Fragment no longer in view will have their View destroyed in order to free up resources. As such, there's no need to update the View because it will be created again in onCreateView() with the updated information. getView() will return a view in between calls to onCreateView() and onDestroyView(). Outside of that, the Fragment can still exist in memory, but not attached to any View, thus getView() will return null.
So basically,
View fragmentView = getView();
if(fragmentView != null) {
// we are in view or at least exist. Update the elements.
}
// Else don't worry about it. Just update the data.