How to create an image rollover?

danspiegel

New Member
Licensed User
Longtime User
All,

Looks like imageViews only have click and long click events. How can I create a rollover effect for images that are serving as buttons?

Thanks in advance for your help.

-Dan
 

danspiegel

New Member
Licensed User
Longtime User
Thanks for the reply. The issue with this toggle button solution is that I want to have an image that changes on click. I am not just trying to change from one solid background color to another. And, the toggle button does not seem to allow an image to display.

Perhaps I am misunderstanding some aspect of your proposed solution though.
 
Upvote 0

Kevin

Well-Known Member
Licensed User
Longtime User
The statelistdrawable option is a bit hard to find but it is in the button settings. You need to change the 'style' before you can allow it use images. It took me a little while to figure that out. Unfortunately I don't have this stuff in front of me, but the settings you need to change are near the bottom of the properties list.
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
The following code will create a button that the has two images. One when it is pressed and the other when it is not pressed.
B4X:
Sub Globals
   Dim btn As Button
End Sub

Sub Activity_Create(FirstTime As Boolean)
   Dim sd As StateListDrawable
   sd.Initialize
   Dim bd1, bd2 As BitmapDrawable
   bd1.Initialize(LoadBitmap(File.DirAssets, "x.jpg")) 'change to images you include in your project
   bd2.Initialize(LoadBitmap(File.DirAssets, "icon.jpg"))
   sd.AddState(sd.State_Pressed, bd2)
   sd.AddCatchAllState(bd1)
   'the following two lines are not needed if the button is added with the designer
   btn.Initialize("btn")
   Activity.AddView(btn, 10dip, 10dip, 200dip, 200dip)
   btn.Background = sd
End Sub

Note that you can also do it in the designer.
 
Upvote 0
Top