• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Change top margin programmatically

Greum

Well-Known Member
I'm trying to change the top margin of a textView (within a constraint view if that makes a difference). I tried the following:

MarginLayoutParams p = (MarginLayoutParams) textView38.getLayoutParams();
p.topMargin = 0;

however, the second MarginLayoutParams gives an error: Cannot resolve symbol.

Can anyone point me in the right direction?
 
How are you setting the changed layout params? Something like this, perhaps?

Code:
layoutParams.setMargins(mLeftMargin, mTopMargin, 0, 0);
 
Thanks. I've not tried that. I thought the code I had was supposed to set the top margin only.

Edit:

Is this not setting the parameters for the layout? I'm trying to set the margin for a single textView.
 
Last edited:
Back
Top Bottom