Android Code Snippet SD: Spinner with CustomView

I've created a new class with CistomView to have the same features on the spinner object as ListView. A second line of text and the ability to add an image beside each item.

Also there is the ability to put the screen in gray when you click on the spinner. And finally, the ability to represent each item with rounded edges. In the cases there is an image that will also be rounded.

Hope it may be useful to you

Proprietes
Enabled As Boolean
SelectedItem As String
SelectedID As String
SelectedIndex As String
GrayDisplay As Boolean ' If Make Screeen gray when click (Set/Get)
BColor As Int ' BackgroundColor of Spinner's Label (Get/Set)
TColor As Int ' TextColor of Spinner's Label (Get/Set)
HintColor As Int ' TextColor of Spinner's Hint (Get/Set)
RoundCorner As Boolean
If the spinner opens in an anomalous position:
Set these prorpietes (left e Top) to set the screen position of the spinner's aperture on the screen.
For this reason we do not recommend inserting the spinner inside a scrollView because it has problems calculating its opening, or setting the position from code using left and top

Left As Int
Top As Int

Method
Initialize (vCallback As Object, vEventName As String)
Add(Text As String, TextHelp As String,ID As String, BackgroundColor As Int, TextColor As Int, Gravities As Int,Image As Bitmap)
' Add Single element
AddAll(L As List, TextHelp As String, BackgroundColor As Int, TextColor As Int, Gravities As Int)
' Add a List
Size as int ' return number of elements
Clear ' Clear all elements
RemoveIndex(Position As Int) ' Remove element at Index
RemoveID(ID As String) ' Remove element from ID
GetItem(Position As Int) As String ' Get Value from position/index
GetItemFromID(ID As String) As String ' Get Value from ID
GetID(Position As Int) As String ' Get ID at position/index
SetIndex(Position As Int) ' Set selected from position/index
SetID(Id As String) ' Set selected from ID
SetBackgroundDrawable(Draw As ColorDrawable) ' Set Background with colordrawable
SetBackgroundImage(Bitmap As Bitmap) ' Set Background with image
SetLabelTextSize(TextSizeLabel As Int) ' Set Text Size of label
Close ' close a ListItem windows
Open ' Apen a ListItem Windows​

Event
ItemClick (Position As Int, Value as String)​

Update 1.02
Screen1.png Screen2.png Screen3.png Screen4.png Screen5.png Screen6.png
 

Attachments

  • SD_Spinner 1.02.zip
    19.7 KB · Views: 1,307
Last edited:

Star-Dust

Expert
Licensed User
Longtime User
View attachment 67819

This is the problem, the Spinner is in a panel with ScrollView
Ok, so the problem is that.
Positions can not be correctly identified within the scrollView.
To calculate them you must also know the position of the panel inside the scrollview. He only calculates the position of the ScrollView within the activity.
 

Star-Dust

Expert
Licensed User
Longtime User
Is this sub true?
B4X:
Public Sub DesignerCreateView (Base As Panel, Lbl As Label, Props As Map)
        mBase = Base
        mBase.Tag="SD_SPINNER"
  
        Left = 0  <------------------------- this
        Top = 0  <------------------------- this
        Dim Obj As View = mBase
        Do While Not(GetType(Obj) = "android.widget.FrameLayout")
            Try
                Left = Left + Obj.Left
                Top =Top + Obj.Top
            Catch
                Log(".")
            End Try
            Obj = Obj.Parent
        Loop
     ....
Make the Left and Top variables available at the beginning of DesignerCreateView and edit them from code.
 

Star-Dust

Expert
Licensed User
Longtime User
Last edited:

Daniel Konstantinidis

New Member
Licensed User
Hello, I am new in B4A.
I like your library but I am having some problems implementing it in my project:


Logger connected to: motorola Moto E (4) Plus
--------- beginning of crash
--------- beginning of main
Copying updated assets files (6)
*** Service (starter) Create ***
--------- beginning of system
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
*** Service (httputils2service) Create ***
** Service (httputils2service) Start **
** Activity (main) Pause, UserClosed = false **
** Activity (pelatesdetails) Create, isFirst = true **
Errore
Error occurred on line: 0 (SD_Spinner)
java.lang.RuntimeException: Object should first be initialized (View).
at anywheresoftware.b4a.AbsObjectWrapper.getObject(AbsObjectWrapper.java:50)
at b4a.example.sd_spinner._designercreateview(sd_spinner.java:632)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:733)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:355)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:255)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:144)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:175)
at anywheresoftware.b4a.objects.CustomViewWrapper.AfterDesignerScript(CustomViewWrapper.java:61)
at anywheresoftware.b4a.keywords.LayoutBuilder.loadLayout(LayoutBuilder.java:162)
at anywheresoftware.b4a.objects.PanelWrapper.LoadLayout(PanelWrapper.java:134)
at anywheresoftware.b4a.objects.TabHostWrapper.createPanelForLayoutFile(TabHostWrapper.java:117)
at anywheresoftware.b4a.objects.TabHostWrapper.AddTabWithIcon(TabHostWrapper.java:160)
at b4a.example.pelatesdetails._configurarcontroles(pelatesdetails.java:397)
at b4a.example.pelatesdetails._activity_create(pelatesdetails.java:366)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:733)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:355)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:255)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:144)
at b4a.example.pelatesdetails.afterFirstLayout(pelatesdetails.java:104)
at b4a.example.pelatesdetails.access$000(pelatesdetails.java:17)
at b4a.example.pelatesdetails$WaitForLayout.run(pelatesdetails.java:82)
at android.os.Handler.handleCallback(Handler.java:836)
at android.os.Handler.dispatchMessage(Handler.java:103)
at android.os.Looper.loop(Looper.java:203)
at android.app.ActivityThread.main(ActivityThread.java:6339)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1084)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:945)
** Activity (pelatesdetails) Resume **
 

