How to access Id's of Fragment element (like: TextView, EditText etc ) in the MainActivity

351 Views Asked by At

Here is the Fragment.xml file. I created some elements (like TextView and ImageView). I assigned IDs to those elements.

<ImageView
    android:id="@+id/ivContact"
    android:layout_width="70dp"
    android:layout_height="70dp"
    android:layout_gravity="center_horizontal"
    android:src="@drawable/contact" />

<TextView
    android:id="@+id/tvName"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center_horizontal"
    android:text="TextView"
    android:textSize="18sp"
    android:textStyle="bold" />

<TextView
    android:id="@+id/tvTel"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center_horizontal"
    android:text="TextView" />
----------------------------------------------------------------------- Here is the Fragment.java file.
TextView tvName,tvTel;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    return inflater.inflate(R.layout.fragment_detail, container, false);
}

Here is the MainActivity.java file. I want to access the IDs of Fragment.xml file. In the MainActivity.java file. But I'm unable to access the IDs. It showing errer. I'm stuck. I had try so many time. But not solved. I think this is old version to access the IDs. But I don't know how can I access the IDs in new version.

ERROR:@layout/activity_main does not contain a declaration with id tvName.

 TextView tvName,tvTel;
 EditText etName,etTel;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    tvName = findViewById(R.id.tvName);
    tvTel = findViewById(R.id.tvTel);

    etName = findViewById(R.id.etName);
    etTel = findViewById(R.id.etTel);
}

Please help me, How can I access the IDs.

1

There are 1 best solutions below

4
logancodemaker On

Normally you should never have to do that. If you want to communicate with the host activity you can use the onAttach method from the fragment or simply requireActivity(). Look at the example below

public class MyFragment extends Fragment {

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragment_layout, container, false);

        return view;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        
        TextView tvName = view.findViewById(R.id.tvName);
        TextView tvTel = view.findViewById(R.id.tvTel);

        TextView etName = view.findViewById(R.id.etName);
        TextView etTel = view.findViewById(R.id.etTel);

        MainActivity activity = (MainActivity) requireActivity();
        

        tvName.setText(activity.sameFields);
        activity.someMethods();
    }

    @Override
    public void onAttach(@NonNull Context context) {
        super.onAttach(context);
        if ((context instanceof MainActivity)) {
            MainActivity activity = (MainActivity) context;
            activity.someMethods();
        }
    }
}