Get the background padding in your onMeasure to reduce the available width (this is to highlight cells in the grid):
@Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { Drawable background = getBackground(); Rect padding = new Rect(); if (background != null && background.getPadding(padding)); int width = MeasureSpec.getSize(widthMeasureSpec); int availableWidth = width - padding.left - padding.right;
And for onLayout, the add-on is applied:
@Override protected void onLayout(boolean changed, int l, int t, int r, int b) { int availableWidth = r - l - getPaddingLeft() - getPaddingRight();
source share