IconButton - Add a icon or an Picture to a button

Mousa Najafi

Member
Licensed User
Longtime User
I added the iconbutton in the customlistview successfully. but in the button_click() event I couldn't use customlistview.getItemFromView(sender) because iconbutton is not a view while getItemFromView(view as View) expects
view argument.
As I have not access to original library I am not able to use reflection library also. Would you help me to use iconbutton for this purpose.

best Regard
 

Mousa Najafi

Member
Licensed User
Longtime User
I searched github with iconbutton library name. It seems there is not a function to cast iconbutton as view there.
If you are able to solve this problem the library will be more complete.
thanks
 

DonManfred

Expert
Licensed User
Longtime User
It seems there is not a function to cast iconbutton as view there.
If you are able to solve this problem the library will be more complete.

Try the version attached (also new in Post #1)

and get inspired here

B4X:
Sub IconButton_Click()
    Log("Button click")
    Dim ib As IconButton = Sender
    Log(ib.Tag)   
End Sub
 

Attachments

  • libIconButtonV1.1.1.zip
    8.7 KB · Views: 347

Mousa Najafi

Member
Licensed User
Longtime User
new version added to customlistview panel , but unfortunately is invisible in android 5 while button event get done . I tested on android 4.2.2 that showed correctly.
Also I added webview to viewpager panel that showed correctly on android 4.2 but blank on android 5
I don't know this relates to iconbutton and webview or something else.
 

Mousa Najafi

Member
Licensed User
Longtime User
Just I was in doubt which library causes the blank webview and missed iconbutton so I posted in both places
I will try the updated AHViewPager library which is designSupport. Am I in the right track ?
 

BarryW

Active Member
Licensed User
Longtime User
IconButton
Version:
1.11
  • IconButton
    Events:
    • Click()
    • Down()
    • HoverEnter()
    • HoverExit()
    • LongClick()
    • Up()
    Methods:
    • AddToParent (Parent As ViewGroup, left As Int, top As Int, width As Int, height As Int)
      programmatically add the view (the Button)
    • DesignerCreateView (base As PanelWrapper, lw As LabelWrapper, props As Map)
    • Initialize (EventName As String)
    • setIcon (LeftSide As Boolean, d As Drawable)
    • setPadding (left As Int, top As Int, right As Int, bottom As Int)
      Sets the Padding for this Button
    Properties:
    • Background As Drawable [write only]
      Sets the Buttons background
    • BackgroundColor As Int [write only]
      Sets the Buttons background color
    • BackgroundDrawable As Drawable [write only]
      Sets the Buttons background drawable (deprecated)
    • Enabled As Boolean [write only]
      Sets the Buttons enabled state
    • Height As Int
    • IconPadding As Int [write only]
      Set the Padding between Text and Icon
    • Left As Int
    • Text As String [write only]
      Set the Text
    • TextAlignment As Void
      Sets the Alignment for this Button
    • TextColor As Int [write only]
      Sets the Textcolor for this Button
    • Top As Int
    • Typeface As Typeface
      Sets the Typeface for this Button
    • Width As Int


B4X:
    Dim bm As BitmapDrawable
    bm.Initialize(LoadBitmap(File.DirAssets,"ic_action_alarm.png"))
    ib1.Text = "Button"
    ib1.IconPadding = 50
    ib1.setIcon(True,bm)

    Dim bm As BitmapDrawable
    bm.Initialize(LoadBitmap(File.DirAssets,"ic_action_achievement.png"))
    ib2.Text = "Button"
    ib2.IconPadding = 50
    ib2.setIcon(False,bm)

    Dim bm As BitmapDrawable
    bm.Initialize(LoadBitmap(File.DirAssets,"ic_action_android.png"))
    ib3.Text = "Button"
    ib3.IconPadding = 50
    ib3.setIcon(False,bm)

IconButton001.png

How to add tag? Tnx...
 

aviario

Active Member
Licensed User
Longtime User
Hello, i've two buttons in designer, but in Activity_Create i try to do:

bt1.AddToParent(MiPanel,bt1.Left ,bt1.top,bt1.Width,bt1.Height)

where "MiPanel As Panel " and i get the following error:


java.lang.NullPointerException
at de.donmanfred.IconButtonWrapper.getLeft(IconButtonWrapper.java:255)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:697)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:336)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:246)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:134)
at b4a.Botones.main.afterFirstLayout(main.java:102)
at b4a.Botones.main.access$000(main.java:17)
at b4a.Botones.main$WaitForLayout.run(main.java:80)
at android.os.Handler.handleCallback(Handler.java:605)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4437)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:835)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:602)
at dalvik.system.NativeStart.main(Native Method)

