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?
Asked
Active
Viewed 328 times
0

Axinite
- 193
- 1
- 1
- 6
-
Post your code. We can't say anything without seeing your code. Plus, why do you need to access a TextView from the Activity? – Gokhan Arik Nov 04 '15 at 18:15
-
I'll post it as soon as I can. Well now that i think about it, i don't have to access it from the activity. I can access it from the fragment class. Two problems though; Like i said, nothing i put in the onCreate ( or the afterOnCreate for that matter ) is being called, no errors too. Second thing is that I will need to access the fragment class instance itself from the activity at one point. How would i go about getting access to the fragment from the activity if it's buried in views? It seems that, in order to get a certain view, you need to access every parent of it. To bubble down to it. – Axinite Nov 04 '15 at 18:31
-
The fragment is just a default fragment added via the menu... Main Activitiy Code : http://pastebin.com/Yf5uLBeZ – Axinite Nov 04 '15 at 18:54
1 Answers
0
- 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 theonCreate
ofFragment
. You need to do this after the fragment inflates the view. This happens only in theonCreateView
. So you need to do thesetText
in theonCreateView
method or in any other method after this one in the lifecycle (preferablyonActivityCreated()
).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?
-
So if i called findViewById from the Main Activity ( which contains a TabLayout that contains a fragment with the textview inside ) and looked for e.g. R.id.myTextView it would return NOT null? Because it returned null for me. Also what would happen if i had two of the same fragments, therefore two TextViews with the same ID? – Axinite Nov 04 '15 at 19:02
-
No I meant the `parent` as in the layout that is getting inflated. If you have two TextViews with same id in the layout, then the first one will be found. Hence it's better not to have same id in a single layout. In your case, you need to get the `Fragment` instance and then call findViewById on that, i.e, if you want to access the TextView from the activity. – Henry Nov 04 '15 at 19:03
-
Yes, guessed as much. I'll try that approach later and get back to you. If it works i'll love you :) – Axinite Nov 04 '15 at 19:08
-
I can't implement the link you gave me. My ViewPager is not dynamic. It has two tabs that are always there and are always the same. I need to access the TextView (myTextView) that's located in the in a fragment that's inflated in the first tab of the ViewPager. How can i do that? – Axinite Nov 04 '15 at 20:37
-
Also, i tried setText in onCreateView and it still isn't being called. – Axinite Nov 04 '15 at 20:38