android - Use text from EditText in activity1 on button in activity2 -
i want button in activity2 show text entered in edittext in activity1. first app using more 1 activity may trying pass things wrong. i've tried setting button text direct edittext value saving didn't work right. direction appreciated!
activity1
start_day called when button in activity1 pressed. sends app activity2.
public class addtasksactivity extends appcompatactivity { public final static string taskone = "task 1 content"; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_add_tasks); } public void start_day(view view){ intent intent = new intent(this, daytasksactivity.class); intent.putextra("taskone", taskone); startactivity(intent); } }
activity2
currently button shows text activity1 "task 1 content." i'm doing right in passing things around can't seem working , haven't quite found yet.
public class daytasksactivity extends appcompatactivity { @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_day_tasks); bundle bundle = getintent().getextras(); string taskone = bundle.getstring("taskone"); button buttontaskone = (button) findviewbyid(r.id.button_task_one); buttontaskone.settext(taskone); } }
- first give id views in layout files
- then instantiate views in respective activity class
- then text edit text , save in bundle , pass it
- get value bundle in second activity
- set value in button
activity1
public class addtasksactivity extends appcompatactivity { private edittext myedittext; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_add_tasks); myedittext = (edittext) findviewbyid(r.id.put_your_id_here_form_layout_file); } public void start_day(view view){ intent intent = new intent(this, daytasksactivity.class); intent.putextra("taskone", myedittext.gettext().tostring()); startactivity(intent); } }
activity2
public class addtasksactivity extends appcompatactivity { private button buttontaskone; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_day_tasks); buttontaskone = (button) findviewbyid(r.id.button_task_one); bundle bundle = getintent().getextras(); string taskone = bundle.getstring("taskone"); if (taskone != null){ buttontaskone.settext(taskone); } } }
Comments
Post a Comment