2

The dimens.xml file that comes with the Android platform (located in the data\res\values directory) defines a dimension named action_button_min_width (value is 56dip).

Is there a way I can get this value programatically than hard-coding it in my code?


The suggestions and duplicate references seem to address getting a value from the resources the I've defined in my own app. My question pertains to getting a value from a resource that is part of Android. When I try the following getResources().getString(android.R.dimen.action_button_min_width); I get a compile error because action_button_min_width is not defined.

dazed
  • 352
  • 3
  • 9
  • Did you tried searching? http://stackoverflow.com/questions/11121028/load-dimension-value-from-res-values-dimension-xml-from-source-code – Pedro Oliveira Oct 22 '14 at 11:26

2 Answers2

1

You can use getResources()

Try this:

String butMinWidth = getResources().getString(R.dimen.action_button_min_width);

OR:

int butMinWidth = getResources().getDimension(R.dimen.action_button_min_width);

Hope it helps.

MysticMagicϡ
  • 28,593
  • 16
  • 73
  • 124
  • The suggestions and duplicate references seem to address getting a value from the resources the I've defined in my own app. My question pertains to getting a value from a resource that is part of the Android platform. When I try the following getResources().getString(android.R.dimen.action_button_min_width); I get a compile error because action_button_min_width is not defined. – dazed Oct 22 '14 at 11:34
  • please check import for R – Palak Oct 22 '14 at 12:03
0

1) float value = getResources().getDimension(R.dimen.action_button_min_width);

you need to create dimens.xml file :

<resources>
...
    <dimen name="action_button_min_width">56dp</dimen>
...
</resources>

or

2) String value = getResources().getString(R.dimen.action_button_min_width);

you need to create values/Strings.xml file :

<resources xmlns:android="http://schemas.android.com/apk/res/android">
...
    <dimen name="action_button_min_width">56dp</dimen>
...
</resources>
Palak
  • 2,165
  • 2
  • 21
  • 31
  • I was hoping to get the value defined in the Android platform resource file rather than defining my own resource. – dazed Oct 22 '14 at 11:51