Image scroll view add text

markh2011

Member
Licensed User
Longtime User
Hi,
How do i add the title of each picture to below the picture in the image view?
The following code loads the images from the program folder and then loads them into the image view.
I want to add the file name to display under each picture.
<code>Sub Activity_Create(FirstTime As Boolean)
If FirstTime Then 'only load the images once.
ProgressDialogShow("Loading images")
LoadImages
ProgressDialogHide
End If
btmpex.Initialize("BitmapExtended")
Activity.LoadLayout("Page4") 'load the layout file
scvPics.Panel.Height = 200dip * Bitmaps.Size 'Set the inner panel height according to the number of images.

For i = 0 To Bitmaps.Size - 1
Dim iv As ImageView 'create an ImageView for each bitmap
iv.Initialize("") 'not interested in any events so we pass empty string.
Dim bd As BitmapDrawable
bd.Initialize(Bitmaps.Get(i))
bd2 = bd.Bitmap
bd3 = btmpex.rotateBitmap(bd2,90)
iv.bitmap = bd3 'set the background of the image view.
'add the image view to the scroll bar internal panel.
scvPics.Panel.AddView(iv, 5dip, 5dip + i * 200dip, scvPics.Width - 10dip, 190dip)
Next
End Sub

Sub LoadImages
Bitmaps.Initialize
Dim files As List
Dim imagesFolder As String
imagesFolder = File.DirDefaultExternal
If File.Exists(imagesFolder, "") = False Then
ToastMessageShow("Images folder not found: " & CRLF & imagesFolder, True)
Return
End If
files = File.ListFiles(imagesFolder) 'get all files in this folder
For i = 0 To files.Size - 1
DoEvents 'required for the ProgressDialog animation
Dim f As String
f = files.Get(i)
If f.ToLowerCase.EndsWith(".jpg") Then
Dim b As Bitmap
b.InitializeSample(imagesFolder, f, 200dip, 200dip) 'load the jpeg file and subsample it if it is too large.
Bitmaps.Add(b) 'add the bitmap to the bitmaps list.
If Bitmaps.Size > 500 Then Exit 'limit it to 50 images
End If
Next
ToastMessageShow("Found " & Bitmaps.Size & " images", True)
End Sub
 

klaus

Expert
Licensed User
Longtime User
I would suggest you to add a Label below each ImageView to display the title.
B4X:
For i = 0 To Bitmaps.Size - 1
    Dim iv As ImageView 'create an ImageView for each bitmap
    iv.Initialize("") 'not interested in any events so we pass empty string.
    Dim bd As BitmapDrawable
    bd.Initialize(Bitmaps.Get(i))
    bd2 = bd.Bitmap
    bd3 = btmpex.rotateBitmap(bd2,90)
    iv.bitmap = bd3 'set the background of the image view.
    'add the image view to the scroll bar internal panel.
    scvPics.Panel.AddView(iv, 5dip, 5dip + i * 240dip, scvPics.Width - 10dip, 190dip)
    'add the lable
    Dim lbl As Label
    lbl.Initialize("")
    lbl.Gravity = Gravity.CENTER_HORIZONTAL
    scvPics.Panel.AddView(lbl, 5dip, 5dip + i * 240dip + 195dip, scvPics.Width - 10dip, 35dip)
    lbl.Text = ImageTitles.Get(i)
Next
You need to change
scvPics.Panel.Height = 200dip * Bitmaps.Size
to
scvPics.Panel.Height = 240dip * Bitmaps.Size

and add
Dim ImageTitles As List
ImageTitles.Initialize

and set the image titles in the LoadImages routine.

Best regards.
 
Last edited:
Upvote 0

markh2011

Member
Licensed User
Longtime User
Hi Klaus,
Thanks for the quick reply.
I am still a real newby to B4A so could you please give me the correct way to set the image titles in the LoadImages routine.
Thanks Mark
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
Here you are:
B4X:
Sub LoadImages
    Bitmaps.Initialize
    ImageTitles.Initialize
    Dim files As List
    Dim imagesFolder As String
    imagesFolder = File.DirDefaultExternal
    If File.Exists(imagesFolder, "") = False Then
        ToastMessageShow("Images folder not found: " & CRLF & imagesFolder, True)
        Return
    End If
    files = File.ListFiles(imagesFolder) 'get all files in this folder
    For i = 0 To files.Size - 1
        DoEvents 'required for the ProgressDialog animation
        Dim f As String
        f = files.Get(i)
        If f.ToLowerCase.EndsWith(".jpg") Then
            Dim b As Bitmap
            b.InitializeSample(imagesFolder, f, 200dip, 200dip) 'load the jpeg file and subsample it if it is too large.
            Bitmaps.Add(b) 'add the bitmap to the bitmaps list.
            ImageTitles.Add(f)
            If Bitmaps.Size > 500 Then Exit 'limit it to 50 images
        End If
    Next
    ToastMessageShow("Found " & Bitmaps.Size & " images", True)
End Sub
Add Dim ImageTitles as List in Sub Globals

If you had posted your project as a zip file, I would have modified it.

Best regards.
 
Upvote 0

markh2011

Member
Licensed User
Longtime User
Hi Klaus,
Thanks for your help!!!!
I still cant get it to work & am getting an error about remove View() at this line:
scvPics.Panel.AddView(iv, 5dip, 5dip + i * 240dip + 195dip, scvPics.Width - 10dip, 35dip)

I have attached you my project and would appreciate if you could have a look at it for me please.

Thanks Mark
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
Sorry, I made a mistake:
scvPics.Panel.AddView(iv, 5dip, 5dip + i * 240dip + 195dip, scvPics.Width - 10dip, 35dip)

must be
scvPics.Panel.AddView(lbl, 5dip, 5dip + i * 240dip + 195dip, scvPics.Width - 10dip, 35dip)

Attached you find a modified version, I added some color for the Labels.
I added also some variables for the ScrollView filling, I prefer working with variables, makes modifications easier.

I had to remove the ACL library (V4.5), I got following error:
Error parsing library
xvs.ACL.Base64.decode already exists.

Best regards.
 

Attachments

  • nugget1.zip
    236.4 KB · Views: 477
Upvote 0

markh2011

Member
Licensed User
Longtime User
Hi Klaus,
Thanks very much it works & looks great.:sign0188:
I also got the error re ACL version 4,5 so i had to use the earlier version. I have researched on the forum and it seems that others are also getting the same error.

Thanks again for your time & effort, i really appreciate it.
 
Last edited:
Upvote 0
Top