This question already has answers here:
How to access Fragment's child views inside fragment's parent Activity?
(17 answers)
Closed 5 years ago.
I know how to get an instance of fragment inside an activity from that activity.
but i don't know how to change properties of View objects like TextView,Button inside a fragment from activity.
anybody can help, please give an example for how to do that.
Just use findViewById() from your activity. Once the fragment is attached to the activity and its view is created all views are part of the view hierarchy ergo you can retrieve references to them from the activity
Related
I'm developing an android app and I have a fragment that contains TextView & ListView in it. The list view has custom list items that contains two buttons. I want to make 'onClickListener' for one of those two buttons in my custom adapter class to change the text of the TextView, but I can't access it by findViewById() every time I try I got null exception.
My guess is that you're trying to access the TextView from inside of the Adapter.
If so, then you won't be able to get the TextView and it's normal to get a nullpointer exception.
The findViewById inside your adapter only finds the views that you have inflated in the getView() method of your adapter.
What you can do here is probably use an interface that's implemented by your Fragment to pass the information from your adapter back to the fragment.
This answer might be a good starting point : How to create interface between Fragment and adapter?
This question already has answers here:
Update ViewPager dynamically?
(21 answers)
Closed 5 years ago.
I have ViewPager with fragments. The adapter gets fragments from override method getItem ().
#Override
public Fragment getItem(int position) {
return new MyFragment();
}
I need the ViewPager to reload all its fragments at a certain event (for example press the button). I.e. all fragments (even the one that is now open and its neighbors) have been updated. How can this be realized?
In the adapter override getItemPosition like so:
public int getItemPosition(Object object) {
return POSITION_NONE;
}
Then call adapter.notifyDataSetChanged() and the ViewPager will remove all views and reload them.
You can call the ViewPager to set the adapter again like this,
yourViewPager.setAdapter(yourAdapter);
But the problem is it will jump to the first element location(ie. to your first Fragment in the ViewPager). To avoid that you can have a Listener that listens to the current item(current fragment) of the ViewPager and store the current position in a variable. Now you we can use this variable to set the current item of the ViewPager after refreshing. Something like this,
yourViewPager.setAdapter(yourAdapter);
yourViewPager.setCurrentItem(currentPosition);
please correct me if my understanding is wrong.
I have a main fragment that contains a viewpager. This viewpager gets the same secondary fragment (different from the main one) but with different parameters every time. Inside the said secondary fragment that is inside the viewpager, i have a recyclerview. Inside these recyclerviews are some fields that the user fills up. When the main fragment (the one that holds the viewpager) is closed (via a button click) i need to get the data from each recyclerview. How do i to that?
The best way to solve this problem is that implement interface which will give call back to activity holding these fragment and then from Activity pass it to fragment(main) and then use viewpager to get Fragment(secondry) and pass data.
Steps one would be implent interface which will give callback to activity and the get fragment from viewpager(Link for same)
This question already has answers here:
How can i pass image view between activities in android
(2 answers)
Closed 5 years ago.
I want to find out if there is a way to send an entire ImageView to another activity. Is it possible to achieve that with GSON?
You could try wrapping it in a class that implements Parcelable, but I think sending an entire view element to another activity is not really the way to go... Just send the image url and then load an image to an imageview in the other activity.
This question already has answers here:
How to pass an object from one activity to another on Android
(35 answers)
Closed 8 years ago.
I have an app MainActivity > ContactActivity > SummaryActivity
On MainActivity I have a text view with directions for the user and once they enter their information I want to get the results from SummaryActivity and post it on MainActivty page. Once the user inputs the information I want the instructions to disappear.
use bundle and pass as extra data with intent ...
see this thread
Passing data from one activity to another using bundle - not displaying in second activity