Button not getting displayed after parsing the XML
I have an XML parser which is derived from Activity class and it parses a Button from XML. If i create Button using Android Button API it is getting displayed on the activity. But when i create my own class for taking button attributes and display button from that class it is not getting displayed. This own class which i am creating extends LinearLayout and开发者_开发技巧 is unable to display.
I am calling the constructor of GuiButton class with required parameters. Please find the code below.
public class GuiButton extends LinearLayout{
String label;
int type;
public String reqType;
public String context;
/**
* Network message for this button
*/
public String netMsg;
/**
* network image id for this button
*/
public String networkImageID;
public String id;
Button btn;
public GuiButton(Context cntxt,String label,int type,String requestType,String netImgID, String id,String context,int priority,int commandType) {
super(cntxt);
this.setOrientation(VERTICAL);
btn = new Button(cntxt);
btn.setText(label);
addView(btn,new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
}
What should i do in order to display this button on Activity? Please help me in solving this problem.
replace your constructor
public GuiButton(Context cntxt,String label,int type,String requestType,String netImgID, String id,String context,int priority,int commandType)
with
public GuiButton(Context cntxt, String label)
and set your onCreate method as mentioned bellow
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
LinearLayout l = (LinearLayout)findViewById(R.id.linear);
GuiButton gb = new GuiButton(this,"Hello");
l.addView(gb);
}
精彩评论