Programatically Add Main LinearLayout and Button to Android App -


there no xml this. question why button not show on screen?

i have added layout setcontentview , added button. why not show?

package com.my.layoutexample;  import android.app.activity; import android.os.bundle; import android.widget.button; import android.widget.linearlayout;  public class mainactivity extends activity {      @suppresswarnings("deprecation")     @override     protected void oncreate(bundle savedinstancestate) {         super.oncreate(savedinstancestate);          linearlayout mainlayout = new linearlayout(null);         mainlayout.setlayoutparams(new linearlayout.layoutparams(                 linearlayout.layoutparams.wrap_content,                 linearlayout.layoutparams.wrap_content));         mainlayout.setorientation(linearlayout.vertical);         setcontentview(mainlayout);          button button = new button(null);         button.settext("send");         button.setlayoutparams(new linearlayout.layoutparams(                 linearlayout.layoutparams.fill_parent,                 linearlayout.layoutparams.fill_parent));         mainlayout.addview(button);     } } 

this works:

@suppresswarnings("deprecation") @override protected void oncreate(bundle savedinstancestate) {     super.oncreate(savedinstancestate);      //1 - need pass context linear layout constructor     linearlayout mainlayout = new linearlayout(this);      //the parent layout must match_parent     mainlayout.setlayoutparams(new linearlayout.layoutparams(             linearlayout.layoutparams.match_parent,             linearlayout.layoutparams.match_parent));     mainlayout.setorientation(linearlayout.vertical);     setcontentview(mainlayout);     //you need pass context button constructor     button button = new button(this);     button.settext("send");     //i set button size of text, fill whole screen (parent) if want doing     button.setlayoutparams(new linearlayout.layoutparams(             linearlayout.layoutparams.wrap_content,             linearlayout.layoutparams.wrap_content));     mainlayout.addview(button); } 

enter image description here


Comments

Popular posts from this blog

javascript - Count length of each class -

What design pattern is this code in Javascript? -

hadoop - Restrict secondarynamenode to be installed and run on any other node in the cluster -