4

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));
Rynardt
  • 5,547
  • 7
  • 31
  • 43
  • possible duplicate of [Android: How to resize a custom view programmatically?](http://stackoverflow.com/questions/2963152/android-how-to-resize-a-custom-view-programmatically) – prolink007 Aug 17 '12 at 13:56

1 Answers1

6

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);
prolink007
  • 33,872
  • 24
  • 117
  • 185
  • 2
    Yes, ended up using RelativeLayout.LayoutParams lp = (android.widget.RelativeLayout.LayoutParams) s1.getLayoutParams(); lp.width = 400; s1.setLayoutParams(lp); – Rynardt Aug 17 '12 at 13:56