0

I want an image to occupy for each screen resolution the same space for all resolutions. E.g.: I want an image to always be 1/5 of the screen.

I have the following code:

private boolean CheckHeaderSize()
{
    Point size = new Point();
    getWindowManager().getDefaultDisplay().getSize(size);

    maximumHeaderSize = size.y / 5;

    BitmapFactory.Options dimensions = new BitmapFactory.Options();
    dimensions.inJustDecodeBounds = true;
    BitmapFactory.decodeResource(getResources(), R.drawable.header, dimensions);

    headerSize = dimensions.outHeight;
    if (dimensions.outHeight > maximumHeaderSize)
        return false;

    return true;
}

But in different screen resolutions is giving me different values... Obviously. I've been googling and I can't find the correct solution...

How can I make the image alwas use the same space and doesn't matter screen resolution?

Sonhja
  • 8,230
  • 20
  • 73
  • 131
  • Check this link http://stackoverflow.com/questions/7168770/how-to-place-views-in-a-specific-location-x-y-coordinates/29025959#29025959 – DJphy Jun 01 '15 at 11:38

1 Answers1

1

Consider using layout_weights and LinearLayouts.

That way you can divide the screen into set portions, regardless of screen resolution.

Fabulous example here

Zain
  • 2,336
  • 1
  • 16
  • 25
  • See answer on what you put as an example in this link: http://stackoverflow.com/questions/9430764/why-are-nested-weights-bad-for-performance-alternatives. – Sonhja Jun 01 '15 at 13:58
  • Fair, but for small layouts, & few levels of nesting the performance loss is negligible. I would still use nested weights for most apps! See the comment (copied below) the marked answer, on the link you posted: "Good thing to be aware of, which I suppose is the purpose of the message. I would note that an exponential impact is still tiny if the exponent involved is small. For small depths of nesting, to not use the CPU required to do this is like having a workhorse that you pamper all week and only lead out for walks on Sundays. Still, for large depths of nesting, it's a point well taken. " – Zain Jun 01 '15 at 14:06