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: 967
  • ba4NiceSpinner.zip
    31.3 KB · Views: 1,429
  • b4aNiceSpinnerLibFiles.zip
    16.5 KB · Views: 1,475
Last edited:

DonManfred

Expert
Licensed User
Longtime User
These files need to set to READ ONLY else they will be deleted the first time that you compile the B4A app.
You can create a new Resourcefolder and reference it with #additionalres: path
In this case you dont need to put them in objects res and you dont need to take care of setting READONLY on all files in the Resourcefolder.
 

Csaba Balogh

Member
Licensed User
Hi all, i have follow the step to downloaad "android-support-v4.jar" and paste in B4A library folder.
Below is my error.

B4X:
Error occurred on line: 42 (frmItem_Maintenance)
java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
    at anywheresoftware.b4a.keywords.LayoutBuilder.loadLayout(LayoutBuilder.java:166)
    at anywheresoftware.b4a.objects.PanelWrapper.LoadLayout(PanelWrapper.java:134)
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:710)
    at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:342)
    at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:249)
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:139)
    at b4a.example.frmitem_maintenance.afterFirstLayout(frmitem_maintenance.java:102)
    at b4a.example.frmitem_maintenance.access$000(frmitem_maintenance.java:17)
    at b4a.example.frmitem_maintenance$WaitForLayout.run(frmitem_maintenance.java:80)
    at android.os.Handler.handleCallback(Handler.java:743)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:150)
    at android.app.ActivityThread.main(ActivityThread.java:5621)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:794)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:684)
Caused by: java.lang.reflect.InvocationTargetException
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.objects.CustomViewWrapper.AfterDesignerScript(CustomViewWrapper.java:64)
    at anywheresoftware.b4a.keywords.LayoutBuilder.loadLayout(LayoutBuilder.java:158)
    ... 17 more
Caused by: android.content.res.Resources$NotFoundException: Resource ID #0x0
    at android.content.res.Resources.getValue(Resources.java:1384)
    at android.content.res.Resources.getDimensionPixelSize(Resources.java:725)
    at main.java.org.angmarch.views.NiceSpinner.init(NiceSpinner.java:127)
    at main.java.org.angmarch.views.NiceSpinner.<init>(NiceSpinner.java:72)
    at nicespinnerwrapper.niceSpinnerWrapper._initialize(niceSpinnerWrapper.java:105)
    ... 20 more
** Activity (frmitem_maintenance) Resume **
 

Csaba Balogh

Member
Licensed User
Hi there,

I know it is my mistake that I cannot figure out what the problem is but also have the same error that tylorrw had some time ago. I did all what was suggested, I changed the attributes of files in drawable dir, but still it returns with the error code. What am I doing wrong? Do I miss somthing?
The err.code:
B4X:
Logger connected to:  Sony D5503
--------- beginning of main
*** 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:1307)
    at android.content.res.Resources.getDimensionPixelSize(Resources.java:714)
    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 JHS.NiceSpinner.main._activity_create(main.java:390)
    at java.lang.reflect.Method.invoke(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:372)
    at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:732)
    at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:351)
    at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:255)
    at java.lang.reflect.Method.invoke(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:372)
    at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:144)
    at JHS.NiceSpinner.main.afterFirstLayout(main.java:104)
    at JHS.NiceSpinner.main.access$000(main.java:17)
    at JHS.NiceSpinner.main$WaitForLayout.run(main.java:82)
    at android.os.Handler.handleCallback(Handler.java:739)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:211)
    at android.app.ActivityThread.main(ActivityThread.java:5389)
    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:1020)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:815)
** Activity (main) Resume **
 

Johan Schoeman

Expert
Licensed User
Longtime User
Hi there,

