Android How to set click listener of a button which is from another layout xml file -
mainactivity shows viewpager there 3 layout files:activity_main.xml、card1.xml , card2.xml want view card1.xml , set listener. should do? i tried using this: layoutinflater layout=this.getlayoutinflater(); view view=layout.inflate(r.layout.card1, null); button b=(button)view.findviewbyid(r.id.b); then set onclicklistener: b.setonclicklistener(new myclicklistener(0)); but useless. public class myclicklistener implements onclicklistener { @override public void onclick(view v) { switch (v.getid()) { case r.id.your_btn_id: // code break; } } } another way can add button click listener in card1.xml fragment class.