0

OK, so here's a wobbly one... Before asking my question, I'll point out that I have read this: What is the difference between "px", "dp", "dip" and "sp" on Android? So here's my problem, I have some squares on screen (EditText) which I define in my XML using dp units, and identifying them square1, square2 etc., and they are positioned in a FrameLayout (since absolute layout is deprecated and that I don't see any other layout that would suit my purposes; I want to control the position of the squares on screen by programming, soI use

square1.setX(cc1x); 
square1.setY(cc1y);
square2.setX(cc2x); 
square2.setY(cc2y);

and so on, and where cc1x, cc1y, cc2x, cc2y are int variables that change according to the logic of the app. The problem is that the java syntax doesn't allow adding a unit to this integer value of the variables, so on some screens my app works just perfectly, but on screens with a different density, everything is either too far apart, either overlapping...

And finally the question(s): Is there a way of "forcing" the units into the setX / setY statements?

or

Is there a way to get the density of the screen of the device where my app will run? (in which case I could recalculate the X/Y coordinates accordingly) (I didn't do research on this second possibility yet, as I just thought of it whilst writing this, so please accept my apologies if it is a duplicate of some other question). Thanks in advance for your thoughts.

Community
  • 1
  • 1
  • 1
    If you could convert between px and dp on your device would that solve your problem? I'm sure I have some code that does that somewhere – Richard Tingle Dec 15 '14 at 20:28
  • What exactly are you trying to do? Android should handle scaling for you in this case. You can cast ints to floats (since I think that is what setX and setY take), but I am not sure that is the appropriate answer as it would likely be handled more appropriately with better formatted xml. – zgc7009 Dec 15 '14 at 20:28
  • you just need to multiply by the dp/px coefficient. – njzk2 Dec 15 '14 at 20:28
  • 2
    This may also be helpful: http://stackoverflow.com/questions/3166501/getting-the-screen-density-programmatically-in-android – Richard Tingle Dec 15 '14 at 20:29
  • To Richard Tingle: I think that's the approach that'll do the trick, also the info out of the link you've posted. To zgc7009, a better formatted xml? Could you suggest an xml format in which I can define by programming where my text fields end up? To njzk2; yes of course, but my question was how can I get that coefficient, luckily Richard gave me the clue. Thanks all 3 of you anyway. – Evan Martinez Dec 15 '14 at 20:50
  • I guess I am just a bit confused why you are setting your views positions dynamically. I feel like this situation could likely be avoided if your xml was formatted differently allowing Android to handle your positioning for you. Of course, I don't know exactly what you are doing and it seems as though you have found a solution so whatever works :) – zgc7009 Dec 15 '14 at 21:16

2 Answers2

0

You can add this method to easily convert between DP on the screen and PX of the device

 public static float convertDpToPx(Context context, int dp) {
    return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, context.getResources().getDisplayMetrics());
}
Austin Musice
  • 543
  • 4
  • 8
  • 1
    It's better to use TypedValue.COMPLEX_UNIT_DIP instead of value "1", because its value may be changed in future. – user2340612 Dec 15 '14 at 21:54
0

I wanted to update on this thread, since I've found a far better solution to the problem, though the answers you guys (& girls?) really did teach me a lot, for which my gratitude. Finally, njzk2's answer has made me reconsider the whole approach to what I wanted to do. So, instead of moving my boxes around, which resulted in the dimensioning problems, now I just move the content of the boxes around, leaving the boxes nicely positioned in place, which is by far more easy. Anyway, I hope this will result usefull to other starters like me.