Dynamically change width of spinner in Android

Rynardt picture Rynardt · Aug 17, 2012 · Viewed 8.2k times · Source

I tried to dynamically change the width of a spinner with the code below, but I get an error message. What is the correct way to do this?

Spinner s1 = (Spinner) findViewById(R.id.s1_10);
s1.setLayoutParams(new LayoutParams(400, LayoutParams.WRAP_CONTENT));

Answer

prolink007 picture prolink007 · Aug 17, 2012

Do something like the following, i know it looks ugly, but this seems like the only way at the moment.

ViewGroup.LayoutParams params = mSomeView.getLayoutParams();
params.width += 10;
s1.setLayoutParams(params);