Sander Jochems
Sander Jochems

Reputation: 142

Update a TextView in a ViewPager from anohter Fragment

I have created a Tabbed Activity with 3 fragments. How can I update the text of a TextView in a Fragment from the MainActivity Java File?

TabStatus.java:

package com.sanderjochems.bluetoothdata;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class TabStatus extends Fragment {

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

fragment_status.xml

<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:id="@+id/constraintLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.sanderjochems.bluetoothdata.MainActivity"
    tools:layout_editor_absoluteY="81dp">

    <android.support.v7.widget.CardView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginEnd="16dp"
        android:layout_marginStart="16dp"
        android:layout_marginTop="16dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical"
            android:paddingBottom="16dp"
            android:paddingLeft="16dp"
            android:paddingRight="16dp"
            android:paddingTop="24dp">

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="@string/title_bluetooth"
                android:textAllCaps="false"
                android:textColor="@color/colorPrimaryText"
                android:textSize="24sp"
                android:textStyle="bold" />

            <Space
                android:layout_width="match_parent"
                android:layout_height="8dp" />

            <TextView
                android:id="@+id/tbMacAddress"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="@string/text_mac_address" />

            <TextView
                android:id="@+id/tbState"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="@string/text_state" />

        </LinearLayout>

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

</android.support.constraint.ConstraintLayout>

I want to update the TextView tbState. How can I do that?

I tried to use a LayoutInflater, but that didnt work.

I used this piece of code for that:

LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View layoutStatus = inflater.inflate(R.layout.fragment_status, null);

tvCurrentState = (TextView) layoutStatus.findViewById(R.id.tbState);

Regards, Sander Jochems

Upvotes: 0

Views: 24

Answers (2)

Sachin Rajput
Sachin Rajput

Reputation: 4344

use event bus and fire the event when you perform an action in your fragment and catch that event in another fragment and update the desired textview.

follow this very simple library implementation and use

https://github.com/greenrobot/EventBus

Upvotes: 0

Related Questions