I am beginner in android. I am trying to find a way to transfer the text from the editText of an an activity to the fragment so that the result is displayed in the textview of the fragment.
Asked
Active
Viewed 77 times
-4
-
You should really use the Fragment as the entire UI with the EditText itself. – OneCricketeer Jan 19 '17 at 04:32
1 Answers
1
you can use bundle to transfer data from activity to fragment
I suppose data will be sent on button click.
Bundle bundle = new Bundle();
bundle.putString("from_activity", edittext.getText().toString());
Fragmentclass fragment= new Fragmentclass();
fragment.setArguments(bundle);
In fragment
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view;
view=inflater.inflate(R.layout.fragment, container, false);
String text= getArguments().getString("from_activity");
TextView tv=(TextView)findViewById(R.id.yourTextView);
tv.setText(text);
return view;
}

santosh kumar
- 2,952
- 1
- 15
- 27
-
This uses a hard-coded string, not a value from an EditText like the question asked – OneCricketeer Jan 19 '17 at 04:32
-
-
Well, except `edittext.getText().toString()` would be empty while the view is loaded – OneCricketeer Jan 19 '17 at 04:40
-
@cricket_007 edittext.getText().toString() will be taken on button click. It will not be null. – santosh kumar Jan 19 '17 at 04:44
-
It won't be empty if it is taken on button click action correct me if i am wrong. – santosh kumar Jan 19 '17 at 04:45
-
That is correct, but it is worth mentioning in case your code was to be copied into onCreate directly – OneCricketeer Jan 19 '17 at 04:50
-