B4A Library Nice Spinner

It wraps this Github project. It requires android-support-v4.jar (a recent version) to be in your additional library folder. You can download it from here. Posting the B4A project, the B4A library files, and the Java code. If you want to make changes to the Java code and recompile it you must add android-support-v4.jar to the libs folder (libs folder is on the folder level as the src folder - see folder structure once you have extracted the zipped file).

The spinner has a nice animated arrow when the spinner opens/closes. You can set amongst others the following:

1. The background color of the selected text
2. The text color of the selected text
3. The background color of the dropdown list
4. The text color of the text in the dropdown list
5. The divider height (between entries) in the dropdown list
6. The color of the divider between entries in the dropdown list (specify 3 different colors or 3 colors of the same color)

Upon selection an event is raised in the B4A project where you can the extract the index of the selected item (first item in a list = index(0)).

1.gif


1.png


2.png


3.png


Some sample code:

B4X:
#Region  Project Attributes
    #ApplicationLabel: NiceSpinner
    #VersionCode: 1
    #VersionName:
    'SupportedOrientations possible values: unspecified, landscape or portrait.
    #SupportedOrientations: unspecified
    #CanInstallToExternalStorage: False

#End Region

#Region  Activity Attributes
    #FullScreen: False
    #IncludeTitle: True
#End Region

Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.

End Sub

Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.

    Private ns1, ns2 As NiceSpinner
End Sub

Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    Activity.LoadLayout("main")
    ns1.Initialize("ns1")
    ns2.Initialize("ns2")

    Activity.AddView(ns1, 20%x, 5%y, 20%x, 10%y)
    Activity.AddView(ns2, 60%x, 5%y, 20%x, 10%y)
 
    Dim r1 As List
    r1.Initialize
    r1.AddAll(Array As String("One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Eleven", "Twelve"))
    Dim divcolor() As Int = Array As Int(Colors.Blue, Colors.Green, Colors.Red)
    ns1.DividerColor = divcolor
    ns1.DividerHeight = 1
    ns1.DropdownListBackgroundColor = Colors.LightGray
    ns1.attachDataSource(r1)
    ns1.DropdownListTextColor = Colors.DarkGray
    'the next two lines of code must be before ns1.SelectedIndex is set
    ns1.SelectedTextColor = Colors.Green
    ns1.SelectedTextBackgroundColor = Colors.White
    'always set this even if just to 0
    ns1.SelectedIndex = 0


    Dim r2 As List
    r2.Initialize   
    r2.AddAll(Array As String("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"))         
    Dim divcolor() As Int = Array As Int(Colors.White, Colors.White, Colors.White)
    ns2.DividerColor = divcolor
    ns2.DividerHeight = 2
    ns2.DropdownListBackgroundColor = Colors.Black
    ns2.attachDataSource(r2)
    ns2.DropdownListTextColor = Colors.White
    'the next two lines of code must be before ns2.SelectedIndex is set
    ns2.SelectedTextColor = Colors.White
    ns2.SelectedTextBackgroundColor = Colors.DarkGray
    'always set this even if just to 0
    ns2.SelectedIndex = 2

End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Sub ns1_item_clicked

    Log("ns1 item " & ns1.SelectedIndex &" selected")

End Sub

Sub ns2_item_clicked

    Log("ns2 item " & ns2.SelectedIndex &" selected")

End Sub

NiceSpinner
Author:
Johan Schoeman
Version: 1
  • NiceSpinner
    Fields:
    • ba As BA
    Methods:
    • BringToFront
    • DesignerCreateView (base As PanelWrapper, lw As LabelWrapper, props As Map)
    • Initialize (EventName As String)
    • Invalidate
    • Invalidate2 (arg0 As Rect)
    • Invalidate3 (arg0 As Int, arg1 As Int, arg2 As Int, arg3 As Int)
    • IsInitialized As Boolean
    • RemoveView
    • RequestFocus As Boolean
    • SendToBack
    • SetBackgroundImage (arg0 As Bitmap)
    • SetColorAnimated (arg0 As Int, arg1 As Int, arg2 As Int)
    • SetLayout (arg0 As Int, arg1 As Int, arg2 As Int, arg3 As Int)
    • SetLayoutAnimated (arg0 As Int, arg1 As Int, arg2 As Int, arg3 As Int, arg4 As Int)
    • SetTextColorAnimated (arg0 As Int, arg1 As Int)
    • SetTextSizeAnimated (arg0 As Int, arg1 As Float)
    • SetVisibleAnimated (arg0 As Int, arg1 As Boolean)
    • attachDataSource (dataset As List)
    • dismissDropDown
    • showDropDown
    Properties:
    • Background As Drawable
    • Color As Int [write only]
    • DividerColor() As Int [write only]
    • DividerHeight As Int [write only]
    • DropdownListBackgroundColor As Int [write only]
    • DropdownListTextColor As Int [write only]
    • Enabled As Boolean
    • Gravity As Int
    • Height As Int
    • Left As Int
    • SelectedIndex As Int
      Get or Set the default spinner item using its index
    • SelectedTextBackgroundColor As Int [write only]
    • SelectedTextColor As Int [write only]
    • Tag As Object
    • Text As String
    • TextColor As Int
    • TextSize As Float
    • Top As Int
    • Typeface As Typeface
    • Visible As Boolean
    • Width As Int
 

