Axil
Axil

Reputation: 3311

How to modify a textview of the fragment from parent activity in android

In my activity, i have created added some fragments

public class MainActivity extends AppCompatActivity implements IabBroadcastListener{
    static ViewPagerAdapter adapter;

    ....
    adapter = new ViewPagerAdapter(getSupportFragmentManager());
    adapter.addFragment(new MainFragment(), "\uE602");
    adapter.addFragment(new QuestionsListFragment(), "\uE620");
    adapter.addFragment(new MyQuestionsFragment(), "\uE61E");
    adapter.addFragment(new NotificationsFragment(), "\uE601");
    adapter.addFragment(new ProfileSettingFragment(), "\uE605");
    viewPager.setAdapter(adapter);

how can i modify like a textview of the fragment from my activity ? Im trying to get the view of the MainFragment from the parent activity

   TextView textview_buy_SLPro = (TextView) howtogetMainFragment.findViewById(R.id.txt_Buy_Pro);

Upvotes: 1

Views: 1880

Answers (5)

C0D3LIC1OU5
C0D3LIC1OU5

Reputation: 8680

There's a number of ways to do this. My favorite is to use a message bus (such as Otto, tinybys, etc) and use events to communicate between app components. It's a design pattern and will take some learning to implement but well worth the effort imho.

Upvotes: 0

kris larson
kris larson

Reputation: 30985

You are talking about a fragment that is part of a ViewPager, so you have two concerns:

  • Your fragment might not have an active view (if you've swiped a few pages away from it, for instance). So the fragment's TextView might not even exist.

  • The fragment itself might not even exist (if you haven't swiped enough pages to create the fragment, or you are subclassing FragmentStatePagerAdapter, for example).

I have found that for this situation, the best design is an event listener pattern, where the fragment sets a listener on the activity when it creates a view. Also, the text value should be stored in the activity so that when someone swipes to the page with that fragment, the fragment can set up the correct value on the TextView.

The pattern goes like this:

  • Create an interface for the event; ex. TextUpdateListener

  • Add a list of listeners to your activity

  • When the fragment creates its view, it adds a listener to the activity, and when it destroys the view, it unregisters the listener. The listener will update its view when it receives the event from the activity.

  • When the value changes, the activity goes through the list of listeners and calls their event method.

I give a pretty detailed code example in this SO answer.

Upvotes: 0

adalpari
adalpari

Reputation: 3121

You can create a method inside your fragment, to change view or even return it.

public class MyFragment extends Fragment {

    TextView mTextView;

    ....

    public void setTextView(String text){
        mTextView.setText(text);
    }

    ....

}

And the call it from your activity:

public class MainActivity extends AppCompatActivity implements IabBroadcastListener{
    static ViewPagerAdapter adapter;

    ....

    MainFragment mMainFragment = new MainFragment();

    adapter = new ViewPagerAdapter(getSupportFragmentManager());
    adapter.addFragment(mMainFragment, "\uE602");
    adapter.addFragment(new QuestionsListFragment(), "\uE620");
    adapter.addFragment(new MyQuestionsFragment(), "\uE61E");
    adapter.addFragment(new NotificationsFragment(), "\uE601");
    adapter.addFragment(new ProfileSettingFragment(), "\uE605");
    viewPager.setAdapter(adapter);

    ....

    //call method inside fragment
    mMainFragment.setTextView("Hello World!");

    ...
}

Upvotes: 1

thealeksandr
thealeksandr

Reputation: 1736

I think better solution is create method in fragment

 public void setBuySLPro(String data) {
       textview_buy_SLPro.setText(data);
 }

And call this method from activity. It's not the best idea to get access to the fragment view from the activity.

And fields should be name according to CamelCase rule.

Upvotes: 0

BOUTERBIAT Oualid
BOUTERBIAT Oualid

Reputation: 1544

Try to use onActivityCreated()

@Override
public void onActivityCreated (Bundle savedInstanceState) {
  super.onActivityCreated(savedInstanceState);

  TextView textview_buy_SLPro = (TextView) getView().findViewById(R.id.txt_Buy_Pro);
  textview_buy_SLPro.setText(getActivity.getSomeText());
}

Upvotes: 0

Related Questions