Skip to content Skip to sidebar Skip to footer

Change Fragments Textview From Activity

I want to change fragments textview from activity(not fragment class).How can I do it ? I am using this code: public void startChat() { FrameLayout layout = (FrameLayout)findVi

Solution 1:

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

Bundleb=newBundle()
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

Solution 2:

You can make like this

publicclassConversationFragmentextendsFragment {

    privateTextView nameView;

    @OverridepublicViewonCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

        View view =  inflater.inflate(R.layout.conversation_fragment, null);
        nameView = (TextView) view.findViewById(R.id.user_name);
    }

    publicvoidsetText(String yourText){
        nameView.setText(yourText); 
    }
}

and in your activity call method setText()

ConversationFragmentconv=newConversationFragment();
conv.setText("asd");

With best regards.

Post a Comment for "Change Fragments Textview From Activity"