I know it is my mistake that I cannot figure out what the problem is but also have the same error that tylorrw had some time ago. I did all what was suggested, I changed the attributes of files in drawable dir, but still it returns with the error code. What am I doing wrong? Do I miss somthing?
The err.code:
B4X:
Logger connected to:  Sony D5503
--------- beginning of main
*** 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:1307)
    at android.content.res.Resources.getDimensionPixelSize(Resources.java:714)
    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 JHS.NiceSpinner.main._activity_create(main.java:390)
    at java.lang.reflect.Method.invoke(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:372)
    at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:732)
    at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:351)
    at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:255)
    at java.lang.reflect.Method.invoke(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:372)
    at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:144)
    at JHS.NiceSpinner.main.afterFirstLayout(main.java:104)
    at JHS.NiceSpinner.main.access$000(main.java:17)
    at JHS.NiceSpinner.main$WaitForLayout.run(main.java:82)
    at android.os.Handler.handleCallback(Handler.java:739)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:211)
    at android.app.ActivityThread.main(ActivityThread.java:5389)
    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:1020)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:815)
** Activity (main) Resume **
See post #18 of this thread.

Rgds
JS
 

BarryW

Active Member
Licensed User
Longtime User
B4X:
Java Version: 8
Parsing code.    (0.00s)
Building folders structure.    (0.01s)
Compiling code.    (0.00s)
Compiling layouts code.    (0.00s)
Organizing libraries.    (0.00s)
    (AndroidX SDK)
Generating R file.    (0.00s)
Compiling generated Java code.    Error
B4A line: 70
spn_search_category.DropdownListTextSize = 20
javac 1.8.0_211
src\com\trabaho\p14\main.java:426: error: cannot find symbol
mostCurrent._spn_search_category.setDropdownListTextSize((int) (20));
                                ^
  symbol:   method setDropdownListTextSize(int)
  location: variable _spn_search_category of type niceSpinnerWrapper
 

AlfaizDev

Well-Known Member
Licensed User
do not work
B4X:
Error occurred on line: 47 (M_Asnaf)
java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
    at anywheresoftware.b4a.keywords.LayoutBuilder.loadLayout(LayoutBuilder.java:170)
    at anywheresoftware.b4a.objects.ActivityWrapper.LoadLayout(ActivityWrapper.java:209)
    at com.Alfaiz.AlmuhraRestaurantsAdmin.m_asnaf._activity_create(m_asnaf.java:395)
    at java.lang.reflect.Method.invoke(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:372)
    at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:732)
    at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:351)
    at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:255)
    at java.lang.reflect.Method.invoke(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:372)
    at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:144)
    at com.Alfaiz.AlmuhraRestaurantsAdmin.m_asnaf.afterFirstLayout(m_asnaf.java:104)
    at com.Alfaiz.AlmuhraRestaurantsAdmin.m_asnaf.access$000(m_asnaf.java:17)
    at com.Alfaiz.AlmuhraRestaurantsAdmin.m_asnaf$WaitForLayout.run(m_asnaf.java:82)
    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:6918)
    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)
Caused by: java.lang.reflect.InvocationTargetException
    at java.lang.reflect.Method.invoke(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:372)
    at anywheresoftware.b4a.objects.CustomViewWrapper.AfterDesignerScript(CustomViewWrapper.java:64)
    at anywheresoftware.b4a.keywords.LayoutBuilder.loadLayout(LayoutBuilder.java:162)
    ... 21 more
Caused by: android.content.res.Resources$NotFoundException: Resource ID #0x0
    at android.content.res.Resources.getValue(Resources.java:2480)
    at android.content.res.Resources.getDimensionPixelSize(Resources.java:1863)
    at main.java.org.angmarch.views.NiceSpinner.init(NiceSpinner.java:127)
    at main.java.org.angmarch.views.NiceSpinner.<init>(NiceSpinner.java:72)
    at nicespinnerwrapper.niceSpinnerWrapper._initialize(niceSpinnerWrapper.java:105)
    ... 25 more
** Activity (m_asnaf) Resume **
 

Johan Schoeman

