There are two problems here. The first is that you are calculating the RMS from a single sample, as you aren't calculating the sum across the whole buffer - you're resetting it every sample. So sum
contains the square-magnitude for one sample, but then you're dividing by the sample length.
The second problem is that you won't easily be able to create a meter to measure loudness. The decibel value you calculate will only be a power ratio where the maximum value of 0dB indicates a peak. It doesn't have any correlation with physical sound pressure, which is generally what people mean when they say 'loudness' (dB(SPL) is the scale where 50dB is the loudness of speech, 110dB a rock concert, etc).
See also: sound meter android