android - How do I add a TextView to another activity -
suppose have 2 activities namely addview
, seeview
.
in seeview
, there button take me addview
activity. in addview
activity, enter text shown on seeview
textview
.
then, clicking button on addview
, taken seeview
added textview addview
displayed. how do programmatically?
views neved transferred between activities, want pass text. should use startactivityforresult()
, onactivityresult()
methods of activity:
public class seeview extends activity { public static final int req_code_add_view; private textview textviewwithtext; @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.layout_of_see_view); //this textview defined in xml: /res/layout/layout_of_see_view.xml //make sure id "@+id/text_view_with_text" textviewwithtext=(textview) findviewbyid(r.id.text_view_with_text); findviewbyid(r.id.button).setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { //intent addview activity intent intent=...; startactivityforresult(intent, req_code_add_view, null); } }) } @override protected void onactivityresult(int requestcode, int resultcode, intent data) { super.onactivityresult(requestcode,resultcode,data); if (resultcode==result_ok) { if (requestcode==req_code_add_view) { string text=data.getstringextra("text", ""); textviewwithtext.settext(text); } } } }
your second activity:
public class addview extends activity { private edittext edittext; @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.layout_of_add_view); //this done in case user quits activity before //button pressed setresult(result_cancelled); textviewwithtext=(textview) findviewbyid(r.id.edit_text); findviewbyid(r.id.another_button).setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { //prepare result seeview activity intent intent=new intent(); intent.putstringextra("text", edittext.gettext().tostring()); //set result, passed onactivityresult() in seeview activity setresult(result_ok, intent); finish(); } }) } }
Comments
Post a Comment