Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Class Dimension for java on android

What is the equivalent form of class java.awt.Dimension for android?

like image 681
Joker Avatar asked Jan 16 '12 05:01

Joker


1 Answers

You can choose one of these options:

  1. android.util.Size (since API 21). It has getWidth() and getHeight(), but note it's immutable, meaning that once created you can't modify it (only create new, updated instances).

  2. android.graphics.Rect. It has getWidth() and getHeight() but they're based on internal left, top, right, bottom and may seem bloated with all its extra variables and utility methods.

  3. android.graphics.Point which is a plain container, but the name is not right and it's main members are called x and y which isn't ideal for sizing. However, this seems to be the class to use/abuse when getting display width and height from the Android framework itself as seen here:

    Display display = getWindowManager().getDefaultDisplay();
    Point size = new Point();
    display.getSize(size);
    int width = size.x;
    int height = size.y;
    
like image 175
Stephan Henningsen Avatar answered Oct 13 '22 07:10

Stephan Henningsen