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

Time:09-10

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)?

CodePudding user response:

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)

CodePudding user response:

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! :)

  • Related