How to change text of textView fragment from activity

I cannot find how to change a textview fragment with Activity . I have 4 files:

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

frag_class.xml has a textView, I want to change the text from MainActivity.java. FragmentClass extends the 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 unfortunately this code gives me a NullPointerException in fc.changeText("some text"); I also tried changing the text directly from MainActivity with:

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

which failed to complete.

[EDIT] Full code here

+27
android textview android-fragments
source share
5 answers

@Lalit's answer is correct, but I see that you do not need to create a function like fragment_obj.updateTextView () ;. I installed all my views as class level objects and was able to directly update the text box.

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

Note. If you need to perform more than one action, then the function will be a way.

+5
source share

You can find the fragment instance 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 Snippet that will update your TextView and call this method using fragment_obj like,

fragment_obj.updateTextView();

+11
source share

From activity to Fragment to 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); } } 
+8
source share

I did it differently. My application works as follows:

After starting my application, I have MainActivity with the created HomeFragment. In HomeFragment, I have a Button and TextView for connecting / displaying BluetoothConnection status.

In HomeFragment, I implemented a handler to get information from BluetoothService. After receiving the message, I wanted to update the TextView and Button text. I created a public interface in HomeFragment using a method that receives String arguments for TextView and Button. In onAttach (Activity a), I created an mCallback object to discuss activity.

The next step implements this interface in MainActivity. From this action, I update the 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); } } 
+1
source share
 public class Dcrypt extends Fragment { Button butD; ImageButton Dcopy; EditText getPass; TextView txtShow; 

use this line of code to get text field support

 private FragmentManager supportFragmentManager; 
0
source share

All Articles