0

I am trying to position an image in my android app so that it will display in the middle of the width of screen and lower part of the height of the screen. I need it to look like this.

enter image description here

My image is 200x50, so I used following logic to achieve this.

Display display = ((Activity)context).getWindowManager().getDefaultDisplay();
screenWidth = display.getWidth();
screenHeight = display.getHeight();
canvas.drawBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.play), screenWidth/2 - 100, 3*screenHeight/4 - 25, null);

But still I am not getting located at correct place and I am getting it like this.

enter image description here

What I am doing wrong here? How can I do this?

Chamila Wijayarathna
  • 1,815
  • 5
  • 30
  • 54

3 Answers3

1

Can you try this

Display display = ((Activity)context).getWindowManager().getDefaultDisplay();
screenWidth = display.getWidth();
screenHeight = display.getHeight();
Bitmap playImage = BitmapFactory.decodeResource(getResources(), R.drawable.play);
canvas.drawBitmap(playImage, (screenWidth/2) - (playImage.getWidth()/2), 3*screenHeight/4 - 25, null);
Progen
  • 129
  • 8
0

why there is an need to write java code for this ?

just specify properties from xml.

center horizontal = true and margin bottom = "some value"

its done

Dnyanesh
  • 331
  • 3
  • 12
  • Hi, I'm very new to this, I don't have an xml file like you have mentioned, I created a GamePanle object extending SurfaceView and added the code I mentioned there. Where should I add an xml code to do this and how should I refer to it? – Chamila Wijayarathna Aug 11 '16 at 04:50
  • Its definitely not 'done'. Not with `margin_bottom` above, mainly because most `ViewGroup` classes align their children to top-left – Shaishav Aug 11 '16 at 04:50
  • @ChamilaWijayarathna - Also keep in mind that you can achieve programmatically what you can in XML ... I'd suggest attempting use of XML for layout in most cases for style and separating view from logic as much as possible. In code, I believe "center horizontal" is related to "gravity" on layout items, and margins should be available on most (if not all) widgets and views. – Del Aug 11 '16 at 04:55
0

Try this

alignParentBottom - true
centerHorizontal - true
marginBottom (Calculate 15% of height and set it)
Viswanath Lekshmanan
  • 9,945
  • 1
  • 40
  • 64