I have two fragments A and B. In my 'A' fragment, there is Textview variable and which is public static. I want to access fragment 'A' Textview variable and change its visibility in fragment 'B'. Can anyone help me to solve this problem? Or anyone could tell me how to pass Textview from Fragment 'A' to Fragment 'B'.
make textview object as public static like below ..
first fragment like make
public static TextView textView;
onCreate() method..
textView.setVisibility(View.VISIBLE);
then second fragment in oncreate method ..
FragmentName. textView.setVisibility(View.GONE);
You should access from fragment A to parent activity and tell the activity to call the one of the fragment B methods like setTextViewAText(String str)
for more information about communicating between fragment and activity see this link :
https://developer.android.com/training/basics/fragments/communicating.html
Related
I have an activity like this:
There are 2 buttons A and B on toolbar and a frame for fragment to take over. Say I have a string variable named button_type in fragment.
I want to have a system so that when I click button A in activity, the button_type in fragment sets to A and when I click button B in activity, the button_type in fragment sets to B.
How to do this?
Please note that I may click the buttons (A,B) when the fragment is already active (its not like after I click one button, the fragment comes.)
Thanks in advance.
Edit:
Currently I am trying this:
In MainActivity I get similar string button_type and set it as A or B according as button click and use the method:
public String getData(){return button_type;}
And in fragment I use: button_type= activity.getData(); in onViewCreated.
But it only seems to have the initial set value of A and B (which is A) and does not change when another button is clicked.
I think the best way if you use an interface for managing text in the fragment. the fragment will implement the interface. When the button click call the function in the fragment which is implemented by fragment.
Another way you can create an object of the fragment using findFragmentByTag() or findFragmentById() and then call the function in the fragment which handles the text.
Create an interface with methods onClickA(String buttonType) and onClickB(String buttonType).
Then create an object that implements this interface (or make fragment implement this interface by itself). I'll call this object listener.
call setButtonType(String buttonType) in listener methods implementation.
Then pass your listener to activity (you can get an instance of parent activity in fragment with getActivity() and cast it to your activity class) and in onClickListener of button A (in activity) call listener.onClickA(yourString) and do the same thing for button B.
I have a Activity which contains a fragment with recyclerview.
The recyclerview has few editable fields and need to put validations on clicking a button on activity.
Please let me know the solution or any techniques to overcome this problem statement
use this (click here ) functionality for data sharing between Fragment to fragment , Fragment to Activity and Activity to Activity
1) in your onCreate declare the BUSand register it
Bus bus = new Bus();
bus.register(this);
2) send data or any action using
bus.post(<your data or any Item>);
3) don't forgot to unregister in your onestroy
bus.unregister(this)
You can get that fragment instance either by 'ID' or by tag (you would need to give a tag while adding this fragment).
Now you can call any public method of that fragment using its instance.
For Example (Using tag), if you want to call a public method 'show()' of FragmentA. Then while adding it to the activity give it a tag like this:
Fragment fragmentA = new FragmentA();
getFragmentManager().beginTransaction()
.add(R.id.fragment_container,fragmentA,"YOUR_TARGET_FRAGMENT_TAG").commit();
And to get its instance (on a button click or as required):
FragmentA fragmentA = getFragmentManager().findFragmentByTag("YOUR_TARGET_FRAGMENT_TAG");
fragmentA.show();
Declare in your fragment the RecyclerView as public
Declare in your activity the fragment as variable
in your activity on click listener get the recyclerview with
fragment.recyclerview
I would like to change the TextView in an Fragment from my MainActivity. As the MainActivity contains a NavigationDrawer I have to change the TextView without such things like startActivityForResult.
I've also tried the LayoutInflater method, but it changes the whole layout, instead of just a small TextView.
Edit:
MainActivity:
new FRAGMENT_Main().setBalance(credits);
Fragment_Main:
public void setBalance(String credits)
{
AmountCredits.setText(credits);
}
Thank you very much!
If i get your question
If the MainActivity has the Instance of that fragment.
Just make a function in Fragment to update the textview.
and call it from MainActivity using the fragment's instance.
I have been looking for the answer everywhere but can't find the right one. I have tried onAttach methods etc, nothing works.
My goal is to change int value from the fragment inside activity which the fragment is called from or call a function which is inside the activity from the fragment.
As example 1:
I declare inside a Fragment: private int sample = 1;
Inside fragment I press the button and changes sample value to 2.
Example 2 with function:
I have a function inside a Fragment: public void sample(){}
Inside fragment I call the function sample()
How do I achieve this???
Maybe a duplicate of this one: Get current activity from fragment
You can get a Reference to the Activity by calling: getActivity() inside the fragment.
MyActivity myActivity = (MyActivity) getActivity();
Also if you want a reference to the Parent Fragment you just need to call: getParentFragment() inside the fragment:
MyParentFragment myParentFragment = (MyParentFragment) getParentFragment();
declare sample int variable globally and then assign value 2 to sample variable on button click . it will store value 2 and you can access any where in the same fragment .
I got a MainActivity and it has 3 fragments. One of those fragments got one TextView. How Can I manipulate that TextView from MainActivity?
On my MainActivity I have a TabLayout with that 3 fragments. I got data from database in MainActivity and I would like to change the textview that is on fragment 1 with that data I got.
Thank you.
I am suggest you to create some method in one of your Fragment
example :
In Fragment One create/ add this method :
public void Test(String jajal_disek) {
Toast.makeText(getActivity().getApplicationContext(), jajal_disek,Toast.LENGTH_SHORT).show();
ABC.setText(jajal_disek); //example your textview name ABC
}
and then in MainActivity call that's method inside FragmentOne like this :
FragmentOne fragment = (FragmentOne) getSupportFragmentManager().findFragmentById(R.id.container_body);
fragment.Test("StackOverFlow");
Hope this will help you, be great if you write source code to identified problems