Cԃաԃ
Cԃաԃ

Reputation: 1258

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

I cannot find how to change fragment's textview from an Activity. I have 4 files :

MainActivity.java
activity_main.xml
FragmentClass.java
frag_class.xml

frag_class.xml has textView, I want to change the text from MainActivity.java. FragmentClass extends Fragment, this fragment is displayed in MainActivity FragmentClass has:

public void changeText(String text){
 TextView t = (TextView) this.getView().findViewById(R.id.tView);
 t.setText(text);
}

and in MainActivity I tried this:

FragmentClass fc = new FragmentClass();
fc.changeText("some text");

But sadly this code gives me NullPointerException at fc.changeText("some text"); I've also tried changing the text directly from MainActivity with:

 TextView t = (TextView) this.getView().findViewById(R.id.tView);
 t.setText(text);

which Failed.

[EDIT] The full code is here

Upvotes: 32

Views: 67869

Answers (5)

Amank
Amank

Reputation: 1

public class Dcrypt extends Fragment {

Button butD;
ImageButton Dcopy;
EditText getPass;
TextView txtShow;

use this line of code to get support of text field

private FragmentManager supportFragmentManager;

Upvotes: 0

Illegal Argument
Illegal Argument

Reputation: 10338

@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.

Upvotes: 6

Mariusz Brona
Mariusz Brona

Reputation: 1609

I accomplished it other way. I have my app working like this:

After firing my app, I have MainActivity with HomeFragment created. In HomeFragment I have Button and TextView for connecting / showing state of BluetoothConnection.

In HomeFragment I implemented Handler for receiving information from BluetoothService. After receiving message I wanted to update TextView and Button text. I created public interface in HomeFragment with method that is getting String arguments for TextView and Button. In onAttach(Activity a) I created mCallback object for talking to activity.

Next step is implementing this Interface in MainActivity. From this Activity I am updating TextView and Button. Everything looks like this:

HomeFragment.java

public interface ViewInterface{
    public void onViewUpdate(String buttonTxt, String txtTxt);
}

@Override
public void onAttach(Activity a){
    super.onAttach(a);
    try{
        mCallback = (ViewInterface)a;
    }catch (ClassCastException e){
        e.printStackTrace();
    }
}

private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {

                case MESSAGE_STATE_CHANGE:

                    if(true) Log.i(CLASS_TAG, "MESSAGE_STATE_CHANGE: " + msg.arg1);
                    switch (msg.arg1) {
                        case BluetoothConnectionService.STATE_CONNECTED:

                            threadTextString = "Connected to: " + connectedDeviceName;
                            threadBtnString = "Disconnect";
                            mCallback.onViewUpdate(threadBtnString, threadTextString);
                            break;
                        case BluetoothConnectionService.STATE_CONNECTING:
                           threadTextString = "Connecting...";
                            mCallback.onViewUpdate(threadBtnString, threadTextString);
                            break;
                         case BluetoothConnectionService.STATE_NONE:

                            threadBtnString = "Connect";
                            threadTextString = "You're not connectedd";
                            mCallback.onViewUpdate(threadBtnString, threadTextString);
                            break;
}

private void updateBtn(Button btn, String data){
    btn.setText(data);
    Log.d(CLASS_TAG + "/" + "updateBtn", "Data: " + data);
}

private void updateTxt(TextView txt, String data){
    txt.setText(data);
    Log.d(CLASS_TAG + "/" + "updateTxt", "Data: " + data);

}

public void update(String buttonTxt, String txtTxt){
    this.updateTxt(connectTxt, txtTxt);
    this.updateBtn(connectButton, buttonTxt);
}

MainActivity.java

@Override
public void onViewUpdate(String buttonTxt, String txtTxt) {
    HomeFragment homeFragment = (HomeFragment)getSupportFragmentManager().findFragmentById(R.id.frame_container);
    if(homeFragment != null){
    homeFragment.update(buttonTxt, txtTxt);
    }
}

Upvotes: 1

Daryn
Daryn

Reputation: 768

From activity to Fragment by Fragment Transaction:

public class MainActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main); 
    }
    public static void changeFragmentTextView(String s) {
        Fragment frag = getFragmentManager().findFragmentById(R.id.yourFragment);
        ((TextView) frag.getView().findViewById(R.id.textView)).setText(s);  
    }
}

Upvotes: 12

Lalit Poptani
Lalit Poptani

Reputation: 67286

You can find the instance of Fragment by using,

For support library,

YourFragment fragment_obj = (YourFragment)getSupportFragmentManager().
                                              findFragmentById(R.id.fragment_id);

else

YourFragment fragment_obj = (YourFragment)getFragmentManager().
                                             findFragmentById(R.id.fragment_id); 

Then create a method in the Fragment that updates your TextView and call that method using fragment_obj like,

fragment_obj.updateTextView();

Upvotes: 12

Related Questions