Inflated view methods not working after inflating the view

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?

android
layout-inflater
asked on Stack Overflow Mar 27, 2021 by Wais Kamal • edited Mar 27, 2021 by Wais Kamal

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0