Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set Top Margin Only in android?

I am trying to set the top margin only pragmatically, i am doing this

 TextView tv = (TextView)findViewById(R.id.my_text_view);
 LinearLayout.LayoutParams params = (LinearLayout.LayoutParams)tv.getLayoutParams();
 params.setMargins(0, 0, 10, 0); //substitute parameters for left, top, right, bottom
 tv.setLayoutParams(params);

now the issue is i've given some value of right margin left margin values in xml file, and by doing this through coding in java class it disturbs the xml values, is there any way to set the top margin only?

like image 556
Steve Avatar asked Jan 31 '26 08:01

Steve


2 Answers

You can modify topMargin of params directly:

LinearLayout.LayoutParams params = (LinearLayout.LayoutParams)tv.getLayoutParams();
params.topMargin = 10;
like image 137
Lawrence Choy Avatar answered Feb 01 '26 20:02

Lawrence Choy


I'm guessing the answer looks something like this:

     TextView tv = (TextView)findViewById(R.id.tv_1);
     LinearLayout.LayoutParams params = (LinearLayout.LayoutParams)tv.getLayoutParams();
     params.topMargin = 10;
     tv.setLayoutParams(params);
like image 37
NameSpace Avatar answered Feb 01 '26 21:02

NameSpace



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!