How to Change Fragment's Textview's Text from Activity

How to change fragment's textView's text from activity

@Lalit answer is correct but I see that you dont need to create a function like fragment_obj.updateTextView();. I set all my view as class level objects and was able to update the textview directly.

fragmentRegister.textViewLanguage.setText("hello mister how do you do");

Note: If you need to perform more than one action then having a function is the way to go.

Change Fragment's TextViews's text from Activity

In onClick method, you can get fragment as following:

public void onClick(View v) {
yourFrag fragment = (yourFrag)getSupportFragmentManager().findFragmentById(R.id.fragment_id);
}

and in fragment, create a method to update the textView, so that we can call it using fragment object as following:

public void update(String s){
textView.setText(s);
}

Note: This textView should be defined in your fragment.

and call this from main activity as following:

public void onClick(View v) {
yourFrag fragment = (yourFrag)getSupportFragmentManager().findFragmentById(R.id.fragment_id);
fragment.update("foo");
}

And yes! if you are not using support library, just replace getSupportFragmentManager() by getFragmentManager().

Set text fragment from main activity

When adding your fragment you can assign a tag to it:

fragmentTransaction.add(R.id.main_container, new HomeFragment(), "MY_FRAGMENT");
fragmentTransaction.commit();

When you need to change the TextView you can recover the fragment with the used tag and call a method of the fragment that changes the text:

HomeFragment fragment = (HomeFragment) getSupportFragmentManager()
.findFragmentByTag("MY_FRAGMENT");
fragment.changeText("New text");

If you are not using the support library, replace getSupportFragmentManager() with getFragmentManager().

That method inside the HomeFragment class would be:

public void changeText(String newText) {
TextView textview = (TextView) getView().findViewById(R.id.name);
textview.setText(newText);
}

Update textView in Fragment from Activity

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;
}
}
}

Change fragments textview from activity

You can either use findFragmentById to find your fragment and call a public method that changes the text

(ConversationFragment) getFragmentManager().findFragmentById(R.id.yourid);

or keep an instance of the class when you create it before you add it to the fragment manager

conv = new ConversationFragment()
fragmentTransaction.add(R.id.container, conv);

then just call the public method using conv or whatever you call it

EDIT:

you use a bundle to send in data to the fragment

Bundle b = new Bundle()
b.putString("text",data)
conv.setArguments(b);

then in your fragment get the arguments with getArguments() and pull the data from the bundle and use it however you need

Android Fragment TextView setText from activity

You can call your setText() method in your fragment from your MainActivity like this:

FragmentManager fm = getSupportFragmentManager();

//if you added fragment via layout xml
YourFragmentClass fragment = (YourFragmentClass)fm.findFragmentById(R.id.your_fragment_id);
fragment.setText();

If you added fragment via code and used a tag string when you added your fragment, use findFragmentByTag instead like this:

YourFragmentClass fragment = (YourFragmentClass)fm.findFragmentByTag("yourTag");

Hope it helps.

How to change TextView Value in fragment

If you are using .add in your goToFragment() code, then you should replace with .replace

fragmentTransaction.add(R.id.container,
currentFragment).commit()

to

fragmentTransaction.replace(R.id.fragment_container,
currentFragment).commit()


Related Topics



Leave a reply



Submit