Android Code Snippet ScrollView properties

klaus

Expert
Licensed User
Subject: Routines to managing ScrollView properties.

Sub: setVerticalScrollBarEnabled
Description: Displays or hides the vertical ScrollBar of the ScrollView.
B4X:
'displays or hides the VerticalScrollBar of the ScrollView
Sub setVerticalScrollBarEnabled(v As View, Enabled As Boolean)
    Dim jo = v As JavaObject
    jo.RunMethod("setVerticalScrollBarEnabled", Array As Object (Enabled))
End Sub
Tags: setVerticalScrollBarEnabled,

Sub: setVerticalFadingEdgeEnabled
Description: Enables the vertical fading edges (shadows at top and bottom) of the ScrollView.
B4X:
'enables the VerticalFadingEdge of the ScrollView
'the VerticalFadingEdges are the 'shadows' on top and bottom of the ScrollView
Sub setVerticalFadingEdgeEnabled(v As View, Enabled As Boolean)
   Dim jo = v As JavaObject
   jo.RunMethod("setVerticalFadingEdgeEnabled", Array As Object (Enabled))
End Sub
Tags: setVerticalFadingEdgeEnabled

Sub: getVerticalScrollbarPosition
Description: Gets the vertical Scrollbar position of the ScrollView.
B4X:
'Gets the vertical Scrollbar position of the ScrollView
'    SCROLLBAR_POSITION_DEFAULT    = 0  position defined by the operating system
'    SCROLLBAR_POSITION_LEFT        = 1
'    SCROLLBAR_POSITION_RIGHT        = 2
Sub getVerticalScrollbarPosition(v As View) As Int
    Dim jo = v As JavaObject
    Return jo.RunMethod("getVerticalScrollbarPosition", Null)
End Sub
Tags: getVerticalScrollbarPosition, setVerticalScrollbarPosition

Sub: setVerticalScrollbarPosition
Description: Sets the vertical Scrollbar position of the ScrollView.
B4X:
'Sets the vertical Scrollbar position of the ScrollView
'    SCROLLBAR_POSITION_DEFAULT    = 0  position defined by the operating system
'    SCROLLBAR_POSITION_LEFT        = 1
'    SCROLLBAR_POSITION_RIGHT        = 2
Sub setVerticalScrollbarPosition(v As View, Position As Int)
    Dim jo = v As JavaObject
    jo.RunMethod("setVerticalScrollbarPosition", Array As Object (Position))
End Sub
Sub: getScrollBarStyle
Description: Gets the ScrollBar style.
B4X:
'Gets the ScrollBar style
'    SCROLLBARS_INSIDE_OVERLAY    = 0  (0x00000000)
'    SCROLLBARS_INSIDE_INSET          = 16777216 (0x01000000)
'    SCROLLBARS_OUTSIDE_INSET        = 50331648 (0x03000000)
'    SCROLLBARS_OUTSIDE_OVERLAY  = 33554432 (0x02000000)
Sub getScrollBarStyle(v As View) As Int
    Dim jo = v As JavaObject
    Return jo.RunMethod("getScrollBarStyle", Null)
End Sub
Tags: getScrollBarStyle, setScrollBarStyle

Sub: setScrollBarStyle
Description: Sets the ScrollBar style.
B4X:
'Sets the ScrollBar style
'    SCROLLBARS_INSIDE_OVERLAY    = 0 (0x00000000)
'    SCROLLBARS_INSIDE_INSET          = 16777216 (0x01000000)
'    SCROLLBARS_OUTSIDE_INSET        = 50331648 (0x03000000)
'    SCROLLBARS_OUTSIDE_OVERLAY  = 33554432 (0x02000000)
Sub setScrollBarStyle(v As View, Style As Int)
    Dim jo = v As JavaObject
    jo.RunMethod("setScrollBarStyle", Array As Object (Style))