Expert
Licensed User
Longtime User
do not work
B4X:
Error occurred on line: 47 (M_Asnaf)
java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
    at anywheresoftware.b4a.keywords.LayoutBuilder.loadLayout(LayoutBuilder.java:170)
    at anywheresoftware.b4a.objects.ActivityWrapper.LoadLayout(ActivityWrapper.java:209)
    at com.Alfaiz.AlmuhraRestaurantsAdmin.m_asnaf._activity_create(m_asnaf.java:395)
    at java.lang.reflect.Method.invoke(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:372)
    at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:732)
    at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:351)
    at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:255)
    at java.lang.reflect.Method.invoke(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:372)
    at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:144)
    at com.Alfaiz.AlmuhraRestaurantsAdmin.m_asnaf.afterFirstLayout(m_asnaf.java:104)
    at com.Alfaiz.AlmuhraRestaurantsAdmin.m_asnaf.access$000(m_asnaf.java:17)
    at com.Alfaiz.AlmuhraRestaurantsAdmin.m_asnaf$WaitForLayout.run(m_asnaf.java:82)
    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:6918)
    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)
Caused by: java.lang.reflect.InvocationTargetException
    at java.lang.reflect.Method.invoke(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:372)
    at anywheresoftware.b4a.objects.CustomViewWrapper.AfterDesignerScript(CustomViewWrapper.java:64)
    at anywheresoftware.b4a.keywords.LayoutBuilder.loadLayout(LayoutBuilder.java:162)
    ... 21 more
Caused by: android.content.res.Resources$NotFoundException: Resource ID #0x0
    at android.content.res.Resources.getValue(Resources.java:2480)
    at android.content.res.Resources.getDimensionPixelSize(Resources.java:1863)
    at main.java.org.angmarch.views.NiceSpinner.init(NiceSpinner.java:127)
    at main.java.org.angmarch.views.NiceSpinner.<init>(NiceSpinner.java:72)
    at nicespinnerwrapper.niceSpinnerWrapper._initialize(niceSpinnerWrapper.java:105)
    ... 25 more
** Activity (m_asnaf) Resume **
See post #18 of this thread. Sure you will figure it out.;)
 

incendio

Well-Known Member
Licensed User
Longtime User
Hi,

I know this is old, but this spinner is exactly what I need, but it didn't work as expected.

This code didn't work
B4X:
ns2.DropdownListBackgroundColor = Colors.Black

No matter what color set for dropdown background, it always set to white, see attachment.

Runs on B4A 11.8, tested on Android 10 (real device) and Android 11 (emulator)

Is there a solution for this?
 

Attachments

  • ss.png
    ss.png
    24 KB · Views: 127

Johan Schoeman

Expert
Licensed User
Longtime User
Hi,

I know this is old, but this spinner is exactly what I need, but it didn't work as expected.

This code didn't work
B4X:
ns2.DropdownListBackgroundColor = Colors.Black

No matter what color set for dropdown background, it always set to white, see attachment.

Runs on B4A 11.8, tested on Android 10 (real device) and Android 11 (emulator)

Is there a solution for this?
What does the attached project look like on your device?
 

Attachments

  • b4aNiceSpinner27042023.zip
    32.4 KB · Views: 157

incendio

Well-Known Member
Licensed User
Longtime User
What does the attached project look like on your device?
See the attached pictures.

Also, these codes raised an error : Unknown member
B4X:
ns1.DropdownListTypeface = l1.Typeface

ns2.DropdownListTextSize = 15
ns2.SelectedItemTextSize = 10
Is there a new version of this class? Mine is ver 1.00
 

Attachments

  • SS1.png
    SS1.png
    18.5 KB · Views: 140
  • SS2.png
    SS2.png
    15 KB · Views: 134

GMan

Well-Known Member
Licensed User
Longtime User
On my Android 13 it Breaks on starting and then Shows nothing
Screenshot_20230428_124733.jpg


Screenshot_20230428_124750.jpg
 
Top