-3

I want to position an image dynamically.

setX and setY works fine, but in pixels. I want my app to be escalable to every device's screen. Furthermore, I want it to work in landscape mode.

What's the proper method?

Daniel Viaño
  • 485
  • 1
  • 9
  • 26
  • I found a solution [here](http://stackoverflow.com/questions/4605527/converting-pixels-to-dp) The first answer is the good one. – Daniel Viaño Jun 22 '16 at 17:15

1 Answers1

0

setX and setY aren't the proper way to position images in Android.

You have to use either LinearLayout or RelativeLayout to have a design supported on any orientations and any devices.

Layouts and ImageViews can be created and added dynamically.

LinearLayout linearLayout = (LinearLayout) findViewById(R.id.mylayout)
TextView txt1 = new TextView(MyClass.this);
linearLayout.setBackgroundColor(Color.TRANSPARENT);
linearLayout.addView(txt1);

Thanks the source code author : Deepak

You only have to change TextView for ImageView.

Alexandre Martin
  • 1,472
  • 5
  • 14
  • 27
  • But this is for a static case, my image moves on every onLocationChange iteration. I can put image inside a Layout but then I have to move it with something like setX. – Daniel Viaño Jun 22 '16 at 16:32
  • Ok, can you tell a bit more about your goal ? I don't really understand. What your app is doing ? – Alexandre Martin Jun 22 '16 at 16:37
  • I have a bar with a ball inside. Every time location changes, it calculates position ball should have inside that bar. The ball is a Imageview, and the bar a LinearLayout. – Daniel Viaño Jun 22 '16 at 16:48
  • It is working fine for my screen giving a value in pixels to ImageView setX, but I installed the app in another device and realized it doesn't fit. – Daniel Viaño Jun 22 '16 at 16:50