Android onChangeListener - android

Could somebody help me to create my own Listener.
I have an Activity where a String is changing and in an Activity's fragment I have to set a TextView's text for this String.

Why you are using Activity and Activity fragment?
just use fragment and make an interface
public interface ddd
{
void MyTestInterface(String str);
}
make your activityFragment implement it and than just pass your activity (this) to the other fragment how handle the string changes.
like: MyTestInterface.stringCallback("str")

Related

How to send date from parent Fragment to Child Fragment?

I'm an Android novice.
As with the title, I want to convey some data from Parents Fragment to child Fragment.
But I'm stuck in it and very hard.
Maybe try saving your child Fragment as a variable in your parent Pragment. Then you are able to call public methods like in your case childFragment.setDate(date).
To transfer information from the parent fragment to the child create an interface
interface YourListner{
void methodToPassData(Date date);
}
After which the child fragment should implement this interface
Then you can cast the fragment to the type of the interface and pass the date to it by calling the method.
ChildFragment fragment = new ChuldFragment();
//display fragment
YourListner fragmentListener = (YourListner)fragmentListener;
fragmentListener.methodToPassData(date);
You can also do this using the constructor in the child fragment.

How to pass data from activity to Fragment which is inside viewpager

I have one tab activity that contain 3 tabs.
I have one edittext and button in my activity, one textview in fragments.
Whenever I need to change the fragment textview I simply add some text in edittext and click the button after. That text should appear in the fragment.
Here I am not able to use setArguments.
If you are using ViewPager to render the fragment use this code in your parent Activity.
if(viewPager.getCurrentItem() == 1) //First fragment
{
FragmentOne frag1 = (FragmentOne)viewPager.getAdapter().instantiateItem(viewPager, viewPager.getCurrentItem());
frag1.textview.setText(yourText);
}
Otto's EventBus to the rescue.
I had exactly this situation to handle. In my case I needed to trigger a fragment change inside a viewpager with locked swipes. So
Gradle:
// EventBus
implementation 'org.greenrobot:eventbus:3.0.0'
Make an event which will be passed from the activity to the fragment
public class FragmentChangeEvent {
public int fragmentToBeChanged;
//here you can define variables of your choosing, just make sure you're including them into the constructor of the event.
public FragmentChangeEvent(int fragmentToBeChanged) {
this.fragmentToBeChanged = fragmentToBeChanged;
}
}
Trigger the event from the activity
EventBus.getDefault().post(new FragmentChangeEvent(1));
And finally make your fragment aware of the bus and the events
#Subscribe(threadMode = ThreadMode.MAIN)
public void onChangeFragmentEvent(FragmentChangeEvent event) {
//do your work here.
}
}

Trying to call a fragment method from parent activity but the method is not being resolved

I tried the solution here:
Calling a Fragment method from a parent Activity
But it didn't work for me.
I have this method in my fragment
public void showbutton()
{
sup.setEnabled(true);
}
and I'm using this in the parent activity
Fragment fragment = (Fragment) getFragmentManager().findFragmentById(R.id.fragment);
fragment.showbutton();
I'm sure it's a silly mistake, I'm still new to Android so forgive me.
Use callback to communicate from fragment to Activity.
public interface UserAction {
void showButton();
}
In the Fragment implement the Interface
#Override
public void showButton() {
sup.setEnabled(true);
}
Then in your activity just call
UserAction mUserAction = getFragmentManager().findFragmentById(R.id.fragment);
mUserAction.showButton();
Firstly, make sure you are accessing the correct FragmentManager, verify that you need to call either getFragmentManager() or getSupportFragmentManager().
Secondly, you should cast the Fragment to your type. That is,
MyFragment fragment = (MyFragment) getFragmentManager().findFragmentById(R.id.fragment);
fragment.showbutton();
I think you should typecast using Fragment Name.
i.e
YourFragment fragment = (YourFragment) getFragmentManager().findFragmentById(R.id.fragment);
fragment.showbutton();
This issue is regarding to fragment life cycle. You are trying to invoke method of fragment which is closed by android OS. You need to create bool in your fragment and set that to false normally but when you need to show button just create that fragment and load that and update your bool to true.Later on check if that bool is true enable your button else disable it. Happy Coding!

How to get the object of fragmentA from fragmentB in a Activity

