Search code examples
javaandroidandroid-layoutandroid-fragmentsfragmentmanager

TextView setText not working


I am trying to setText to the textviews in the fragment which is contained in the activity. When I set text using the setText method I can see that the mText class variable of the textview is assigned that value. However, on the UI end I cannot see the text set by the code.

I have an activity layout like this.

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:tools="http://schemas.android.com/tools"
            android:id="@+id/meaningcontainer"
            tools:context="com.xyz.MeaningActivity"
            tools:ignore="MergeRootFrame"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
>


<TextView
    android:layout_width="match_parent"
    android:layout_height="50dp"
    android:layout_below="@+id/meaning_toolbar"
    android:id="@+id/word_selected"
    android:textSize="@dimen/abc_text_size_title_material_toolbar"
    />

<FrameLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@id/word_selected"
    android:id="@+id/word_meanings"
    >

    <fragment
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/frag_meaning"
        android:name="com.xyz.ui.MeaningActivity$MeaningFragment"
        tools:layout="@layout/fragment_meaning"
        />

</FrameLayout>

The corresponding fragment that is included in this layout is following:

<ScrollView xmlns:tools="http://schemas.android.com/tools"
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        tools:context=".ui.MeaningActivity$MeaningFragment"
>

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    >

    <android.support.v7.widget.CardView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:elevation="@dimen/cardview_default_elevation"
        >

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            >

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="16dp"
                android:id="@+id/postype_noun"
                android:textAlignment="textStart"
                android:textSize="@dimen/abc_text_size_title_material_toolbar"
                android:layout_marginStart="@dimen/activity_horizontal_margin"
                />

        </LinearLayout>

    </android.support.v7.widget.CardView>


</RelativeLayout>

In the activity onCreate method I try to setText. Here is the code for that:

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

    if (savedInstanceState == null) {
        Fragment fragment = new MeaningFragment();
        getSupportFragmentManager().beginTransaction()
                .add(R.id.word_meanings, fragment).commit();
    }
    Fragment fragment = getSupportFragmentManager().
                            findFragmentById(R.id.frag_meaning);
    View frag_view = fragment.getView();
    TextView postextView = (TextView) frag_view.       
                                    findViewById(R.id.postype_noun);
    postextView.setText("test");

}

The textView postextView is not null and get assigned the value of test. But on the UI end I can not see any test string.


Solution

  • Try removing card layout from your activity's layout.