How do you access your custom view's android:layout_width and android:layout_height values?
Sample xml:
<com.example.CustomButton
android:id="@+id/btn"
android:layout_width="150dp"
android:layout_height="70dp" />
In CustomButton:
@Override
protected void onMeasure(int widthSpec, int heightSpec) {
int measuredWidth = MeasureSpec.getSize(widthSpec);
int measuredHeight = MeasureSpec.getSize(heightSpec);
// How can we measure based on android:layout_width, android:layout_height?
// Currently this implementation measures off the parent's values
setMeasuredDimension(measuredWidth, measuredHeight);
}
You must get the layout params of the view:
CustomButton.LayoutParams viewParams = yourView.getLayoutParams();
And then you can access or modify the width or view:
viewParams.height;
viewParams.width;
Hope it helps!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With