Andre Amaral Braga
Andre Amaral Braga

Reputation: 321

RelativeLayout size and position (margins)

I want my RelativeLayouts be at the same size and position independently of the size of the device screen (relatively). For example:

If device A has a screen width of 480 and height of 800 and device B has a screen width of 720 and height of 1280, my RelativeLayout must have, for example, 160 width and 267 height in device A and 240 width and 427 height in device B. It's simple, just get the screen width and height and calculate the percent you want. This part I did with this code below and it's working:

int width = methodToGetScreenWidth(); 
relativeOneWidth = ((width / 100) * 90); // 90% of the screen width.
relativeOneHeight = (int) (relativeOneWidth / 1.75f); 
relativeTwoWidth = (relativeOneWidth / 2); 
relativeTwoHeight = (relativeOneHeight / 4); 
relativeThreeWidth = (relativeOneWidth / 4); 
relativeThreeHeight = (relativeOneHeight / 4);

It's working fine in all devices I tested. The problem is the position. I tried a lot of things to calculate the margins, but it didn't be at the same position in every devices (most times it's very close to the desired position, but not the exactly). Here's what I tried:

relativeOneLeftMargin = ((width - relativeOneWidth) / 2); // The screen width - the relativeOne width / 2, it should returns the relativeOne left margin, shouldn't it? 
relativeOneTopMargin = ((height - relativeOneHeight) / 2); // And this, the relativeOne top margin, right?
relativeTwoLeftMargin = (int) (relativeOneLeftMargin * 1.5f);
relativeTwoTopMargin = (int) (relativeOneTopMargin * 0.78f);
relativeThreeLeftMargin = relativeOneTopMargin + relativeOneHeight;

P.S.: The relativeOne is in the middle of the screen:

relativeOne.addRule(RelativeLayout.CENTER_IN_PARENT);

I also tried to get the margins this way, but it returns 0:

relativeOne.getLeft();
relativeOne.getTop();

I really don't know why it's not in the same position in all devices. Any suggestion?

Upvotes: 0

Views: 167

Answers (2)

Andre Amaral Braga
Andre Amaral Braga

Reputation: 321

I changed this code:

relativeTwoLeftMargin = (int) (relativeOneLeftMargin * 1.5f);
relativeTwoTopMargin = (int) (relativeOneTopMargin * 0.78f);

To this one:

relativeTwoLeftMargin = Math.round(relativeOneLeftMargin * 1.5f);   
relativeTwoTopMargin = Math.round(relativeOneTopMargin * 0.78f);

Now it's working. I guess the int cast did not return the exactly value.

Upvotes: 0

Alexe Mihai
Alexe Mihai

Reputation: 36

First, when using positioning in different devices, it is recommended to use DP (device-dependent points). You can see how to use them at http://developer.android.com/guide/practices/screens_support.html

When you use:

relativeOne.getLeft();
relativeOne.getTop();

You will always get 0, as, at the moment you make this call, Android did not finish drawing the parent of your layout, thus it has no information about its relative positioning. If you want to has access to this information, you have to make the call after the drawing. One way to do this is by using method post of class View:

parent.post(new Runnable() {
            @Override
            public void run() {
                relativeOne.getLeft();
                relativeOne.getTop();
            }
        });

In this way, you execute the call after the layout has been drawn.

Upvotes: 1

Related Questions