Set Background image

Bernd68

Member
Licensed User
Longtime User
Hi all,

i am struggling with the idea to iterate through BitmapDrawables like you can see on the code snippet. As i did read the tutorials for generating Buttons with a "for next" loop, i'd like to do the same with attaching png's to the Buttons.

Any idea?
My solution causes an error "inconvertible" types (of course Drawable vs. string), but there must be a way around.....

:BangHead:


B4X:
Dim bmp0, bmp1, bmp2, bmp3 As BitmapDrawable
    bmp0.Initialize(LoadBitmap(File.DirAssets, "ic_menu_today.png"))
    bmp1.Initialize(LoadBitmap(File.DirAssets, "ic_menu_week.png"))
    bmp2.Initialize(LoadBitmap(File.DirAssets, "ic_menu_favorite.png"))
   bmp3.Initialize(LoadBitmap(File.DirAssets, "ic_menu_settings_holo_light.png"))
   
   'Add menu items to menu
   For i = 0 To 3 'create 4 buttons as panels
        Log(i)
      Dim mBtn As Panel
        mBtn.Initialize("mBtn")
      mBtn.Color = Colors.Black
        If i = 0 Then
         pnlMenu.AddView(mBtn,0, 2dip, mBtnWidth, mHeight-3dip)
         mBtn.Background = "bmp" & i
       Else
         pnlMenu.AddView(mBtn,1dip + mBtnWidth * i, 2dip, mBtnWidth-1dip, mHeight-3dip)
                           mBtn.Background = "bmp" & i
      End If
   Next
 

NJDude

Expert
Licensed User
Longtime User
What you need to do is to create an array of BMP's
B4X:
bmp(1).Initialize(LoadBitmap(File.DirAssets, "ic_menu_today.png"))
bmp(2).Initialize(LoadBitmap(File.DirAssets, "ic_menu_week.png"))
bmp(3).Initialize(LoadBitmap(File.DirAssets, "ic_menu_favorite.png"))
bmp(4).Initialize(LoadBitmap(File.DirAssets, "ic_menu_settings_holo_light.png"))
 
Upvote 0

Bernd68

Member
Licensed User
Longtime User
Perfect and one more Question

Perfect. Did the trick for me.

One more question...Is it possible to set the height and width of the Background-Image?

Thx in advance

Bernd
 
Upvote 0

Bernd68

Member
Licensed User
Longtime User
Use LoadBitmapSample instead of LoadBitmap.

Thank you for the tip. But it was not for the purpose i did want to achive.
My idea was to set Height & Width to a fixed size, but setting a backround image always scales the image to the view size.

I finally added an ImageView on top of the panel with a smaller Width and Height.

Rgds Bernd
 
Upvote 0
Top