Roy
Roy

Reputation: 656

Why setWidth does not work?

For example, for a TextView in Android you have to set the LayoutParams instead of the setWidth method.

TextView tv = new TextView(getContext());
LayoutParams params = new LayoutParams(100, LayoutParams.WRAP_CONTENT);
tv.setLayoutParams(params);

But why? In the Android docs it says it would deliver the same result.

Upvotes: 10

Views: 2032

Answers (1)

garlic
garlic

Reputation: 23

You have to use layoutParams because setting a different width or height can change all the positioning of the layout. All the view hierarchy has to accommodate to the new dimensions, the only way is changing the layout params. The parent then gets notificated and starts all the process of measuring the views of the children.

Upvotes: 2

Related Questions