androidandroid-fragmentsfindviewbyidandroid-identifiers

Can I limit findViewById() to within the scope of an individual fragment?


In my application, I have multiple instances of the same fragment, Activity_Fragment, displayed in a LinearLayout. In each fragment, there is an activity_text textview and an edit_button. When the button is pressed in a fragment, it should change the text of the textview in the same fragment. However, when multiple instances of the fragment are added, any edit_button pressed in any fragment will only change the activity_text textview in the first fragment added to the LinearLayout. This is because all fragments are identical, and their child views share the same id's. Each fragment should act independently of one other; any edit_button pressed in a fragment should only affect that fragment.

Is it possible to have the findViewById() method limited to the scope of an individual fragment so that one fragment won't be affected by an event in another fragment, or will I have to somehow change the id of each view in a fragment upon creation?

Here is the Activity Fragment xml:

 <?xml version="1.0" encoding="utf-8"?>
    <android.support.constraint.ConstraintLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/activity_fragment">

        <CheckBox
            android:id="@+id/checkbox"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="32dp"
            android:layout_marginTop="16dp"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <TextView
            android:id="@+id/activity_text"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginEnd="5dp"
            android:layout_marginStart="16dp"
            android:layout_marginTop="16dp"
            android:text="Edit Activity"
            android:textSize="18sp"
            app:layout_constraintBaseline_toBaselineOf="@+id/checkbox"
            app:layout_constraintLeft_toRightOf="@+id/checkbox"
            app:layout_constraintRight_toLeftOf="@+id/edit_button"/>

        <Button
            android:id="@+id/edit_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginBottom="16dp"
            android:layout_marginEnd="16dp"
            android:layout_marginTop="16dp"
            android:onClick="editActivity"
            android:text="Edit Activity"
            app:layout_constraintBaseline_toBaselineOf="@+id/checkbox"
            app:layout_constraintRight_toRightOf="parent"/>

Here is my MainActivity.java with the ActivityFragment class. The onClick method for the edit_button is at the very bottom:

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    public static class ActivityFragment extends Fragment {
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup 
        container, Bundle savedInstanceState) {
            // Inflate the layout for this fragment
            return inflater.inflate(R.layout.activity_fragment, container, 
            false);
        }
    }

    public void addActivity(View view) {
        ActivityFragment fragment1 = new ActivityFragment();
        getSupportFragmentManager().beginTransaction()
                .add(R.id.fragment_container, fragment1).commit();
    }

    //This is the editButton method
    public void editActivity(View view) {
        TextView activityText = (TextView) findViewById(R.id.activity_text);
        activityText.setText("success");
    }
}

Solution

  • Your problem can be solved by limiting the scope that findViewById() scans. If you want to look within a particular fragment, you'll probably want to call myFragment.getView().findViewById() (rather than just using findViewById() from your activity).

    So how can you do this? Well, it's not easy with the way you have things set up. Since you're using the android:onClick attribute to set your click listener, you have to handle things from inside the activity.

    So don't use android:onClick.

    Instead, inside your Fragment's onCreateView() method, write something like this:

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        View root = inflater.inflate(R.layout.your_fragment, container, false);
    
        Button button = (Button) root.findViewById(R.id.edit_button);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                TextView activityText = (TextView) getView().findViewById(R.id.activity_text);
                activityText.setText("success");
            }
        });
    
        return root;
    }