Home Ask Login Register

Developers Planet

Your answer is one click away!

Mohamed Samsudeen February 2016

onLayout() behavior in Android

I have my custom view which will add button controls dynamically. In my onLayout method, I have a logic something like below.

protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
    super.onLayout(changed, left, top, right, bottom);
    for (int i = 0; i < this.getChildCount(); i++) {
        this.getChildAt(i).layout(left, top, right / 2, top + 200);

this crops my button control instead of shrinking. will layout() change the width of the View or it will just crop it.

Do I need to call measure for all the child in onMeasure().

Thanks in advance.


Robert Williams February 2016

Yes, generally speaking, measure tells views how big to be, and layout tells views where to be. If you override layout without measure, the views will be drawn full size in the wrong position, and will be cropped by the canvas.

Post Status

Asked in February 2016
Viewed 3,902 times
Voted 6
Answered 1 times


Leave an answer

Quote of the day: live life