What i have
I have an activity
MainActivity.java here in this activity I have a
fragment(MainFragment.java)
In MainFragment.java I have two child fragments ChildOne.java and
ChildTwo.java
I have a editText in ChildOne.java
What I am trying to do:
I need to get the value in the editText of ChildOne.java from
ChildTwo.java
how to achieve this !
You can achieve this in multiple ways, but I'll give you one supposing that both Fragments are displayed at the same time.
In your ChildTwo use getParentFragment to get MainFragment.
MainFragment parent = (MainFragment) getParentFragment();
String edit_text_value = parent.getChildOneText();
In your MainFragment you need a reference object to your ChildOne fragment.
public String getChildOneText()
{
return mFirstFragment.getEditTextValue();
}
Finally in your ChildOne fragment create a method to return your EditText.
public String getEditTextValue()
{
return my_edit.getText().toString();
}
Hope it helps!
You may create one function in mainActivity and a variable that holds the value in there. And call that function when you want to save the data of editText, And can create a function to get the data in MainActivity as well. Call those functions like this -
((Home) getActivity()).shareData("example string");
((Home) getActivity()).receiveData("example string");
May check this answer and this one

update TextView in fragment A when clicking button in fragment B

I have two fragments sitting side by side in the same activity. When I touch a button in the right fragment (fragment B), I need a TextView in the left fragment to update (fragment A). I have looked all over for the best way to do this, but nothing seems to work for my needs. Could someone possibly give me an example of how I would code this? Fragment A is set through the XML layout, and fragment B gets loaded programmatically into a container. I have tried accomplishing this by using a method in fragment A to update the text, and calling on that method from a method in the parent activity. I then call on the method in the parent activity from fragment B.
This is the code in fragment B that declares the interface and calls a method in the interface
AttackCards attackCards;
public interface AttackCards {
public void deckSize();
}
public void onAttach(DeckBuilder deckBuilder) {
super.onAttach(deckBuilder);
attackCards = (AttackCards) deckBuilder;
}
attackCards.deckSize(); //this is in my onclick methods
This is the code in the activity that implements the interface and calls the method in fragment A
public class DeckBuilder extends Activity implements AttackCards{
public void deckSize() {
DeckBuilderFragment deckBuilderFragment = (DeckBuilderFragment)getFragmentManager().
findFragmentById(R.id.deckbuilder_fragment);
deckBuilderFragment.deckSize();
}
This is the method in fragment A that appends the textview with the contents of a shared preferences value
public void deckSize() {
deckSize = (TextView) getView().findViewById(R.id.decksize);
final SharedPreferences defaultDeck = getActivity()
.getSharedPreferences("defaultDeck", Context.MODE_PRIVATE);
deckSize.setText(String.valueOf(defaultDeck.getInt("decksize", 0)));
}
Sadly this attempt simply brings me a nullpointer when touching a button. I am getting a null pointer at
attackCards.deckSize(); //this is in my onclick methods
Could someone please help me out with an example of how to do this correctly?
One fragment should not communicate to another fragment directly. It should do so through attached activity. The detail explanation with code example is available here
Android Developer site
Declare an interface in Fragment B, and implement the interface in the activity. Call the interface through callback in Fragment B when button is clicked. You can have a public function in Fragment A to update the TextView, so activity directly call the function to update the text.
You can define an interface in Fragment B and implement it on the MainActivity. Then on the callback method (onClickOnB in this case) set the text on the TextView. You should obtain a reference of the TextView in the Activity's onCreate() after setContentView(). This works because Fragment A is static. Otherwise, you can create a public method inside Fragment A so you can set the text from inside the callback by getting a reference of Fragment A and calling such method.
Fragment B
public class FragmentB extends Fragment implements onClickListener{
ClickOnB listener;
public void setOnFragmentBClickListener(ClickOnB listener){
this.listener = listener;
}
#Override
public void onClick(View v){
//stringMessage is a `String` you will pass to `Fragment` A to update its `TextView`
listener.onClickOnB(stringMessage);
}
interface ClickOnB{
public void onClickOnB(String message);
}
}
MainActivity
public class MainActivity extends Activity implements ClickOnB{
#Override
protected onCreate(Bundle savedInstanceState){
//Get a reference of `Fragment` B somewhere in your code after you added it dynamically and set the listener.
((FragmentB)getFragmentManager().findFragmentByTag("FragmentB")).setOnFragmentBClickListener(this);
}
#Override
public void onClickOnB(String message){
//Set the text to the `TextView` here (I am assuming you get a reference of the `TextView` in onCreate() after inflating your layout.
mTextView.setText(message);
}
}

Categories

Resources