Search code examples
javaandroidxmlandroid-fragmentsandroid-nested-fragment

Access textfield of nested fragment


I have several fragments. Some fragments have the same buttons at the top and the bottom of the fragment, but the content is different.

Therefore I'm using nested fragments for the top and the buttom part. This is my java class: LogFragment.java. I insert two fragments for the top navigation and the footer at the bottom.

public class LogFragment extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) 
    {
        View rootview = inflater.inflate(R.layout.fragment_log, container, false);

        Fragment navigationFrag = new NavigationFrag();
        FragmentTransaction trans1 = getChildFragmentManager().beginTransaction();
        trans1.add(R.id.navigationFrag, navigationFrag).commit();

        Fragment footerFragment = new FooterFragment();
        FragmentTransaction trans2 = getChildFragmentManager().beginTransaction();
        trans2.add(R.id.footerFragment, footerFragment).commit();

        return rootview;
    }
}

This is the layout file: fragment_log.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <FrameLayout android:id="@+id/NavigationFragment"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:orientation="vertical">

        <Button
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Start"/>

        <Button
            android:id="@+id/button2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Stop"
            android:layout_below="@+id/button1"/>

    </RelativeLayout>

    <FrameLayout android:id="@+id/FooterFragment"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

</LinearLayout>

The nested NavigationFragment and FooterFragment contains some buttons, images or textfields. For example: fragment_footer.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

        <TextView
            android:id="@+id/logInfo"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="LOG" />

</RelativeLayout>

My question:

How can I access/manipulate/fill in some data of the elements in my nested fragments by the LogFragment.java class?


For example:

I want to access the textview with the id logInfo of the nested fragment_footer.xmlby the LogFragment.java.


Solution

  • Just use [child fragment].getView().findViewById(R.id.[elemnet id]) to get the elements in nested fragments. For example :

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) 
    {
        View rootview = inflater.inflate(R.layout.fragment_log, container, false);
    
        Fragment navigationFrag = new NavigationFrag();
        FragmentTransaction trans1 = getChildFragmentManager().beginTransaction();
        trans1.add(R.id.navigationFrag, navigationFrag).commit();
    
        final Fragment footerFragment = new FooterFragment();
        FragmentTransaction trans2 = getChildFragmentManager().beginTransaction();
        trans2.add(R.id.footerFragment, footerFragment).commit();
    
        Button btn1 = (Button)rootview.findViewById(R.id.button1);
        btn1.setOnClickListener( new OnClickListener(){
            @Override
            public void onClick(View v) {
                TextView txv = (TextView)footerFragment.getView().findViewById(R.id.logInfo);
                txv.setText("Updated log of Footer");
            }
        });
    
        return rootview;
    }
    

    By the way,

    1. the child fragment is available to access only after onCreateView(),
    2. so you need to declare the child fragment as final or member data of parent.