B4A Class SLSPinner - Android 4 style spinner for all API's

This is a plugin replacement for the standard spinner styled as the spinner in Android 4. I redesigned an app using the Android 4 spinner and then hated it running on earlier versions with the old style spinner, so I created this.

I've added several additional functions to the normal Spinner. You can change more of the appearance, List Border color, List Border width, List width, List height (within certain constraints), Item Heights etc.

I've also added an option to allow a touch on the button when the list is displayed, to return the button value as if it were selected from the list - ButtonTouchSelect, something I needed used with manual ordering of a list.

The background icon is drawn in code, so you can change it add a different graphic or change the colors as you please, or you can just use the defaults.

The Pressed and Focused colors in the state list drawable are taken from the Hololight Theme if it's available, if not the defaults are used. If for some reason neither are available, the Class will create it's own.

This is a custom view class, which you can also add manually to your code. The example shows both and includes a standard spinner for comparison.

I've tested it on various devices and API levels, please let me know if you find any problems.

The code is included so you can poke about and change anything you don't like. If you make improvements, please repost.

This is new software, and is quite complex in places. Please test thoroughly in any apps you plan to use it in.

SLSpinner
Author:
Steve Laming
Version: 0.9

  • Methods:
    • Add(Item As String) As String
      Add an item to the spinner

      AddAll(List As List) As String
      Add all items from a List of Strings to the spinner

      Background As Object
      Get or Set the background for the visible Button. Can be one of: BitmapDrawable, ColorDrawable, GradientDrawable or StateListDrawable
      Returns a drawable of the type that was initially or subsequently set


      BringToFront As String
      Changes the Z order of this view and brings it to the front.

      ButtonTouchSelect As boolean
      Gets or sets whether a touch on the button passes the currently selected item as a selection
      Default is False


      Clear As String
      Clear all items from the Spinner

      Color(Color As int) As String - [Write Only]
      Sets the background of the Button to be a ColorDrawable with the given color.

      DropDownTextColor As int
      Get or set the DropDownTextColor

      Enabled As boolean
      Enable / Disable the spinner

      GetItem(Index As int) As String
      Get Item at index from the list

      Gravity As int
      Gets or sets the Gravity for the displayed Button and drop down List

      Height As int
      'Gets or sets the height of the displayed View

      IndexOf(Value As String) As int
      Returns the index of Value in the Spinner or -1 if not found

      Initialize(TargetModule As Object, EventName As String) As String
      Initializes the object.
      Required for both designer and code setup.
      Provides callback {EventName}_ItemClick(Position As Int,Value As Object)


      Invalidate As String
      Invalidates the view, forcing it to redraw itself.

      IsInitialized As boolean
      Returns True if the View has been initialized. For a manually added Spinner, setup must have also been called

      ItemHeight As int
      Gets or sets the ItemHeight. Default height is 48Dip

      Left As int
      Gets or sets the view's left position.

      ListBorderColor(Color As int) As String - [Write Only]
      Sets the List Border Color . Default = Colors.ARGB(120,35,35,35) and gives a shadow effect

      ListBorderSize(Size As int) As String - [Write Only]
      Set the List Border Size. Default = 5Dip Set to '0' to hide the border

      ListColor(Color As int) As String - [Write Only]
      Sets the background of the List to be a ColorDrawable with the given color.

      ListHeight As int
      Gets or sets the lists height excluding any border
      Seting this will be ignored if it would make the height of the list go off the screen, so that the list can scroll fully.


      ListWidth As int
      Gets or sets the lists width excluding any border

      RemoveAt(Index As int) As String
      Removes the item at the specified index.

      RemoveView As String
      Removes this view from its parent.

      RequestFocus As boolean
      Tries To set the focus To this View.
      Returns True If the focus was set.


      SelectedIndex As int
      Gets or sets the index of the selected item. Returns -1 if no item is selected.

      SelectedItem As String - [Read Only]
      Returns the value of the selected item. Returns an empty string if no item is selected.

      SendToBack As String
      Changes the Z order of this view and sends it to the back.

      SetBackgroundImage(Image As Bitmap) As String
      Set the Spinner Buttons background image

      SetLayout(Left As int, Top As int, Width As int, Height As int) As String
      Changes the view position and size.

      Setup(Act As Activity, Pnl As Panel, Left As int, Top As int, Width As int, Height As int) As String
      Use when setting up the CustomView manually. Pass an initialized panel to add the Spinner to or Null to add to the Activity

      Size As int - [Read Only]
      Returns the number of items in the list.

      Tag As Object
      Gets or sets the Tag value. This is a place holder which can used to store additional data.

      TextColor As int
      Gets or sets the Buttons text color.

      TextSize As float
      Get or sets the text size. Default for a customview is 14, default for a Spinner is 16

      ToggleList As String
      Show / Hide the drop down list

      Top As int
      Gets or sets the view's top position.

      TypeFace As Typeface
      Get or set the Typeface used by the spinner

      Visible As boolean
      Gets or sets the spinners Visibility

      Width As int
      Gets or sets the view's width. If you have set listwidth, setting this will only effect the Button width

      Depends on:
      javaobject, reflection

Version 0.9 attached with example. You can compile the module to a library if you prefer.

Added pre3.2 for those without the latest javaobject
 

Attachments

  • SLSPinner.zip
    16.8 KB · Views: 856
  • SLSPinnerPre3.2.zip
    15.4 KB · Views: 685
Last edited:

GMan