End Sub
Sub: getOverScrollMode
Description: Gets the OverScrollMode mode of the ScrollView, a small movement when reaching the top or the bottom during scrolling.
B4X:
'gets the OverScrollMode mode of the ScrollView
'    OVER_SCROLL_ALWAYS                        = 0
'    OVER_SCROLL_IF_CONTENT_SCROLLS = 1
'    OVER_SCROLL_NEVER                            = 2
Sub getOverScrollMode(v As View) As Int
    Dim jo = v As JavaObject
    Return jo.RunMethod("getOverScrollMode", Null)
End Sub
Tags: getOverScrollMode, setOverScrollMode

Sub: setOverScrollMode
Description: Sets the OverScrollMode mode of the ScrollView, a small movement when reaching the top or the bottom during scrolling.
B4X:
'sets the OverScrollMode mode of the ScrollView
'    OVER_SCROLL_ALWAYS                        = 0
'    OVER_SCROLL_IF_CONTENT_SCROLLS = 1
'    OVER_SCROLL_NEVER                            = 2
Sub setOverScrollMode(v As View, Mode As Int)
    Dim jo = v As JavaObject
    jo.RunMethod("setOverScrollMode", Array As Object (Mode))
End Sub
Sub: getScrollBarDefaultDelayBeforeFade
Description: Gets the ScrollBarDefaultDelayBeforeFade of the ScrollView in milliseconds.
The DelayBeforeFade is the time before the ScrollBar disappears.
B4X:
'sets the ScrollBarDefaultDelayBeforeFade of the ScrollView in milliseconds
'DelayBeforeFade is the time before the ScrollBar disappears
Sub getScrollBarDefaultDelayBeforeFade(v As View) As Int
    Dim jo = v As JavaObject
    Return jo.RunMethod("getScrollBarDefaultDelayBeforeFade", Null)
End Sub
Tags: getScrollBarDefaultDelayBeforeFade, setScrollBarDefaultDelayBeforeFade

Sub: setScrollBarDefaultDelayBeforeFade
Description: Sets the ScrollBarDefaultDelayBeforeFade of the ScrollView in milliseconds.
The DelayBeforeFade is the time before the ScrollBar disappears.
B4X:
'sets the ScrollBarDefaultDelayBeforeFade of the ScrollView in milliseconds
'DelayBeforeFade is the time before the ScrollBar disappears
Sub setScrollBarDefaultDelayBeforeFade(v As View, Duration As Int)
    Dim jo = v As JavaObject
    jo.RunMethod("setScrollBarDefaultDelayBeforeFade", Array As Object (Duration))
End Sub
Sub: getScrollBarFadeDuration
Description: Gets the ScrollBarFadeDuration of the ScrollView in milliseconds.
The ScrollBarFadeDuration is the fading duration (duration of the slow disappearing).
B4X:
'gets the ScrollBarFadeDuration of the ScrollView in milliseconds
'ScrollBarFadeDuration is the fading duration (slowly disappearing)
Sub getScrollBarFadeDuration(v As View) As Int
    Dim jo = v As JavaObject
    Return jo.RunMethod("getScrollBarFadeDuration", Null)
End Sub
Tags: getScrollBarFadeDuration, setScrollBarFadeDuration

Sub: setScrollBarFadeDuration
Description: Sets the ScrollBarFadeDuration of the ScrollView in milliseconds.
The ScrollBarFadeDuration is the fading duration (duration of the slow disappearing).
B4X:
'sets the ScrollBarFadeDuration of the ScrollView in milliseconds
'ScrollBarFadeDuration is the fading duration (slowly disappearing)
Sub setScrollBarFadeDuration(v As View, FadeDuration As Int)
    Dim jo = v As JavaObject
    jo.RunMethod("setScrollBarFadeDuration", Array As Object (FadeDuration))
