androidmanifest2
androidmanifest2

Reputation: 53

Update textView in Fragment from Activity

I have 3 TextView in Fragment..

In MainActivity I have method which generate 3 string every 10 seconds.

And now I want to update TextViews from activity, but I don't know how.

Can someone give me advice ?

Upvotes: 0

Views: 2709

Answers (2)

Red M
Red M

Reputation: 2789

It could be achieved using an interface in your activity, and passing that parameter to your fragment whenever the string is generated:

public class MainActivity extends AppCompatActivity {
private OnGenerateStringListener onGenerateStringListener;


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

    onGenerateStringListener = (OnGenerateStringListener) this;

    onStartGenerateStringFragment();

    try {
        generateString();
    } catch (InterruptedException e) {
        e.printStackTrace();
    }

}

private void generateString() throws InterruptedException {

    // After string a is generated
    String a = "test one";
    onGenerateStringListener.onGeneratedString(a);

    // Sleep 10 secs
    Thread.sleep(10000);

    String b = "test Two";
    onGenerateStringListener.onGeneratedString(b);

    // Sleep 10 secs
    Thread.sleep(10000);

    String c = "test Three";
    onGenerateStringListener.onGeneratedString(c);

}

private void onStartGenerateStringFragment() {
 //Method to launch your fragment
}    

interface OnGenerateStringListener {
    void onGeneratedString(String string);
}

}

And this is your Fragment class:

public class FragmentTextView extends Fragment implements MainActivity.OnGenerateStringListener{

    private View view;
    private TextView textViewOne;
    private TextView textViewTwo;
    private TextView textViewThree;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        if (view != null) {
            ViewGroup group = (ViewGroup) view.getParent();
            if (group != null) {
                group.removeView(view);
            }
        } else {
            view = inflater.inflate(R.layout.fragment_layout, container, false);
            textViewOne = view.findViewById(R.id.textViewOne);
            textViewTwo = view.findViewById(R.id.textViewTwo);
            textViewThree = view.findViewById(R.id.textViewThree);
        }
        return view;
    }


    @Override
    public void onGeneratedString(String string) {
        switch (string) {
            // Use a switch case to determine which text view gets what parameters
            // for the sake of the example, I just passed a dummy text view input
            case "test one":
                textViewOne.setText(string);
                break;
            case "test Two":
                textViewTwo.setText(string);
                break;
            case "test Three":
                textViewThree.setText(string);
                break;
        }
    }
}

Upvotes: 2

zbys
zbys

Reputation: 463

Set id's for the textviews in the xml file: android:id="@+id/myId Then do this:

TextView textView = (TextView) findViewById(R.id.myId);
textView.setText("updated text");

Upvotes: 1

Related Questions