Well-Known Member
Licensed User
Longtime User
Hoi,
does i need extra libs or special IDE versions for that ?
I use 2.7 and deleted the bal.file, but in the module an error appear with 3 subs

B4X:
Private Sub GetRelativeTop(V As JavaObject) As Int

at (V As JavaObject)
 
Last edited:

stevel05

Expert
Licensed User
Longtime User
You need the javaobject library, I'm not at my PC but just search the forum for it.
 

GMan

Well-Known Member
Licensed User
Longtime User
thx , will get it
 

LucaMs

Expert
Licensed User
Longtime User
(I state that I have 3:00)

This line gives error:
B4X:
 id = JO.RunMethodJO("getResources",Null).RunMethodJO("getSystem",Null).RunMethod("getIdentifier",Array As Object(ImageName,"drawable","android"))


Maybe it should be:
B4X:
'      id = JO.RunMethodJO("getResources",Null).RunMethodJO("getSystem",Null).RunMethod("getIdentifier",Array As Object(ImageName,"drawable","android"))
        JO = JO.RunMethod("getSystem",Null)
        JO = JO.RunMethod("getResources",Null)
        id = JO.RunMethod("getIdentifier",Array As Object(ImageName,"drawable","android"))

?

I don't know, but i remember a "structure" like that for the target of reflector

Not, it is wrong too.
 
Last edited:

LucaMs

Expert
Licensed User
Longtime User
Maybe so (?):

B4X:
        r.Target = r.GetContext
        r.Target = r.RunMethod("getResources")
        r.Target = r.RunMethod("getSystem")
       
        id = r.RunMethod4("getIdentifier", Array As Object(ImageName, "drawable", "android"), _
                                        Array As String("java.lang.String", "java.lang.String", "java.lang.String"))
 

stevel05

Expert
Licensed User
Longtime User
They work from left to right so if you don't have 3.2 it would be:

B4X:
'      id = JO.RunMethodJO("getResources",Null).RunMethodJO("getSystem",Null).RunMethod("getIdentifier",Array As Object(ImageName,"drawable","android"))
        JO = JO.RunMethod("getResources",Null)
        JO = JO.RunMethod("getSystem",Null)
        id = JO.RunMethod("getIdentifier",Array As Object(ImageName,"drawable","android"))
 

stevel05

Expert
Licensed User
Longtime User
Added version pre3.2 for earlier versions, but must support JavaObject. Sorry, I don't know the earliest version that will support it.
 

Theera

Well-Known Member
Licensed User
Longtime User
where is post#1? I cann't find.
 

GMan

Well-Known Member
Licensed User
Longtime User
The "normal" version should work but the problem is the .bal File which was made with a newer version.
i have to delete and recreate it, but dont set (as it seems) all views in it or didnt configure them as needed.
 

stevel05

Expert
Licensed User
Longtime User
Updated Pre3.2 version created on B4a 3 without the new designer. The example should now work as is for pre 3.2 versions.
 

GMan

Well-Known Member
Licensed User
Longtime User
Should, but did not - otherwise i would not write :rolleyes:
 

stevel05

Expert
Licensed User
Longtime User
Download it again, I updated it for post #13
 

junglejet

Active Member
Licensed User
Longtime User
First thanks for all the efforts to let the community participate in the code.
Two issues arise on all my devices (2.3, 4.1.2 and 4.4.2):

1) When the list opens from a spinner at the very bottom of the page it is displayed southwards and partially south of the bottom border and items cannot be selected there. I cannot find where to adjust it in a way that it opens northwards

2) If have a spinner with only two items. When I have selected one item then the next time I open the list it is kind of blocked. I must wipe in some way (don't know exactly what and why and how) so that a black overflaid marking hightlighting both items goes away. Then I can select an item again. It appears all items are selected on the second opening and they must be deselected first.

Other than that it looks nice and works good

-- Andy
 

stevel05

Expert
Licensed User
Longtime User
Hi Andy,

Thanks for your feedback.

The orientation of the spinner is determined automatically if the whole list will fit below the button the it is displayed there regardless of where the button is, otherwise it is dependent on it's absolute position on the page. If the button is below the middle of the screen, the list id displayed above it, if the list is above the middle, the list is displayed below it. The list should go nearly to the edge of the screen, if there are more options on the list than can be displayed the list will scroll.

Could you provide a small project that shows both of these issues as I can't recreate them on my devices.

What devices are you using?

Thanks
 

junglejet

Active Member
Licensed User
Longtime User
Hi Steve

give me some more time to figure out what's going on. I am just in the middle of it.

On the first issue I have a scrollview underlying which panel may be longer than the visual page. It appears the list is using the scrollview panel as reference.

Thanks
Andy
 

stevel05

Expert
Licensed User
Longtime User
Hi Andy,

Did you find out any more?

On the first issue, in the SetListPosition sub (Lines 173/174) I have used 100%y as SPListPnl is added to the Activity this should be the height of the screen, You could check it and change those to mAct.Height and see if it makes any difference.
 

junglejet

Active Member
Licensed User
Longtime User
Hi Steve,

some time passed by...busy...
I don't know if you are still online?
With mAct.Height instead of 100%y there is no difference.
I figured out with bigger fonts it is even worse.
I will try to understand your code.

Update: the SetListPosition routine does not recognize when a spinner sits on a scrollable panel (scrollview). It always behaves as if the spinner is at its designed posiiton from the top of the scrollview.

Andy
 
Last edited:
Top