How to change the bachground image of panel in runtime

gkumar

Active Member
Licensed User
Longtime User
I have a panel set with a background image through the designer with gravity Fill option. In the same panel few other controls also located. But In the run time how can I change the background image without modifying the any other properties of panel.
I tried with SetBackGroundImage() and Invalidate() after that. But it did not work.
 

gkumar

Active Member
Licensed User
Longtime User
Hello Erel,

I followed the 2nd method, still the background image is not changing.
I tried like by default I set the panel background as Color drawable, and in the code 2nd method as you mentioned above. But still its not displaying.
 
Upvote 0

gkumar

Active Member
Licensed User
Longtime User
Any update on the above one. I wanted this info for immediate implementation. Even I tried with ImageView, its not updating its image in runtime. The thing I found is If you remove the image view and add it again it works. If there is no other solution means I have to go with this solution.
One more thing I wanted is When image view with image is displyed it should be in background, because other button controls are hidden below the image.
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Any update on the above one.
Only 3 hours have passed...

This code changes the panel background when you click on the activity:
B4X:
Sub Globals
   Dim p As Panel   
End Sub

Sub Activity_Create(FirstTime As Boolean)
   p.Initialize("")
   Activity.AddView(p, 0, 0, 100dip, 100dip)
   p.Color = Colors.Yellow
End Sub

Sub Activity_Click
   Dim bd As BitmapDrawable
   bd.Initialize(LoadBitmap(File.DirAssets, "button.png"))
   bd.Gravity = Gravity.FILL
   p.Background = bd
End Sub
 
Upvote 0

gkumar

Active Member
Licensed User
Longtime User
Erel, thanks for the inputs.
But in my scenaro, I am adding a panel to activity with around 10-15 buttons in different locations in the panel. So I am not adding panel in the code. only from the designer. Thats why its not changing the image at runtime :BangHead: Any inputs for this issue?
Try to add a panel in the designer with bitmapdrawable and with a bitmap, and try to change the bitmap in the any part of the code at runtime.
 
Last edited:
Upvote 0

gkumar

Active Member
Licensed User
Longtime User
Hello Erel,

I tried with the small application its working! But from my application, (few more things have been done), Its not displaying the image. I am following the same method as in the sample you have given
I am wondering what may be the problem?:BangHead:
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
It would be much more efficient if you posted your project as a zip file so we could see what you are doing wrong.
You are asking for help but you don't show what you have done, whithout seeing your code how could you expect us guess what you have done.

Best regards.
 
Upvote 0

gkumar

Active Member
Licensed User
Longtime User
Finally its working, But the thing I found is, it works only if I add,
Activity.AddView(panel, 0, 30, 320, 280) in the activity_create, even though panel is added through designer. If I remove the above line, then background image will not br updated to new image.
What may be the reason for this one?

Thanks,
 
Upvote 0

achtrade

Active Member
Licensed User
Longtime User
B4X:
Sub Activity_ClickDim bd AsBitmapDrawable
bd.Initialize(LoadBitmap(File.DirAssets, "button.png"))
bd.Gravity = Gravity.FILL
p.Background = bd
End Sub

If after put an image with the code above I want to remove the image of p.background , how can I do it?

thanks
 
Last edited:
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…