android - Button click event is not working when inflated layout is used -
please can me, since new android not able solution - button onclick event not working if use inflated layout. here usemerge.xml code,
<relativelayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#2996ff" > <linearlayout android:id="@+id/i1" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" > </linearlayout> </relativelayout>
toplayout.xml
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/linear1" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <button android:id="@+id/butt1" android:layout_width="130dp" android:layout_height="wrap_content" android:layout_marginleft="24dp" android:layout_marginstart="20dp" android:layout_margintop="24dp" android:background="@drawable/mybutton" android:text="@string/schedule" /> </linearlayout>
mainactivity.java
private linearlayout lin1; private button bt1; protected void oncreate(bundle savedinstancestate) { // todo auto-generated method stub super.oncreate(savedinstancestate); setcontentview(r.layout.usemerge); final layoutinflater inflater = (layoutinflater)getsystemservice(context.layout_inflater_service); lin1 = (linearlayout)findviewbyid(r.id.i1); view vi=inflater.inflate(r.layout.toplayout,lin1,false); lin1.addview(vi); bt1=(button)vi.findviewbyid(r.id.butt1); bt1.setonclicklistener(new view.onclicklistener() { @override public void onclick(view vo) { // todo auto-generated method stub intent ain=new intent(getbasecontext(), scheduleactivity.class); startactivity(ain); } });
define activity in androidmanifest.xml :
<activity android:name=".scheduleactivity"/>
set click listener before add button linearlayout :
public class mainactivity extends fragmentactivity { private linearlayout lin1; private button bt1; protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.usemerge); lin1 = (linearlayout) findviewbyid(r.id.i1); view vi = layoutinflater.from(this).inflate(r.layout.toplayout, lin1, false); bt1 = (button) vi.findviewbyid(r.id.butt1); bt1.setonclicklistener(new view.onclicklistener() { @override public void onclick(view vo) { intent ain = new intent(mainactivity.this, scheduleactivity.class); startactivity(ain); } }); lin1.addview(vi); } }
Comments
Post a Comment