Panel image background disappear

postasat

Active Member
Licensed User
Longtime User
I repeat my question also here.

In my code I have only one activity (main) and often I call a sub to change panel as in code below.
Everything is ok but sometimes, randomly, one of the panel that I call not show his background image (show a black background).
I defined background image (jpg <100K) only in designer (image view).
As Erel already told me I added a line with panel1.removeview but nothing change.
My application is finish and perfectly functional except for this "bug".

I hope in your help.

CODE
Sub LoadLayoutToPanel (Layout As String)
If Panel1.IsInitialized Then
Activity.RemoveViewAt(0)
End If
Panel1.Initialize("")
Panel1.removeview
Panel1.LoadLayout(Layout)
Activity.AddView(Panel1, 0, 0, 100%x, 100%y)
End Sub
 

postasat

Active Member
Licensed User
Longtime User
I tried with Activity.RemoveView but I had no result. I don't know why I have this problem.

To solve I found a way (maybe not correct but functional). I call a sub and load an "activity.background" immediately after I loaded a layout with the above sub "LoadtoLayout".

But to do this I must load all the images that I will use as background in Activity.create. If I try to load in other part of the program the program after some time terminate (out of memory).

I use this sub below where bd1 --> bd6 are my bitmap.drawable that I load in the beginning as background.

B4X:
Sub callscreen6
  Activity.RemoveView
  Activity.Background = bd6
End Sub

I had to Dim all images as bitmap.drawable:
B4X:
Dim bd1, bd2, bd3, bd4, bd5, bd6 As BitmapDrawable

I hope to help other people but I hope in the future to discover why I had this problem...
 
Upvote 0

nfordbscndrd

Well-Known Member
Licensed User
Longtime User
I have a 7" tablet running Android 2.2 and have never had a problem with it, but on my Toshiba Thrive, a 10.1" tablet running 3.1, views sometimes don't finish drawing, which could be your problem if you are also testing on a 10" tablet; otherwise, probably not.

To get around the problem, I use the following type of code:

B4X:
Activity.AddView(Panel1, 0, 0, 100%x, 100%y)
For i = 1 To 2
   Panel1.Invalidate
   DoEvents
Next

I do it twice because usually once just won't do the job.
 
Upvote 0

ADPTraining

Member
Licensed User
Longtime User
postasat I have exactly the same issue. I think it has to do with Activity not clearing it's memory completely. I'll try your suggestion...
 
Upvote 0

ADPTraining

Member
Licensed User
Longtime User
Backgrounds not loading properly.

To Postasat and others that might benefit. First off, thanks Postasat, you technique worked, with slight changes for me. I did exactly as you mentioned. Here's the Sub:


Dim BackGndImage As BitmapDrawable

Sub Activity_Create(FirstTime As Boolean)
'Do not forget to load the layout file created with the visual designer. For example:
'Activity.LoadLayout("Layout1")

getdtc.Initialize("")
Activity.AddView(getdtc, 0, 0, 100%x, 100%y)
getdtc.LoadLayout("dtc")
getdtc.Visible = True
BackGndImage.Initialize(LoadBitmap(File.DirAssets, "background medium.jpg")) '<--Added this
Activity.Background = BackGndImage '<--Added this

End Sub

I also added this Sub...


Sub Activity_KeyPress (KeyCode As Int) As Boolean
If KeyCode = KeyCodes.KEYCODE_BACK Then '<--If BACK key then End current Activity.
Activity.RemoveAllViews
Activity.Finish
End If
End Sub

Now the background loads as expected and have tested it many times. From what I can see, Basic4Android is having a hard time loading from the Visual Interface. When the background is in code then it works fine and like you said, load it on the Activity_Create. Thanks...

PS. I'm also having the same issue with loading Views, so have a feeling this will also do the trick. Not professional, but works...
 
Upvote 0

ADPTraining

Member
Licensed User
Longtime User
Layout image not showing

Besides what my older post says, I ended up NOT PUTTING and image to the layout. The best practice is to Load a panel to the layout, then load an image to the panel. At least on the Designer, loading images to the layout directly causes intermittent issues of the image or bitmap not showing at times. Hope that helps
 
Upvote 0
Top