Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to declare wrap content to a dynamic button in code

Tags:

android

Here is my code

Button myButton = new Button(this);
myButton.setText("Press Me");
myButton.setTextColor(Color.WHITE);


LinearLayout layout = (LinearLayout) findViewById(R.id.linearLayout1);
layout.addView(myButton);

How do I add wrapcontent to this button?

like image 386
Sando Avatar asked Aug 01 '11 13:08

Sando


2 Answers

Use this line (I used FILL_PARENT to demonstrate)

layout.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
                LayoutParams.WRAP_CONTENT));
like image 116
Sherif elKhatib Avatar answered Nov 17 '22 20:11

Sherif elKhatib


set the wrap content for button

myButtonsetLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));

or for layout

layout.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
like image 25
Pratik Avatar answered Nov 17 '22 20:11

Pratik