6

I am new to Android. I like having the free range of drawing objects where ever I want. So i have been using Absolute Layout. I get a message saying to use a different layout. And I have read that this is because of the different res of different phones. My question is, is this the only reason in not using Absolute Layout? I have made a method that uses metrics to adjust the pixels.

public int widthRatio(double ratioIn){
    DisplayMetrics dm = new DisplayMetrics(); //gets screen properties
    getWindowManager().getDefaultDisplay().getMetrics(dm);
    double screenWidth = dm.widthPixels;      //gets screen height
    double ratio = screenWidth/100;           //gets the ratio in terms of %
    int displayWidth = (int)(ratio*ratioIn);  //multiplies ratio desired % of screen 
    return displayWidth;
}

//method to get height or Ypos that is a one size fits all
public int heightRatio(double ratioIn){
    DisplayMetrics dm = new DisplayMetrics(); //gets screen properties
    getWindowManager().getDefaultDisplay().getMetrics(dm);
    double screenHeight = dm.heightPixels;    //gets screen height
    double ratio = screenHeight/100;          //gets the ratio in terms of %
    int newHeight = (int)(ratio*ratioIn);     //multiplies ratio by desired % of screen
    return newHeight;
}

//sets size of any view (button, text, ...) to a one size fits all screens
public void setSizeByRatio(View object, int width, int height){
    LayoutParams params = object.getLayoutParams();
    params.width = widthRatio(width);
    params.height = heightRatio(height);
}

So if i say setSizeByRatio(Button, 10, 25); It will set the buttons width to 10% of the width of the screen and the height to 25% percent of the screen.

Are there any phones that Absolute Layouts do not work on? Does this layout cause any other issues?

JBreezy901
  • 197
  • 2
  • 8

2 Answers2

6

The reason why the AbsoluteLayout is deprecated is because you have alternatives in the LinearLayout or the GridLayout that does the same and more. It seems that you are trying to calculate positions based on absolute positions and number of pixels, an approach that should in general be avoided due to issues with varoius screen sizes and densities.

Read the link that @amiekuser provided and focus on the understanding how the best practice is. Some hints are creating images for ldpi, mdpi and hdpi folders, using the unit for dpi (density independent pixels) instead of raw pixels and how to test your app on multiple screen sizes and densities using the emulator.

Edit:

To set the x- and y-position of a View you must use LayoutParams. See this question on how to to set the TopMargin and LeftMargin for a View using LayoutParams.

Community
  • 1
  • 1
Krøllebølle
  • 2,878
  • 6
  • 54
  • 79
  • Thanks for that. It was real helpful. I have been trying to set the x and y on relative layout but I could never get it to work. I just assumed margin would go of what ever was beside it and not always off the outer edge of the parent. Going to use it to play around with some of my methods and try it on some different emulators. I will let you know how it is – JBreezy901 Mar 17 '12 at 03:48
3

Android phones comes in many form factors i.e. not only it varies greatly in terms of screen size(2.7", 3.2", 3.7" ....) but also in terms of resolution (480X800, 480X848 etc). Google itself suggest not to use AbsoluteLayout. in fact its deprecated in the newer api versions.

The link below explains all these in details:

http://developer.android.com/guide/practices/screens_support.html

Check the best practices section.

amiekuser
  • 1,630
  • 1
  • 19
  • 31
  • thanks for the link. It was helpful. It seems to me my method of scaling and getting "density independence" is alot like there's. I have used dip and dp and I do not REALLY get the exact proportions I want across all devices. Is there any other layouts that you can define the x and y positions? Does deprecated mean it will not work on all devices? – JBreezy901 Mar 15 '12 at 09:20
  • Deprecated usually means that there are newer and better alternatives to which you can achieve what you want. Using the AbsoluteLayout will work, but as described in my post you should use LinearLayout, GridLayout or RelativeLayout. – Krøllebølle Mar 15 '12 at 09:23
  • I have tried linearlayouts and RelativeLayout, You cant define a x and y pos. Is there anything outside of Absolute that lets you define x and y pos? I look forward to reading your comment in the morning. – JBreezy901 Mar 15 '12 at 09:45
  • O. and thanks for actually answering my question. I did read the link. It was useful. That is what took me 25min to respond. – JBreezy901 Mar 15 '12 at 09:47
  • @JBreezy901 See my edited code for a link to a similar question. You have to use LayoutParams and the leftMargin (x-value) and rightMargin (y-value) parameters. LayoutParams are available for all layouts (I think). – Krøllebølle Mar 15 '12 at 12:29