android - Access view of other layout -
i have 1 mainactivity , 2 fragments namely input.java , output.java. want access textview located @ output_layout in input fragment (more precisely, if click on button in input_layout, textview of output_layout should change). how can this? calling these input_layout , out_put layout dynamically in main_activity.
as beginner, pardon ignorance. help highly appreciated. in advance.
try utilize interfaces. example.
public interface onoutputfragmenttextchanger { public void onchangetext(string what); }
in onattach method of input.class next:
@override public void onattach(activity activity) { super.onattach(activity); // makes sure container activity has implemented // callback interface. if not, throws exception seek { mcallback = (onoutputfragmenttextchanger) activity; } grab (classcastexception e) { throw new classcastexception(activity.tostring() + " must implement onoutputfragmenttextchanger"); } }
and in yours onclicklistener phone call mcallback.onchangetext("foooo");
next in output class create method
public void updateinputview(string what) { mtextview.settext(what); }
next implement onoutputfragmenttextchanger in activity , override method onchangetext(string what):
public class mainactivity extends fragmentactivity implements onoutputfragmenttextchanger { //do somwthing @override public void onchangetext(string what) { inputfragment inputfragment = (inputfragment) getsupportfragmentmanager().findfragmentbyid(r.id.fragment); // or seek findfragmentbytag if (inputfragment != null) { inputfragment.updateinputview("some string"); } } }
android android-fragments android-fragmentactivity
No comments:
Post a Comment