MadCode
MadCode

Reputation: 7

Android get Frame dp dimension

Any way to get layout (frame, linear ...) in dp? My FrameLayout is set to be wrap content so I calculate dimension of items inside to get the dimension but I am not sure about the reliability; any solution? Thanks in advance

Upvotes: 0

Views: 464

Answers (1)

Shadik Khan
Shadik Khan

Reputation: 1215

do this like below code

int height = yourLayout.getLayoutParams().getHeight;
int width = yourLayout.getLayoutParams().getWidth;

It will give you height, width in pixel

Carefull

Write this code after your layour(views) created successfully, Otherwise it will return 0.

Now convert this px to dp by using the following method

public int pxToDp(int px) {
    DisplayMetrics displayMetrics = getContext().getResources().getDisplayMetrics();
    int dp = Math.round(px / (displayMetrics.xdpi / DisplayMetrics.DENSITY_DEFAULT));
    return dp;
}

Upvotes: 1

Related Questions