NoName
NoName

Reputation: 293

Android How to change fragment TextView using java in MainActivivity

I insert one fragment in activity_main.xml using java, i put fragment in linear layout in activity_main. Fragment xml file has one textView.

How can i change TextView when app start from MainActivity using java, when app start and when it called onCreate? I try few solution form stacoverflow but i can get this to work. Emulator get crashed when he start to load app.

Here is activity_main.xml code:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical"
    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="com.example.android.fragment_promjenaviewprekojave.MainActivity">


    <LinearLayout
        android:id="@+id/linearLayoutFragment_id"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
    </LinearLayout>
</LinearLayout>

Here is Fragment code:

package com.example.android.fragment_promjenaviewprekojave;

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

public class BlankFragment extends Fragment {
    public BlankFragment() {
        // Required empty public constructor
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_blank, container, false);
    }
}

Here is xml file for that fragment who contain only 1 TextView

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.android.fragment_promjenaviewprekojave.BlankFragment">

<TextView
    android:id="@+id/fragmentTextView_id"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

</LinearLayout>

Here is the MainActivity.java from where I want to change fragment TextView

package com.example.android.fragment_promjenaviewprekojave;

import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

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

        BlankFragment objA = new BlankFragment();
        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        fragmentTransaction.add(R.id.linearLayoutFragment_id, objA);

        fragmentTransaction.commit();

        TextView textView = (TextView) findViewById(R.id.fragmentTextView_id);
        textView.setText("We add text to fragment TextView");
    }
}

Upvotes: 0

Views: 85

Answers (2)

NSimon
NSimon

Reputation: 5287

In your BlankFragment code, change

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    return inflater.inflate(R.layout.fragment_blank, container, false);
}

to :

//Declare a member to hold a reference to your TextView
private TextView  myTextView;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View v = inflater.inflate(R.layout.fragment_blank, container, false);
    myTextView = (TextView) v.findViewById(R.id.fragmentTextView_id);
    myTextView .setText("Whatever text you want");
    return (v);
}

//Also declare a function to modify the text on your fragment
public void setCustomText(String str) {
    if (myTextView != null) {
      myTextView.setText(str);
    }
}

Now back in your activity, you can call

objA.setCustomText("Hello World!");

Bare in mind that you will need to store a reference to objA in your activity, such as

private BlankFragment  objA;

The onCreate will then be :

objA = new BlankFragment();

Lastly, this method works fine once the views are all in place and drawn. If you're looking to change the text inside the fragment at creation, you will want to use a newInstance pattern on the fragment, where you can pass the text as an argument.

Something like this :

objA = BlankFragment.newInstance("Hello world");

Where newInstance is defined in your fragment as follow :

public static BlankFragment newInstance(String str) {
    BlankFragment f = new BlankFragment();
    Bundle args = new Bundle();
    args.putString("TextTag", str);
    f.setArguments(args);
    return (f);
}

And then you can retrieve this value in the onCreate of your Fragment like this :

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View v = inflater.inflate(R.layout.fragment_blank, container, false);
    myTextView = (TextView) v.findViewById(R.id.fragmentTextView_id);
    String text = getArguments().getString("TextTag", "");
    myTextView .setText(text);
    return (v);
}

Upvotes: 1

eoinzy
eoinzy

Reputation: 2242

In your xml, you should have a fragment container. Assuming the ID of this is fragment_id:

For support library,

BlankFragment blankFrag = (BlankFragment)getSupportFragmentManager().
                                              findFragmentById(R.id.fragment_id);

Or else for non-support library Fragment:

BlankFragment blankFrag = (BlankFragment)getFragmentManager().
                                             findFragmentById(R.id.fragment_id); 

You will also need a public method in the BlankFragment class with a signature like setTextViewText(String text) and then call it from MainActivity like blankFrag.setTextViewText("my text");

Upvotes: 1

Related Questions