Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot find resource in dimens

Tags:

android

The resource cannot be found. Here is my resource:

dimens.xml

<resources>
    <dimen name="recyclerview_padding">10dp</dimen>
</resources>

In my fragment I tried each of the following:

val padding = getResources().getDimension(R.dimen.recyclerview_padding) as Int

val padding = context.getResources().getDimension(R.dimen.recyclerview_padding) as Int

val padding = activity.getResources().getDimension(R.dimen.recyclerview_padding) as Int

I also tried cleaning my project but this did not work.

like image 501
Johann Avatar asked Nov 17 '25 03:11

Johann


1 Answers

This is how you should get the value

<resources>
<dimen name="recyclerview_padding">10dp</dimen>
</resources>

dimens.xml

val dimenFromXml = resources.getDimension(R.dimen.recyclerview_padding) // Int

It will be in Float by default or you can use getDimensionPixelOffset which will convert it to Int

val dimenFromXml = resources.getDimensionPixelOffset(R.dimen.recyclerview_padding) // Float
like image 66
Manoj Perumarath Avatar answered Nov 19 '25 17:11

Manoj Perumarath



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!