androidandroid-layoutandroid-scrollviewandroid-scrolling

Customized View with Horizontal Scroll


I am trying to write a custom View with the ability of scroll horizontally. I have gone through many posts on the forums but I am still unable to get what I want. I want a custom view on which I am plotting the pitch contour of an audio recording. The pitch contour is quite long to fit in the screen. So, I want to add a horizontal scroll to this. As of now, I have created a customized view extending HorizontalScrollView. Once the window has the focus, I invalidate() the view and draw the pitch contour on the screen. After I complete drawing, I try to jump to the right most position by calling scrollTo(getRight(), 0). This doesn't change anything and the contour is exactly the same position. When I try to print the value using getRight(), it gives me the value 1794. I am using a Nexux 5 for testing. Can anyone give me any hints on this?

PS: The x pixel value for the last value in the contour is Last value of x for is:5499.3.


Solution

  • The mistake I was doing was not to provide an implementation for onMeasure() inside my custom View. If the custom view has the layout_width mentioned as wrap_content (in case of a HorizontalScrollView), we need to set the width of the custom view inside onMeasure(). A good explanation of the same is here. Hope this is helpful to other people.