0

Hey how can I do it in Android Studio?

I want to type something in an Edittext ( which it located in an Activity extends AppCompatActivity), and if I click on save, this text will be shown on another Textview (which is located in another class extends Fragments)?

AccountActivity.java:

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

final EditText editText = (EditText) findViewById(R.id.usernameedittext);
TextView save = (TextView) findViewById(R.id.profile_save);

save.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        Intent intent = new Intent(AccountActivity.this, ProfileFragment.class);
        intent.putExtra("name", editText.getText().toString());
        startActivity(intent);
    }
});

ProfileFragment.java:

@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_profile, container, false);


    final TextView username = (TextView) view.findViewById(R.id.profile_username);

    username.setText(getActivity().getIntent().getExtras().getString("name")) }
FloFlo
  • 43
  • 7
  • Have a look at this: https://stackoverflow.com/questions/12739909/send-data-from-activity-to-fragment-in-android – Deepak Mar 13 '20 at 06:16

3 Answers3

0

You can make the textView a static variable and reference to it then you null the value when it is set to prevent memory leak

0

You can send a broadcast with data from activity on save button click method and receive that broadcast in fragment and get data from intent of broadcast and show it in the view.

Edit : Please find below example for sending and receiving broadcast.

Firstly, in ProfileFragment.java, create a broadcast class as:

public class MyBroadcastReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        if(intent.getAction().equals("my_broadcast")){
            final TextView username = (TextView) view.findViewById(R.id.profile_username);
            username.setText(intent.getStringExtra("name"));
        }
    }
}

and create a class level insatance of MyBroadcastReceiver as

MyBroadcastReceiver myReceiver = new MyBroadcastReceiver();

and also add this code

@Override
public void onResume() {
    super.onResume();
    getActivity().registerReceiver(myReceiver, new IntentFilter("my_broadcast"));
}

@Override
public void onPause() {
    super.onPause();
    getActivity().unregisterReceiver(myReceiver);
}

And lastly, you have to send broadcast when you want to update the text. So, in you AccountActivity.java class, update you click method to

save.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        Intent broadcastIntent = new Intent();
                    broadcastIntent.setAction("my_broadcast");
                    broadcastIntent.putExtra("name", editText.getText().toString());
                    sendBroadcast(broadcastIntent);
    }
});

Hope this helps. Good luck!

Massab
  • 1,030
  • 9
  • 22
  • Hey, how do I code this? I am a beginner and I don't know how it works :P – FloFlo Mar 13 '20 at 17:09
  • Hey sorry for the late answer. This code isn't working on me. **final TextView username = (TextView) view.findViewById(R.id.profile_username);** on this line. there is always a problem with the "view". always it is this error **"error: cannot find symbol variable view"** – FloFlo Mar 16 '20 at 19:36
  • @FloFlo you need to do a little change, please make the view variable in `onCreateView` of fragment a global class variable. – Massab Mar 18 '20 at 01:20
  • Is it also right if I do this ? **final TextView username = (TextView) ((Activity) context).findViewById(R.id.profile_username);** But also if I do this, the Textview does not Change if I type something in Edittext – FloFlo Mar 18 '20 at 09:58
  • No, this won't work because you are referring to context of parent activity to find view, but what you have to do is, to use the view of fragment to call findViewById in that view. So, just change the view you are inflating in onCreateView as a class variable and use that in the broadcast. – Massab Mar 18 '20 at 12:18
  • Please accept the answer as well, as it is correctly answering your actual query. – Massab Mar 18 '20 at 12:19
0

Just like @Massab said. You can send a broadcast from one class to another. Here is the answer on how to do it: How to send and receive broadcast message and answer from the android-studio: https://developer.android.com/guide/components/broadcasts