I am new to Android. I like to have a free range of drawing objects wherever I want. Therefore, I use Absolute Layout. I get a message saying that I am using a different layout. And I read that this is due to the different resolution of different phones. My question is that this is the only reason you are not using Absolute Layout? I created a method that uses metrics to adjust 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 width of the buttons to 10% of the width of the screen and the height to 25% of the screen.
Are there any phones that Absolute layouts do not work on? Does this layout cause any other problems?
source share