Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Problems using setMarginEnd and setMarginStart on Android

I'm trying to set some margins by code. Nevertheless I'm having troubles setting the Start and End margins. For a strange reason using the methods setMarginStart and setMarginEnd does not modify the view left and right margins. I'm attaching the code that I'm using:

    RelativeLayout.MarginLayoutParams  params = (RelativeLayout.MarginLayoutParams) loopMainLayout.getLayoutParams();
    float fabRadius = context.getResources().getDimension(R.dimen.fab_size) / 2;
    params.bottomMargin = (int) (params.bottomMargin + fabRadius);
    params.topMargin = (int) (params.topMargin + fabRadius);
    params.setMarginEnd(params.getMarginEnd() + (int) fabRadius);
    params.setMarginStart(params.getMarginStart() + (int) 
    loopMainLayout.setLayoutParams(

Am I missing any kind of function call here in order to let the layout calculate the end and start margins?

Note: If I use params.leftMargin and params.rightMargin everything woks as supposed. But I'm trying to use end and start margins as best practice.

like image 970
vicmns Avatar asked Aug 02 '26 20:08

vicmns


1 Answers

A bit late to the party but I found a working solution for whoever will stumble upon this like me. You need to call setLayoutParams(LayoutParams) back on the view with the updated params as per documentation in the source code (look for MarginLayoutParams.startMargin|endMargin) MarginLayoutParams

like image 154
Cosmin Radu Avatar answered Aug 04 '26 11:08

Cosmin Radu



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!