How to declare wrap content to a dynamic button in code

Viewed 11191

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?

3 Answers

Kotlin

val button = Button(context)
button.layoutParams = ViewGroup.LayoutParams(LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.MATCH_PARENT)
Related