Android Code Snippet Tiling an Image: SetTiledBackground

SubName: SetTiledBackground
Description: Allows you to fill the view/activity background with a tile image

B4X:
Sub SetTiledBackground(v as View, b as Bitmap)
  Dim bd As BitmapDrawable
  bd.Initialize(b)

  Dim r As Reflector
  r.Target = bd
  r.RunMethod3("setTileModeXY","REPEAT", "android.graphics.Shader$TileMode", _
                        "REPEAT", "android.graphics.Shader$TileMode")
  v.Background = bd
End Sub

Tags: UI, Tiled, Background, Activity
 

Shahid Saeed

Active Member
Licensed User
Longtime User
SubName: SetTiledBackground
Description: Allows you to fill the view/activity background with a tile image

B4X:
Sub SetTiledBackground(v as View, b as Bitmap)
  Dim bd As BitmapDrawable
  bd.Initialize(b)

  Dim r As Reflector
  r.Target = bd
  r.RunMethod3("setTileModeXY","REPEAT", "android.graphics.Shader$TileMode", _
                        "REPEAT", "android.graphics.Shader$TileMode")
  v.Background = bd
End Sub

Tags: UI, Tiled, Background, Activity
Can you further explain the execution of this sub:

how should i implement it in my activity?

B4X:
Dim BG As Bitmap = LoadBitmapSample(File.DirAssets, "bg.jpg", Activity.Width, Activity.Height)
SetTileBackground(Activity.SetBackgroundImage(), BG)
[code]

OR 

[code]
Dim BG As Bitmap = LoadBitmapSample(File.DirAssets, "bg.jpg", Activity.Width, Activity.Height)
Activity.SetBackgroundImage(SetTileBackground(What to put here, BG))
 

Shahid Saeed

Active Member
Licensed User
Longtime User
Note the parameter v is a view, and the sub assigns v.Background at the end.
Use it like:

B4X:
SetTileBackground(Activity, BG)
With this code I was able to set tiled background on Activity, but when I am trying to add tiled background on a panel, it doesn't display the background?
 

Tom Christman

Active Member
Licensed User
Longtime User
Note the parameter v is a view, and the sub assigns v.Background at the end.
Use it like:

B4X:
SetTileBackground(Activity, BG)
Thanks Desolate for this example. I think the above should be
B4X:
SetTiledBackground(Activity,BG)
. It was missing the d in Tiled. I tried it out and it's very cool./...inserted this in Periklis' extraviews movie code as background for the gif plays.
 

Shahid Saeed

Active Member
Licensed User
Longtime User
Thanks Desolate for this example. I think the above should be
B4X:
SetTiledBackground(Activity,BG)
. It was missing the d in Tiled. I tried it out and it's very cool./...inserted this in Periklis' extraviews movie code as background for the gif plays.

the d will not do anything since the SetTileBackground is a sub which have same spellings. The code is working fine when loading the background in The Activity; it is just not displaying the background when I am applying the code to a panel instead of Activity.
 

Tom Christman

Active Member
Licensed User
Longtime User
@ Shahid Saeed: My reply was not to you and did not refer to your code. It was to the Desolute Soul and the fact that anyone using HIS code and used that call would get an error. It didn't refer to your code at all!
 

thedesolatesoul

Expert
Licensed User
Longtime User
With this code I was able to set tiled background on Activity, but when I am trying to add tiled background on a panel, it doesn't display the background?
It works in a panel, I just tried it.
Uploaded a sample.

@ Shahid Saeed: My reply was not to you and did not refer to your code. It was to the Desolute Soul and the fact that anyone using HIS code and used that call would get an error. It didn't refer to your code at all!
I think you need to read the thread in context. I was refering to Shahid Saeed's code because for some reason he calls the sub differently in his code.
I know, its getting confusing right :p
But any programmer worth his salt better be able to figure it out.
 

Attachments

  • TempTest.zip
    8.6 KB · Views: 461
Last edited:

Shahid Saeed

Active Member
Licensed User
Longtime User
It works in a panel, I just tried it.
Uploaded a sample.


I think you need to read the thread in context. I was refering to Shahid Saeed's code because for some reason he calls the sub differently in his code.
I know, its getting confusing right :p
But any programmer worth his salt better be able to figure it out.
I am adding panel through Designer View:- below is code of your example with panel added through designer view but it doesn't show the background image.

B4X:
Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.
   
End Sub

Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.

End Sub

Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    Activity.LoadLayout("main")
   
    Dim Panel1 As Panel 
    Panel1.Initialize("")
    'Activity.AddView(p, 100dip, 100dip, 100dip,100dip)   
    SetTiledBackground(Panel1, LoadBitmap(File.DirAssets,"refresh.png"))
   
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Sub SetTiledBackground(v As View, b As Bitmap)
  Dim bd As BitmapDrawable
  bd.Initialize(b)

  Dim r As Reflector
  r.Target = bd
  r.RunMethod3("setTileModeXY","REPEAT", "android.graphics.Shader$TileMode", _
                        "REPEAT", "android.graphics.Shader$TileMode")
  v.Background = bd
End Sub
 
Top