Thanks
 

Attachments

  • Botones.zip
    63.8 KB · Views: 294

DonManfred

Expert
Licensed User
Longtime User
Not really, just downloaded, there is still no icons :)
I just did a download of the example from post #1. The icons ARE in the files folder

Any chance set icon possition on top or bottom ?
The underlying Library does not expos a method to change the position to top or bottom.
 

LucianoB

Member
Licensed User
Longtime User
Thanks for this lib, its very usefull. But I'm having a small problem. I used your example and created events just for the first button ib1
B4X:
Sub ib1_Down()
    Log("down")
End Sub

Sub ib1_Up()
    Log("up")
End Sub

Sub ib1_HoverEnter()
    Log("hover enter")
End Sub

Sub ib1_HoverExit()
    Log("hover exit")
End Sub

Then when I push the ib1, without releasing the finger from it, the code goes through all four events I created: Down, Up HoverEnter, HoverExit.
What am I doing wrong?
thank you for your help

You can see the attached example.
 

Attachments

  • IconButtonsProb.zip
    7.4 KB · Views: 286

LucianoB

Member
Licensed User
Longtime User
Maybe...
what I would like to achieve is to have buttons changing color when pushed. I used the way shown on this example with normal views, but here, when the first button is pushed, the code executes the _Up() sub also. Any idea? thank you
 

Attachments

  • IconButtonsProb1.zip
    7.6 KB · Views: 295

Lello1964

Well-Known Member
Licensed User
Longtime User
Hello Don,

TextAlignment doesn't work

i have tryed ib1.TextAlignment=Gravity.RIGHT, but no changes.
 

Rusty

Well-Known Member
Licensed User
Longtime User
Is there a way to set the font-size on an iconbutton in code?
It can be set in the designer, but there is not method for text-size...that I can find.
Thanks,
Rusty
 

BarryW

Active Member
Licensed User
Longtime User
IconButton
Version:
1.11
  • IconButton
    Events:
    • Click()
    • Down()
    • HoverEnter()
    • HoverExit()
    • LongClick()
    • Up()
    Methods:
    • AddToParent (Parent As ViewGroup, left As Int, top As Int, width As Int, height As Int)
      programmatically add the view (the Button)
    • DesignerCreateView (base As PanelWrapper, lw As LabelWrapper, props As Map)
    • Initialize (EventName As String)
    • setIcon (LeftSide As Boolean, d As Drawable)
    • setPadding (left As Int, top As Int, right As Int, bottom As Int)
      Sets the Padding for this Button
    Properties:
    • Background As Drawable [write only]
      Sets the Buttons background
    • BackgroundColor As Int [write only]
      Sets the Buttons background color
    • BackgroundDrawable As Drawable [write only]
      Sets the Buttons background drawable (deprecated)
    • Enabled As Boolean [write only]
      Sets the Buttons enabled state
    • Height As Int
    • IconPadding As Int [write only]
      Set the Padding between Text and Icon
    • Left As Int
    • Text As String [write only]
      Set the Text
    • TextAlignment As Void
      Sets the Alignment for this Button
    • TextColor As Int [write only]
      Sets the Textcolor for this Button
    • Top As Int
    • Typeface As Typeface
      Sets the Typeface for this Button
    • Width As Int


B4X:
    Dim bm As BitmapDrawable
    bm.Initialize(LoadBitmap(File.DirAssets,"ic_action_alarm.png"))
    ib1.Text = "Button"
    ib1.IconPadding = 50
    ib1.setIcon(True,bm)

    Dim bm As BitmapDrawable
    bm.Initialize(LoadBitmap(File.DirAssets,"ic_action_achievement.png"))
    ib2.Text = "Button"
    ib2.IconPadding = 50
    ib2.setIcon(False,bm)

    Dim bm As BitmapDrawable
    bm.Initialize(LoadBitmap(File.DirAssets,"ic_action_android.png"))
    ib3.Text = "Button"
    ib3.IconPadding = 50
    ib3.setIcon(False,bm)

IconButton001.png

any way to resize textsize?
 
Top