0

I have two closely-related questions.

In values\dimens.xml I have:

<dimen name     ="small_flag_height">    72sp   </dimen>

In code I have:

    /* a */ int resId = R.dimen.small_flag_height;

At one point today, the assignment statement above was displayed like this:

    int resId = 72sp;  

When I hovered over 72sp, R.dimen.small_flag_height was shown in a popup message;

when I clicked 72sp, the statement was changed to that shown above at /* a */.

(a) How do I get AS to show that value again? (I have no idea how I got it to show the value in the first place.)

EDIT If I close the file and reopen it, 72sp is again displayed. How else can I do this?


The statement below merely shows the assigned int value for R.dimen.small_flag_height, which happens to be 2131099648:

    Log.w("resId","" + resId);

(b) What do I have to do to obtain the value (72sp) assigned to small_flag_height? (I don't want to display it, I want to use it in a computation.)

DSlomer64
  • 4,234
  • 4
  • 53
  • 88
  • 1
    I'm not sure this helps, but try clicking on the resId and theb press ctrl+> this shoukd show the value of your resource. – Mikhail Jul 16 '15 at 17:41
  • As a sidenote, this shortkut also works with method/class bodies the same way – Mikhail Jul 16 '15 at 17:46

2 Answers2

2

getResources().getDimension(R.dimen.small_flag_height); will give you the dimension in pixels, not dips. You will need to convert pixels to dips to complete your calculation.

Look here to see how to convert pixels back to dips: Android - pixels to dips

This answer should work:

public int getDip(int pixel)
{
        float scale = getBaseContext().getResources().getDisplayMetrics().density;
        return (int) (pixel * scale + 0.5f);
 }
Community
  • 1
  • 1
ajacian81
  • 7,419
  • 9
  • 51
  • 64
  • It always amazes me how close I am to my own solution when someone posts what I was just on the verge of figuring out: `density` is indeed what I was looking for but hadn't yet found. – DSlomer64 Jul 16 '15 at 18:13
  • No worries. Good luck with Android. It's a different beast than most other languages/frameworks you've dealt with. – ajacian81 Jul 16 '15 at 19:30
  • You got that right! I thought plain ol' Java was a monster (even worse in Eclipse). I don't know how to classify Android, but "friendly" isn't an adjective I'd include. – DSlomer64 Jul 16 '15 at 20:18
0

This answers (b) above:

 getResources().getDimension(R.dimen.small_flag_height);

However, result is not returned in sp units--i.e., not 72sp or 72, but rather as 159.75002, and I'm not sure what those units are...

EDIT Per doc for getDimension: "Unit conversions are based on the current DisplayMetrics associated with the resources."

(a) is answered in a comment above.

DSlomer64
  • 4,234
  • 4
  • 53
  • 88