Attachments

  • TheJavaCode.zip
    54.8 KB · Views: 570
  • ba4NiceSpinner.zip
    31.3 KB · Views: 949
  • b4aNiceSpinnerLibFiles.zip
    16.5 KB · Views: 972
Last edited:

ArminKH

Well-Known Member
is this using material design standards?i cant download today :(
any way thank u for sharing
 

Johan Schoeman

Expert
Licensed User
I would suggest that you replace your library files with the attached else you will not get the Black background with the White text in the dropdown list for devices with OS >= Lollipop.

Sorry, but did not test on my device with a later OS and only saw later that the white text on black background was not working for newer OS. Fixed it in the attached library files.
 

Attachments

  • b4aNiceSpinnerLibFilesV2.00.zip
    16.3 KB · Views: 419
  • UpdatedsrcFolder.zip
    33.4 KB · Views: 388

Johan Schoeman

Expert
Licensed User
is this using material design standards?i cant download today :(
any way thank u for sharing
No Armin - I have bypassed it in the code bit had to fix another issue in the library. Have posted V2.00 of the lib in post #4 (above). Will also add the new Java code for those that want to perhaps pursue this issue.
 

Johan Schoeman

Expert
Licensed User
No Armin - I have bypassed it in the code bit had to fix another issue in the library. Have posted V2.00 of the lib in post #4 (above). Will also add the new Java code for those that want to perhaps pursue this issue.
Updated Java code also posted in post#4 above
 

sanjibnanda

Active Member
Licensed User
Dear Johan,

I am getting this error when I compile programme

** Activity (main) Create, isFirst = true **

main_activity_create (B4A line: 32)

ns1.Initialize("ns1")

java.lang.NoSuchMethodError: android.support.v4.content.ContextCompat.getDrawable

at main.java.org.angmarch.views.NiceSpinner.init(NiceSpinner.java:177)

at main.java.org.angmarch.views.NiceSpinner.<init>(NiceSpinner.java:70)

at nicespinnerwrapper.niceSpinnerWrapper._initialize(niceSpinnerWrapper.java:63)
 

Johan Schoeman

Expert
Licensed User
Dear Johan,

I am getting this error when I compile programme

** Activity (main) Create, isFirst = true **

main_activity_create (B4A line: 32)

ns1.Initialize("ns1")

java.lang.NoSuchMethodError: android.support.v4.content.ContextCompat.getDrawable

at main.java.org.angmarch.views.NiceSpinner.init(NiceSpinner.java:177)

at main.java.org.angmarch.views.NiceSpinner.<init>(NiceSpinner.java:70)

at nicespinnerwrapper.niceSpinnerWrapper._initialize(niceSpinnerWrapper.java:63)
Have you downloaded android-support-v4.jar from the link that I posted in post#1? You might be using an older version and therefore the error
 

sanjibnanda

Active Member
Licensed User
Dear Johan,

I am getting this error when I compile programme

** Activity (main) Create, isFirst = true **

main_activity_create (B4A line: 32)

ns1.Initialize("ns1")

java.lang.NoSuchMethodError: android.support.v4.content.ContextCompat.getDrawable

at main.java.org.angmarch.views.NiceSpinner.init(NiceSpinner.java:177)

at main.java.org.angmarch.views.NiceSpinner.<init>(NiceSpinner.java:70)

at nicespinnerwrapper.niceSpinnerWrapper._initialize(niceSpinnerWrapper.java:63)

It was my mistake Thanks updated Jar.
 

Johan Schoeman

Expert
Licensed User
;););)
Dear Johan,

In std. spinner we can store the view like

dim sp as spinner

sp.initilaze(" ")

sp = Panel.GetView(0)


how can we do this in nice spinner

regards
I really don't know. I would suggest you browse the forum and see if there is a solution for a custom view. But what I do know is that you people have got us under some serious pressure in the cricket test after day 1 of the test that started yesterday....;)