End Sub
Sub: getScrollBarSize
Description: Gets the ScrollBar size in pixels, unfortunately this seems not working, no effect on my devices.
B4X:
'Gets the ScrollBar size in pixels
'    unfortunately this seem not working, no effect on my devices
Sub getScrollBarSize(v As View) As Int
    Dim jo = v As JavaObject
    jo.RunMethod("getScrollBarSize", Null)
End Sub
Tags: getScrollBarSize, setScrollBarSize

Sub: setScrollBarSize
Description: Sets the ScrollBar size in pixels, unfortunately this seem not working, no effect on my devices.
B4X:
'Sets the ScrollBar size in pixels
'    unfortunately this seem not working, no effect on my devices
Sub setScrollBarSize(v As View, Size As Int)
    Dim jo = v As JavaObject
    jo.RunMethod("setScrollBarSize", Array As Object (Size))
End Sub
Sub: getVerticalFadingEdgeLength
Description: Gets the length, in pixels, of the fading edge (the shadows on top and bottom of the ScrollView).
B4X:
'Gets the length, in pixels, of the fading edge (the shadows on top and bottom of the ScrollView
Sub getVerticalFadingEdgeLength(v As View) As Int
    Dim jo = v As JavaObject
    Return jo.RunMethod("getVerticalFadingEdgeLength", Null)
End Sub
Tags: getVerticalFadingEdgeLength

Dependencies: JavaObject library.
Documentation: http://developer.android.com/reference/android/view/View.html

Tags: ScrollView, ScrollBar, setVerticalScrollBarEnabled,

The routines above are desiged as independant routines.
If you want to set several propeties at the same time you could do it in a 'simpler' way directly in the code without calling each routine separately :
B4X:
Dim jo = scvText As JavaObject
jo.RunMethod("setVerticalScrollbarPosition", Array As Object (1))   ' position left
jo.RunMethod("setVerticalFadingEdgeEnabled", Array As Object (True))
jo.RunMethod("setScrollBarDefaultDelayBeforeFade", Array As Object (2000))
jo.RunMethod("setScrollBarFadeDuration", Array As Object (2000))

Attached a small test project (written with B4A version 3.80).
 

Attachments

Last edited:

DavideV

Active Member
Licensed User
Hi Klaus,
thank you for this code snippet :)
i'm using some of these properties that works for my need.
For my new app i need to disable the scroll by touch since i'm scrolling the view by code and i don't want the user to scroll.
Is there a way to do this?
I tried the .disable properties but does nothing...

thx in advance

DavideV
 

klaus

Expert
Licensed User
You can use this:
In Activity_Create add:
B4X:
Dim obj As Reflector
obj.Target = ScrollViewName 'the name of your ScrollView
obj.SetOnTouchListener("scvTouch")
And add this routine, which will consume the Touch event.
B4X:
Sub scvTouch(viewtag As Object, action As Int, X As Float, Y As Float, motionevent As Object) As Boolean
    Return True
End Sub
 

dibesw

Active Member
Licensed User
I have an error in run:
java.lang.RuntimeException: Method: getOverScrollMode not found in: anywheresoftware.b4a.objects.ScrollViewWrapper$MyScrollView
I have 3.50 b4a version
 

Cableguy

Expert
Licensed User
Sub: setVerticalFadingEdgeEnabledDescription: Enables the vertical fadin edges (shadows at top and bottom) of the ScrollView.
Code:
'enables the VerticalFadingEdge of the SvrollView
'the VerticalFadingEdges are the 'shadows' on top and bottom of the ScrollViewSub setVerticalFadingEdgeEnabled(v AsView, Enabled As Boolean)Dim jo = v AsJavaObject
jo.RunMethod("setVerticalFadingEdgeEnabled", ArrayAs Object (Enabled))End Sub
Tags: setVerticalFadingEdgeEnabled
Hi Klaus, I'm having trouble setting this. This is not working as it should, the shadows still appear when I scroll over the inner panel limits
 
Top