How do I set margin in a TextView in java source code?

1

When I'm in xml , it only applies the property:

android:margin="15"

I want to apply this same property, but in the source code of java , how do I do this?

    
asked by anonymous 24.04.2015 / 02:20

1 answer

2

Just set the margin in LayoutParams of View and ask for a new evaluation of Layout :

View view = ...

ViewGroup.LayoutParams lp = view.getLayoutParams();

if(lp instanceof ViewGroup.MarginLayoutParams) {

    ViewGroup.MarginLayoutParams mlp = (ViewGroup.MarginLayoutParams) lp;

    lp.margin = 15;

    // Nao esqueca de requisitar o reajuste no layout
    view.requestLayout();

}
    
24.04.2015 / 02:24