Android Code Snippet SD: Spinner with CustomView

Johan Hormaza

Well-Known Member
Licensed 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
        Top = 0
        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
        MyAct=Obj
    
        If Props.ContainsKey("BackgroundColor") Then BColor = Props.Get("BackgroundColor")
        If Props.ContainsKey("BackgroundColorList") Then BColorList=Props.Get("BackgroundColorList")
        If Props.ContainsKey("TextColor") Then TColor = Props.Get("TextColor")
        If Props.ContainsKey("TextSize") Then TextSize = Props.Get("TextSize")
        If Props.ContainsKey("HintColor") Then HintColor = Props.Get("HintColor")
        If Props.ContainsKey("GrayDisplay") Then GrayDisplay = Props.Get("GrayDisplay")
        If Props.ContainsKey("RoundCorner") Then RoundCorner = Props.Get("RoundCorner")
    
        Lab.TextColor = TColor
    Lab.Color = 0xFFE2E2E2
        Lab.Typeface = Typeface.DEFAULT_BOLD
        Lab.Gravity = Gravity.CENTER
        Lab.Gravity = Gravity.CENTER_VERTICAL
        Lab.TextSize = TextSize
        Lab.BringToFront
        Lab.Text = "Pot. Luminaria"
        SetIndex(SelectedIndex)
    mBase.Color = 0xFFE2E2E2
        mBase.AddView(Lab,13%x,0,mBase.Width,mBase.Height)
        mBase.AddView(img,3dip,2dip,12%x,mBase.Height - 4dip)
 

Star-Dust

Expert
Licensed User
I'm not sure I understand what you're asking
 

Star-Dust

Expert
Licensed 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
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
Last edited:

Johan Hormaza

Well-Known Member
Licensed User
It's wrong.
To set Top and Left of the view, use Spinner.GetBase.Top and Spinner.GetBase.Left.

While Spinner.Top and Spinner.Left establish the position to open the Spinner
My brother, thank you, God bless you ...
Everything works now in an extraordinary way!:):):)
 

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
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
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
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)
 
Top