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?
You can modify topMargin of params directly:
LinearLayout.LayoutParams params = (LinearLayout.LayoutParams)tv.getLayoutParams();
params.topMargin = 10;
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With