44

If AbsoluteLayout is deprecated what can I use instead of it?

I've done an app that uses AbsoluteLayout but it doesn't work well with the different screen resolutions. I use because I can set the X and Y position of a button. Can I set the position of a button using another layout?

JoeyCK
  • 1,384
  • 4
  • 19
  • 29
  • 3
    You will be better served describing what you are trying to achieve. Part of the reason `AbsoluteLayout` is deprecated is because it does not support multiple screen sizes well. Any direct corollary to `AbsoluteLayout` (e.g., `FrameLayout` with margins) will suffer the same problem. You should start by seeking other solutions your problem, ones that will work better with a wide range of devices. – CommonsWare Aug 22 '10 at 00:06
  • Thank you. I've added some info now. – JoeyCK Aug 22 '10 at 12:08
  • I've just released a library that would have been of interest for the OP: https://github.com/ManuelPeinado/ImageLayout – Manuel Apr 18 '13 at 06:35
  • I like AbsoluteLayout, it puts things where I tell it. I have an example using a button that is twice the width of another button but both come out the same size because some smart-ass layout thinks it knows what I am trying to do. – Paul McCarthy Jan 28 '20 at 01:02

5 Answers5

24

you can use RelativeLayouts as described here: Set the absolute position of a view

Community
  • 1
  • 1
Kyle
  • 10,839
  • 17
  • 53
  • 63
  • 3
    The big problem with RelativeLayout is that if you nest them, the number of calls to OnMeasure doubles at every step. So if you need a deep hierarchy, this is not a good answer. – William Jockusch Jan 06 '15 at 19:41
  • @WilliamJockusch I found this out the hard way myself. The solution (best sofar) is to use LinearLayout whenever it is possible (and it usually is), and to nest RelativeLayouts inside of LinearLayouts. I had a table view which I made with relative layout with nested RelativeLayouts - on changing only the top view to LinearLayout, I got a normal execution time. I would speculate that you could hack an "AbsoluteLayout" by creating a LinearLayout with RelativeLayout inside of it, but the sound of that is terrible. – user1122069 Mar 30 '16 at 10:56
12

Use RelativeLayout and set left and right margins like

RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
lp.leftMargin = x;
lp.topMargin =  y;
Lavanya
  • 3,903
  • 6
  • 31
  • 57
Ganesh Kanna
  • 2,269
  • 1
  • 19
  • 29
6

Consider using FrameLayout. If you set your child gravity to TOP|LEFT then you can use leftMargin and topMargin as the positions. As a bonus you get a few other useful positioning mechanisms by changing the gravity.

fluffy
  • 5,212
  • 2
  • 37
  • 67
1

I will suggest you guys if you want to have full control over the positions of your views on the screen just to extend ViewGroup and make your own implementation of AbsoluteLayout. The easiest solution will be to use one of the existing Layouts and play with margins, paddings, gravity and so on, but the control is not gonna be so powerful and can cost some problems on the diff device screens.

Stoycho Andreev
  • 6,163
  • 1
  • 25
  • 27
0

I would just use a relative layout and set it based off of the other items/edges of the screen. Otherwise your layout appears different on different devices.

Andrew
  • 830
  • 3
  • 10
  • 27
  • 1
    I'm doing a graphic where I have to put text views in EXACT positions on a STATIC image. I think the only way would be (in effect if not technically) an absolute layout. Then I could do ones for various buckets but as it is I'm expecting a pain in the ass with it. – Geeks On Hugs Jul 24 '13 at 23:48