You seem to have misunderstood the way that Android layouts work. Basically views get assigned a size by their parents. onMeasure allows the view to influence the size, but must follow the method contract.
In particular your onMeasure does not take into account the measure mode (both horizontal and vertical). You need to provide a measurement for all nine possible states:
horiz exactly, vertical exactly
: set the size to what was passed in (should be last invocation)
horiz exactly, vertical at most
: set the size horizontally to the given value, vertically set the value
that is at most the given value (stretching with aspect ratio may make
this smaller)
horiz exactly, vertical unspecified
: set the horizontal size to the one given, set the vertical size to the
best corresponding size (for example maintaining aspect ratio)
horiz at most, vertical exactly
: set the vertical size to the given size. Set the horizontal size to an
appropriate size that is at most the given horizontal size.
horiz at most, vertical at most
: Find which dimension bounds the image according to the aspect ratio.
Treat that dimension as if it was given exactly (but if the image is
smaller than the bounds you may also use that instead)
horiz at most, vertical unspecified
: If the horizontal size is smaller than the image treat it as if that
size was given for the exact horizontal dimension, otherwise treat it as
if the horizontal constraint was exact.
horiz unspecified, vertical exact
: Like horiz exact, vertical unspecified, but horiz and vertical swapped
horiz unspecified, vertical at most
: Like horiz at most, vertical unspecified, but horiz and vertical swapped
horiz unspecified, vertical unspecified
: The intrinsic size of the image or some default if the image is not
given (the default uses 100,100), using 0,0 makes the layout editor
awkward.
Btw. Don't catch Exception, that is poor practice.