0

I have a function which inflates a layout and is supposed to manipulate the views contained within the layout. The layout's base element is a ConstraintLayout.

The fragment:

public class HomeFragment extends Fragment {
    ...
    public void myFunction() {
        View v = getLayoutInflater().inflate(R.layout.fragment_home, null);
        TextView t = (TextView) v.findViewById(R.id.my_text_view);
        t.setText("Some other text"); // nothing happens
        t.setTextColor(0xFF0000FF); // nothing happens
        t.setBackgroundColor(0xFFFF0000); // nothing happens
        Log.d("call", "myFunction() was called");
    }
}

The layout:

<androidx.constraintlayout.widget.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="match_parent">

    <TextView
        android:id="@+id/my_text_view"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:background="#F0F0F0"
        android:text="TextView"
        android:textColor="#000"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

<androidx.constraintlayout.widget.ConstraintLayout/>

The code compiles and runs successfully, but when I call myFunction() nothing happens to the TextView, while the log statement logs successfully. What am I doing wrong?

9
  • There's nothing weird in your code but where do you add the View v?
    – Shay Kin
    Mar 27, 2021 at 9:31
  • It is the base view (the ConstraintLayout) in fragment_home.
    – Wais Kamal
    Mar 27, 2021 at 16:04
  • can you please add the full code of your fragment ?
    – Shay Kin
    Mar 27, 2021 at 16:12
  • 1
    I added the relevant code (full code is over a thousand lines).
    – Wais Kamal
    Mar 27, 2021 at 16:37
  • 1
    What should rootView be in the example you've given? Note that this XML is contained in a fragment (fragment_home.xml).
    – Wais Kamal
    Mar 27, 2021 at 17:05

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Browse other questions tagged or ask your own question.