How to read custom dimension attribute from java code

A few things to say :

  • first you need a xml name space declaration line at the top of your xml, exactly in the same way as you do with android xmlns : xmlns:foo=”http://schemas.android.com/apk/res-auto”
  • then you need to prefix valueTextSize with your xmlns : foo:valueTextSize=”40sp”

After that, it’s not a very good idea to get a string, android offers more powerfull solution to deal with dimensions :

int unitsTextSize = typedArray.getDimensionPixelSize(R.styleable.BasicGauge_unitsTextSize, textSize);

then there are some subtleties :

  • for a Paint, or a TextPaint, you can this value as is : paint.setTextSize(unitTextSize):
  • for a textview, the above approach would fail, and you have to use an overload of setText to get the correct result : textView.setTextSize(TypedValue.COMPLEX_UNIT_PX, unitTextSize);

The difference comes from what is called “raw pixels” (unscaled according to density, just raw) and “scaled pixels” (the opposite).

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)