Lisek
Lisek

Reputation: 783

Referring to Views from different Fragments in same Activity

Let say the target application is built from 3 fragments which are all in the same activity public class MainActivity extends android.support.v4.app.FragmentActivity implements ActionBar.TabListener. Starting fragment is public class ButtonSectionFragment extends Fragment where there is a Button:

public class ButtonSectionFragment extends Fragment {
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
    final View rootView = inflater.inflate(R.layout.btn, container, false);
    Button mybutton = (Button) rootView.findViewById(R.id.mybutton);

    mybutton.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View v) {
        ????????????????????

    }
});
}

There are ?? in the onClick method, I will get to that. And there is another fragment like this:

public static class TextSectionFragment extends Fragment {
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.tv, container, false);

    TextView textv = (TextView) rootView.findViewById(R.id.texty);
}

Both of the fragments are using different layouts so this is why rootView is being used right in front of findViewById.

The outcome I would like to achieve is: by Button from 1st fragment click set the TextView from 2nd fragment to Hello. What should I put in the ??? part to make it all work?

Upvotes: 0

Views: 521

Answers (4)

droidx
droidx

Reputation: 2172

In continuation to Raghunandan's answer, you could check similar implementation in the link.

update TextView in fragment A when clicking button in fragment B

Do not forget to get a reference of the textview of the TextSectionFragment into the MainActivity.

Upvotes: 0

Vancert Staff
Vancert Staff

Reputation: 205

    Fragment frag1 = new ButtonSectionFragment ();
    Fragment frag2 = new TextSectionFragment();
    FragmentManager         fm      = getSupportFragmentManager();
    FragmentTransaction     ft      = fm.beginTransaction(); 

    ft.add(layout, frag1);
    ft.add(layout, frag2);
    ft.commit();

    View frag1RootView = frag1.getView();
    View frag2RootView = frag2.getView();

    Button btn = (Button)frag1RootView.findViewById(id);
    TextView tv = (TextView)frag2RootView.findViewById(id);

untested but... I think that would do it...

EDIT: You should get the root views onActivityCreated(); or it'll throw you a null...

Upvotes: 0

WindRider
WindRider

Reputation: 12318

You just have to use getActivity().findViewById() instead of getView().findViewById()

final TextView tv = (TextView) getActivity().findViewById(R.id.texty);
if (tv != null)
   tv.setText("Hello");

Fragments are just branches inside the common layout tree of activity. All fragment views of a common activity can be accessed through Activity.findViewByXXX(). The only complication is that fragments can be dynamically added, removed, replaced, etc. So you to be sure that the needed fragment is already inflated into the layout hierarchy. You can make initialization of the UI in onViewCreated() of the other fragment. That guarantees you the layout has been loaded already.

Upvotes: 1

Raghunandan
Raghunandan

Reputation: 133560

You need to use interface as callback to the activity then set the textview in fragment.

Two framgents should never communicate directly.

http://developer.android.com/training/basics/fragments/communicating.html.

Framgent1 -->Activity -->Fragment2.

You can comunicate value from fragment2 to activity first then from activity to fragment2. Then set text in fragment2

Upvotes: 2

Related Questions