java - Add a Button dynamically to a LinearLayout in Android -


i working on project needs add buttons dynamically. whenever run application application force closes. i've learned problem when try add buttons.

package com.feras.testproject; import android.app.activity; import android.os.bundle; import android.view.layoutinflater; import android.view.view; import android.view.view.onclicklistener; import android.view.viewgroup.layoutparams; import android.widget.button; import android.widget.linearlayout;  public class testproject extends activity {     /** called when activity first created. */     @override     public void oncreate(bundle savedinstancestate) {         super.oncreate(savedinstancestate);      setcontentview(r.layout.main);     addall();     // set text button in old testament    } public void addall() {     linearlayout linearlayout = (linearlayout)findviewbyid(r.id.layout1);     button btn = new button(this);      btn.settext("mybutton");      linearlayout.addview(btn);        } } 

try this:

linearlayout.addview(                      btn,                       new layoutparams(                           layoutparams.wrap_content,                            layoutparams.wrap_content)                      ); 

Comments

Popular posts from this blog

php - What is the difference between $_SERVER['PATH_INFO'] and $_SERVER['ORIG_PATH_INFO']? -

fortran - Function return type mismatch -

queue - mq_receive: message too long -