Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set the dimensions of a view programatically using sdp in andorid?

Tags:

java

android

I want to programmatically set the dimensions of TextView. But I'm using sdp and ssp values. How can I use these values to set the dimensions of a textview programatically.

TextView tasks = new TextView(this);
tasks.setText(name);
tasks.setTextSize(??);
tasks.setMargin(??)
ViewGroup.LayoutParams l1 = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, height??); // what is the height input here?

And how to set the text size (ssp) and margin/padding (sdp)?

like image 249
Deepak Avatar asked Nov 23 '25 17:11

Deepak


2 Answers

I have 2 option for you.

  1. If you just want set text size/ margin for only one screen size. Create file dimens on values folder. And use that code getResources().getDimension(R.dimen.dimen_name). The point is dimens just allow dp, sp and px. But I think that is enough.
  2. In case you want set for multi-screen. You can use that lib com.intuit.sdp:sdp-android and call multi-dimension like that getResources().getDimension(R.dimen._1sdp)
like image 84
TheZero Avatar answered Nov 26 '25 08:11

TheZero


You could make ur dimens.xml file and enter your desired margins/dimensions for each density.

I would suggest using a library from https://github.com/intuit/sdp

You could use this to set your margins or dimensions using

Java ->
getResources.getDimension(R.dimen._**sdp);

Kotlin ->
resources.getDimension(R.dimen._**sdp)

If u want progmatically set text size or sp sizes, use this https://github.com/intuit/ssp

Java ->
tasks.setTextSize(getResources.getDimension(R.dimen._**ssp);

Kotlin ->
tasks.setTextSize(resources.getDimension(R.dimen._**ssp)

Hope this helps! :)

like image 32
M Ashhal Avatar answered Nov 26 '25 06:11

M Ashhal



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!