1
votes

I'm creating an Android custom view and overriding the view's

void onSizeChanged(int w, int h, int oldw, int oldh)

The value for w and h are 300 when I create the layout with values:

android:layout_width="100dp" android:layout_height="100dp"

I'm having trouble finding documentation as to the units used for the w and h parameters. Are they in pixels?

2
"Are they in pixels?" - Yep. - Mike M.
Pixels. All dimensions in code are pixels. You can convert from DP if you need to. - Gabe Sechan

2 Answers

0
votes

Yes they are in pixels , you can convert dps into pixel for your use their.

float px = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 14, r.getDisplayMetrics());

This way you can have pixels with your desired dp's and use them in code accordingly . Hope this helps :)

0
votes

When this function is called, the view size has changed If we change the display size code, we can take the height and width of this function and use these parameters inside functions as a new dimension.

Like the code below

    @Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
    super.onSizeChanged(w, h, oldw, oldh);
    if (mHeight == 0 && mWidth == 0 && w != 0 && h != 0) {
        mHeight = getHeight();
        mWidth = getWidth();
    }
}

Now, whenever the height and width change, I can use it and enjoy it:):)