I have an ActionBarActivity
and fragment. I am using FragmentPagerAdapter
that provides fragment to my app. My question How can I access parent Activity View in Fragment ??
Asked
Active
Viewed 6.4k times
75

Bot
- 2,285
- 3
- 17
- 20
5 Answers
188
You can use
View view = getActivity().findViewById(R.id.viewid);
Quoting docs
Specifically, the fragment can access the Activity instance with getActivity() and easily perform tasks such as find a view in the activity layout

Raghunandan
- 132,755
- 26
- 225
- 256
-
Works, but I would use an interface to communicate with the activity or something like that. – DeliriumTremens Apr 05 '14 at 18:25
-
1your welcome to use the interface way also. But that is for communicating with activity – Raghunandan Apr 05 '14 at 18:26
-
6Yep who knows what the state is going to be, hacky I know but I have lunch to eat. – Kevin Parker Feb 25 '15 at 16:24
-
1Cool, just you would need to check if Fragment is added by isAdded() to avoid crash if activity is not attached yet. – Maher Abuthraa May 14 '17 at 18:34
-
@Raghunandan. Doesn't work if I rotate the device. That time the view becomes null and i get a NPE – zulkarnain shah Aug 03 '18 at 04:10
-
Works fine for me.Thanks – Md Nakibul Hassan May 23 '19 at 07:08
-
always get access from last fragment if I try to access from first or second fragment – Zahidul Aug 07 '19 at 05:01
-
2Android docs are written in a very stupid way. Docs quoted as `Specifically, the fragment can access the Activity instance with getActivity() and easily perform tasks such as find a view in the activity layout` yet Android Studio warning reads as `getActivity() may be null` – Farid Aug 10 '19 at 13:36
-
@FARID feel free to file a issue with google at android issues tracker. The docs might be updated. the warning is a lint warning. getactivity can be null if you try to access it even before the fragment is attached to the activity. – Raghunandan Aug 10 '19 at 16:58
-
for Kotlin val view = activity!!.findViewById
(R.id.view) – MohammadReza Nov 16 '20 at 08:49 -
Can we use the activity view reference in fragment xml ? – AndroidDev Jun 23 '21 at 09:51
-
you can refer to a activity view in fragment – Raghunandan Jun 23 '21 at 11:02
-
I ended up using `final FloatingActionButton fab = requireActivity().findViewById(R.id.fab);` – Steve Gelman Jan 28 '23 at 17:49
-
@SteveGelman yes you can use requireActivity() or requireContexgt(). for more info and when to use what https://medium.com/android-news/the-requireactivity-and-requirecontext-example-1c089ce11a3a – Raghunandan Jan 30 '23 at 05:13
9
At first, create a view like this:
View view = getActivity().findViewById(R.id.viewid);
Then convert it to any view that you need like this:
if( view instanceof EditText ) {
editText = (EditText) view;
editText.setText("edittext");
//Do your stuff
}
or
if( view instanceof TextView ) {
TextView textView = (TextView) view;
//Do your stuff
}

Mohsen mokhtari
- 2,841
- 1
- 30
- 39
9
In Kotlin it is very easy to access parent Activity View in Fragment
activity!!.textview.setText("String")

kishan verma
- 984
- 15
- 17
2
Note that if you are using findViewById<>() from activity, it wont work if you call it from fragment. You need to assign the view to variable. Here is my case
This doesn't work
class MainActivity{
fun onCreate(...){
//works
setMyText("Set from mainActivity")
}
fun setMyText(s: String){
findViewById<TextView>(R.id.myText).text = s
}
}
________________________________________________________________
class ProfileFragment{
...
fun fetchData(){
// doesn't work
(activity as MainActivity).setMyText("Set from profileFragment")
}
}
This works
class MainActivity{
private lateinit var myText: TextView
fun onCreate(...){
myText = findViewById(R.id.myText)
// works
setMyText("Set from mainActivity")
}
fun setMyText(s: String){
myText.text = s
}
}
________________________________________________________________
class ProfileFragment{
...
fun fetchData(){
// works
(activity as MainActivity).setMyText("Set from profileFragment")
}
}

Irfandi D. Vendy
- 894
- 12
- 20
2
if you are using kotlin then you can use this syntax
val view= requireActivity().findViewById<YourVIEW>(R.id.view_id)

Yazdan Ilyas
- 374
- 4
- 8