Star-Dust

Expert
Licensed User
Longtime User
Hello, I am new in B4A.
I like your library but I am having some problems implementing it in my project:


Logger connected to: motorola Moto E (4) Plus
--------- beginning of crash
--------- beginning of main
Copying updated assets files (6)
*** Service (starter) Create ***
--------- beginning of system
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
*** Service (httputils2service) Create ***
** Service (httputils2service) Start **
** Activity (main) Pause, UserClosed = false **
** Activity (pelatesdetails) Create, isFirst = true **
Errore
Error occurred on line: 0 (SD_Spinner)
java.lang.RuntimeException: Object should first be initialized (View).
at anywheresoftware.b4a.AbsObjectWrapper.getObject(AbsObjectWrapper.java:50)
at b4a.example.sd_spinner._designercreateview(sd_spinner.java:632)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:733)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:355)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:255)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:144)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:175)
at anywheresoftware.b4a.objects.CustomViewWrapper.AfterDesignerScript(CustomViewWrapper.java:61)
at anywheresoftware.b4a.keywords.LayoutBuilder.loadLayout(LayoutBuilder.java:162)
at anywheresoftware.b4a.objects.PanelWrapper.LoadLayout(PanelWrapper.java:134)
at anywheresoftware.b4a.objects.TabHostWrapper.createPanelForLayoutFile(TabHostWrapper.java:117)
at anywheresoftware.b4a.objects.TabHostWrapper.AddTabWithIcon(TabHostWrapper.java:160)
at b4a.example.pelatesdetails._configurarcontroles(pelatesdetails.java:397)
at b4a.example.pelatesdetails._activity_create(pelatesdetails.java:366)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:733)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:355)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:255)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:144)
at b4a.example.pelatesdetails.afterFirstLayout(pelatesdetails.java:104)
at b4a.example.pelatesdetails.access$000(pelatesdetails.java:17)
at b4a.example.pelatesdetails$WaitForLayout.run(pelatesdetails.java:82)
at android.os.Handler.handleCallback(Handler.java:836)
at android.os.Handler.dispatchMessage(Handler.java:103)
at android.os.Looper.loop(Looper.java:203)
at android.app.ActivityThread.main(ActivityThread.java:6339)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1084)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:945)
** Activity (pelatesdetails) Resume **
The error indicates that it has not been initialized.
Show the code where you state, initialize and use the Spinner class.
 

Star-Dust

Expert
Licensed User
Longtime User
To enter a code you must put it in here
upload_2018-10-7_21-31-26.png

It seems you did not initialize them Spinner.
Did you include the Spinner in Design?
Have you seen my example?
 

Daniel Konstantinidis

New Member
Licensed User
Thank you about telling me the way to put a code, I didn't know how.
1)Yes I did include the Spinner in Design.
2)I am trying to reproduce your example.

May be the fact that I am implementing the spinner inside a TabHost matters?
 

Star-Dust

Expert
Licensed User
Longtime User
2)I am trying to reproduce your example.
I think not, but I see your code is very messy.

Try a standar spinner and see if it generates an error.

I make your code call a function of the Spinner Class when isn't initialized.
I see that you receive data from the internet and at some point close and reopen an activity (what is wrong).
They seem not very clear things
 
Last edited:

Daniel Konstantinidis

New Member
Licensed User
If this helps, this is the errors I get after debuging my app :
B4X:
** Activity (main) Resume **
** Activity (main) Pause, UserClosed = false **
** Activity (pelatesdetails) Create, isFirst = true **
Error occurred on line: 59 (SD_Spinner)
java.lang.ClassCastException: android.view.ViewGroup$LayoutParams cannot be cast to anywheresoftware.b4a.BALayout$LayoutParams
    at anywheresoftware.b4a.objects.ViewWrapper.getLeft(ViewWrapper.java:162)
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:733)
    at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:352)
    at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:255)
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:144)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:175)
    at anywheresoftware.b4a.objects.CustomViewWrapper.AfterDesignerScript(CustomViewWrapper.java:61)
    at anywheresoftware.b4a.keywords.LayoutBuilder.loadLayout(LayoutBuilder.java:162)
    at anywheresoftware.b4a.objects.PanelWrapper.LoadLayout(PanelWrapper.java:134)
    at anywheresoftware.b4a.objects.TabHostWrapper.createPanelForLayoutFile(TabHostWrapper.java:117)
    at anywheresoftware.b4a.objects.TabHostWrapper.AddTabWithIcon(TabHostWrapper.java:160)
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:733)
    at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:355)
    at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:255)
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:144)
    at b4a.example.pelatesdetails.afterFirstLayout(pelatesdetails.java:104)
    at b4a.example.pelatesdetails.access$000(pelatesdetails.java:17)
    at b4a.example.pelatesdetails$WaitForLayout.run(pelatesdetails.java:82)
    at android.os.Handler.handleCallback(Handler.java:836)
    at android.os.Handler.dispatchMessage(Handler.java:103)
    at android.os.Looper.loop(Looper.java:203)
    at android.app.ActivityThread.main(ActivityThread.java:6339)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1084)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:945)
 

Star-Dust

Expert
Licensed User
Longtime User
Say you are assigning a type of type seen a value or a variable of other type. Check the line 59
 
Top