Rgds
Johan
 

alimanam3386

Active Member
Licensed User
Hi

when I use it I get bellow exception:

Samsung Note 4 , API 22
B4X:
Extra ---- Service [10:53:50]
** Service (starter) Create **
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
Error occurred on line: 32 (Main)
android.content.res.Resources$NotFoundException: Resource ID #0x0
   at android.content.res.Resources.getValue(Resources.java:2452)
   at android.content.res.Resources.getDimensionPixelSize(Resources.java:1835)
   at main.java.org.angmarch.views.NiceSpinner.init(NiceSpinner.java:125)
   at main.java.org.angmarch.views.NiceSpinner.<init>(NiceSpinner.java:70)
   at nicespinnerwrapper.niceSpinnerWrapper._initialize(niceSpinnerWrapper.java:63)
   at nicespinnerwrapper.niceSpinnerWrapper.Initialize(niceSpinnerWrapper.java:56)
   at java.lang.reflect.Method.invoke(Native Method)
   at java.lang.reflect.Method.invoke(Method.java:372)
   at anywheresoftware.b4a.shell.Shell.runVoidMethod(Shell.java:753)
   at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:343)
   at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:247)
   at java.lang.reflect.Method.invoke(Native Method)
   at java.lang.reflect.Method.invoke(Method.java:372)
   at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:134)
   at ir.manamsoft.sendpush.main.afterFirstLayout(main.java:102)
   at ir.manamsoft.sendpush.main.access$000(main.java:17)
   at ir.manamsoft.sendpush.main$WaitForLayout.run(main.java:80)
   at android.os.Handler.handleCallback(Handler.java:739)
   at android.os.Handler.dispatchMessage(Handler.java:95)
   at android.os.Looper.loop(Looper.java:145)
   at android.app.ActivityThread.main(ActivityThread.java:6872)
   at java.lang.reflect.Method.invoke(Native Method)
   at java.lang.reflect.Method.invoke(Method.java:372)
   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1404)
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1199)
java.lang.Exception:  android.content.res.Resources$NotFoundException: Resource ID #0x0
 

Johan Schoeman

Expert
Licensed User
Hi

when I use it I get bellow exception:

Samsung Note 4 , API 22
B4X:
Extra ---- Service [10:53:50]
** Service (starter) Create **
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
Error occurred on line: 32 (Main)
android.content.res.Resources$NotFoundException: Resource ID #0x0
   at android.content.res.Resources.getValue(Resources.java:2452)
   at android.content.res.Resources.getDimensionPixelSize(Resources.java:1835)
   at main.java.org.angmarch.views.NiceSpinner.init(NiceSpinner.java:125)
   at main.java.org.angmarch.views.NiceSpinner.<init>(NiceSpinner.java:70)
   at nicespinnerwrapper.niceSpinnerWrapper._initialize(niceSpinnerWrapper.java:63)
   at nicespinnerwrapper.niceSpinnerWrapper.Initialize(niceSpinnerWrapper.java:56)
   at java.lang.reflect.Method.invoke(Native Method)
   at java.lang.reflect.Method.invoke(Method.java:372)
   at anywheresoftware.b4a.shell.Shell.runVoidMethod(Shell.java:753)
   at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:343)
   at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:247)
   at java.lang.reflect.Method.invoke(Native Method)
   at java.lang.reflect.Method.invoke(Method.java:372)
   at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:134)
   at ir.manamsoft.sendpush.main.afterFirstLayout(main.java:102)
   at ir.manamsoft.sendpush.main.access$000(main.java:17)
   at ir.manamsoft.sendpush.main$WaitForLayout.run(main.java:80)
   at android.os.Handler.handleCallback(Handler.java:739)
   at android.os.Handler.dispatchMessage(Handler.java:95)
   at android.os.Looper.loop(Looper.java:145)
   at android.app.ActivityThread.main(ActivityThread.java:6872)
   at java.lang.reflect.Method.invoke(Native Method)
   at java.lang.reflect.Method.invoke(Method.java:372)
   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1404)
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1199)
java.lang.Exception:  android.content.res.Resources$NotFoundException: Resource ID #0x0
There are a number of folders and files in the B4A project's /Objects/res folder. Do you have them in your project and have all the files in these folders been set to READ ONLY?
 

jruiz1998

Member
Licensed User
Very nice Library, very useful.
Just one question.
I did not find any difference using "disabled True or False".
My application needs to block the ComboBox under some circumstances avoiding to change the first item selected.
I used disbled = False and did not work as I needed.
Any help would be welcome.

Thanks in advance.
JRuiz
 
Top