coderslay
coderslay

Reputation: 14370

How to set the height and width of linear layout dynamically in android

I have a Linearlayout in my xml which has fill_parent for both height and width... Now i want to dynamically change the height and width... I tried Using

_llChooseType.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,getWindowManager().getDefaultDisplay().getHeight()-100));

But it is giving me java.lang.ClassCastException: android.view.ViewGroup$LayoutParams exception...

How to do it?

Upvotes: 1

Views: 20554

Answers (4)

Sujit
Sujit

Reputation: 10622

Use this..

new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,getWindowManager().getDefaultDisplay().getHeight()-100));

Upvotes: 6

Nallath
Nallath

Reputation: 2106

The setLayoutParams widht & height can only have the following 3 values: FILL_PARENT, MATCH_PARENT and WRAP_CONTENT.

You should use public void layout (int l, int t, int r, int b) To actually change the layout of the created view/viewgroup.

Upvotes: 1

Ziteng Chen
Ziteng Chen

Reputation: 1969

If the _llChooseType is a LinearLayout object then I think you should use

new LinearLayout.LayoutParams(...);

Regards

Ziteng Chen

Upvotes: 2

dragonfly
dragonfly

Reputation: 411

Do this... Type:

_llChooseType.setLayoutParams(new Layo

and press ctrl+space_bar. This will give you a list of LayoutParams options to choose from...select LinearLayout.LayoutParams and write the rest of the code as is. The problem here is that the LayoutParams you use are ViewGroup.LayoutParams and they don't apply to linear layouts!

Upvotes: 0

Related Questions