Android Question IconButton(s) which one was clicked

Rusty

Well-Known Member
Licensed User
Longtime User
I have installed several iconbuttons on my activity.
Each one is directed to the same "click" event. I need to determine which one was clicked. I set a Tag value that tells me which one it was, but...
When the click event fires, I do this:
B4X:
Sub icbCategory_Click
    Try
        Dim ThisIconButton As IconButton
        ThisIconButton = Sender
        Dim tg As string
        tg = ThisIconButton.tag
log("thisiconbutton " & thisiconbutton.tag)
    Catch
        Log("icbCategory_Click error " & LastException.Message)
    End Try
End Sub

It abends and logs:
Error occurred on line: 2767 (Main)
java.lang.ClassCastException: anywheresoftware.b4a.ShellBA cannot be cast to android.view.View
at anywheresoftware.b4a.objects.ViewWrapper.getTag(ViewWrapper.java:232)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:708)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:337)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:247)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:134)
at de.donmanfred.IconButtonWrapper$1.onClick(IconButtonWrapper.java:73)
at android.view.View.performClick(View.java:4487)
at android.view.View$PerformClick.run(View.java:18746)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:149)
at android.app.ActivityThread.main(ActivityThread.java:5257)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:794)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:610)
at dalvik.system.NativeStart.main(Native Method)

I don't understand the error. Is the iconbutton not a view?
any help will be appreciated.
Rusty
 

Rusty

Well-Known Member
Licensed User
Longtime User
Hi Don,
The Demo program provided shows the CLICK event and it works but doesn't provide any information that I can find that is useful, through normal means.



I was trying to use your iconify library but it fails. I posted it earlier today.
It dies on the loadlayout with
** Service (starter) Create **
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
Error occurred on line: 83 (Main)
java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
at anywheresoftware.b4a.keywords.LayoutBuilder.loadLayout(LayoutBuilder.java:166)
at anywheresoftware.b4a.objects.ActivityWrapper.LoadLayout(ActivityWrapper.java:209)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:708)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:340)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:247)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:134)
at b4a.example.iconify.main.afterFirstLayout(main.java:102)
at b4a.example.iconify.main.access$000(main.java:17)
at b4a.example.iconify.main$WaitForLayout.run(main.java:80)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:149)
at android.app.ActivityThread.main(ActivityThread.java:5257)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:794)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:610)
B4X:
Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:

    icfy.Initialize()
    Entypo.Initialize
    icfy.with(Entypo)
  
    FontAwesome.Initialize
    icfy.with(FontAwesome)
  
    Ionicons.Initialize
    icfy.with(Ionicons)
  
    MaterialCommunity.Initialize
    icfy.with(MaterialCommunity)
  
    Material.Initialize
    icfy.with(Material)

    Meteocons.Initialize
    icfy.with(Meteocons)

    SimpleLineIcons.Initialize
    icfy.with(SimpleLineIcons)

    Typicons.Initialize
    icfy.with(Typicons)

    Weathericons.Initialize
    icfy.with(Weathericons)
  
      
    Activity.LoadLayout("Layout1")
line 83 is the Activity.loadlayout above

Any suggestions? We can move this to another thread if you wish.
Thanks,
Rusty
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
The library has a problem it looks like....
It is in a "old style" from me... I´ve changed due to b4a6....
I´ll rewrite/update the lib the next days. But as of now i´ve not the time asi´mworking on a customer wrapper
 
Upvote 0

Rusty

Well-Known Member
Licensed User
Longtime User
Same results as before.
** Activity (main) Resume **
Error occurred on line: 71 (Main)
java.lang.ClassCastException: anywheresoftware.b4a.BA cannot be cast to android.view.View
at anywheresoftware.b4a.objects.ViewWrapper.getTag(ViewWrapper.java:232)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:708)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:337)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:247)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:134)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:157)
at de.donmanfred.IconButtonWrapper$1.onClick(IconButtonWrapper.java:73)
at android.view.View.performClick(View.java:4487)
at android.view.View$PerformClick.run(View.java:18746)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:149)
at android.app.ActivityThread.main(ActivityThread.java:5257)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:794)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:610)
at dalvik.system.NativeStart.main(Native Method)
thisiconbutton (Exception) java.lang.Exception: java.lang.ClassCastException: anywheresoftware.b4a.BA cannot be cast to android.view.View
button1 button1

line 71 :
B4X:
Try
        Dim ThisIconButton As IconButton
        ThisIconButton = Sender
        Dim tg As String
this is line 71 ->        tg = ThisIconButton.tag
Log("thisiconbutton " & ThisIconButton.tag)
    Catch
        Log("thisiconbutton " & LastException)
    End Try
Rusty
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
For me it works with V1.13

B4X:
Sub IconButton_Click()
    Log("Button click")
    Dim ib As IconButton = Sender
    Log(ib.Tag)   
   
End Sub
results in
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
Button click
Android
Button click
BarCode
Button click
Button
Button click
BarCode
 
Upvote 0

Rusty

Well-Known Member
Licensed User
Longtime User
... my mistake.
Apparently, I had an iconbutton.jar in my main Android libraries folder and I was putting the new one in the additional libraries.
I removed the one in the main folder and now it works in the demonstration program where the iconbutton is added to the Activity.

One last question, is there a way to change the text size and gravity?
Thanks Don!
 
Last edited:
Upvote 0
Top