Android Tutorial List folder content and show selected image.

Attached is a little program shows how to read a directory content, show the filenames in a listview, and showing an example of the selected image file in imageview.

Duplicated from the original post in the spanish forum.

Here is the code:
B4X:
Sub Process_Globals
End Sub

Sub Globals
   Dim ListView1 As ListView
   Dim ImageView1 As ImageView
   Dim Label1 As Label
   
   Dim path1,file1 As String
End Sub

Sub Activity_Create(FirstTime As Boolean)

  Dim GD As GradientDrawable
  GD.Initialize("TR_BL", Array As Int(Colors.Blue, Colors.LightGray))
  Activity.Background = GD
   
   ListView1.Initialize("ListView1")
  ListView1.ScrollingBackgroundColor = Colors.Transparent
   
   ImageView1.Initialize("test")
   ImageView1.Color = Colors.ARGB(25,0,0,0)
   
   Label1.Initialize("Label1")
   Label1.Color = Colors.argb(90,0,0,0)
   Label1.TextColor = Colors.Yellow
   Label1.TextSize = 9

  ListView1.SingleLineLayout.ItemHeight = 50dip
  ListView1.SingleLineLayout.Label.TextSize = 20
  ListView1.SingleLineLayout.Label.TextColor = Colors.White
  ListView1.SingleLineLayout.Label.Gravity = Gravity.LEFT
  ListView1.FastScrollEnabled = True

  root=File.DirRootExternal
  path1 = root & "/DCIM/Camera"

  ListFolderContent(path1)

   Activity.AddView(ListView1, 0, 0, 100%x, 50%y)
   Activity.AddView(Label1, 0, 50%y, 100%x, 30dip)
   Activity.AddView(ImageView1, 0, (50%y + 30dip), 100%x, 50%y)

End Sub

Sub Activity_Resume
End Sub

Sub Activity_Pause (UserClosed As Boolean)
End Sub

Sub ListFolderContent(folder As String) As String
  Dim fileList As List
  Dim n As Int

  fileList = File.ListFiles(path1)
  fileList.Sort(True)

  For n = 0 To fileList.Size-1
    file1 = fileList.Get(n)
      ListView1.AddSingleLine(file1)
  Next
End Sub


Sub ListView1_ItemClick (Position As Int, Value As Object)
    Activity.Title = "Selected: ("& Position &") <" & Value &" >"
      
      Try
         ImageView1.Bitmap = LoadBitmapSample(path1,Value, ImageView1.Width, ImageView1.Height)
         Label1.Text = "Ok."
      Catch
         Label1.Text = "error " & LastException
      End Try
End Sub

enjoy :)
 

Attachments

  • ListviewDircontentImagebox.zip
    6.1 KB · Views: 1,825

sally3599

Member
Licensed User
Longtime User
some error message

when click a directory it shows:
B4X:
error(ErrnoException) libcore.io.ErrnoException: open failed: EISDIR(is a directory)
something wrong?
 

sally3599

Member
Licensed User
Longtime User
there are only three folders in the emulator

.android_secure
DCIM
LOST.DIR

it show "error(ErrnoException...." when I click DCIM.
 
Last edited:

wheretheidivides

Active Member
Licensed User
Longtime User
This code doesn't check whether the entry is actually a folder. You should use File.IsDirectory to make sure that the entry is not a folder.

So what would the updated code look like selecting a file? also, 'root' is not defined (unless I'm missing a library)". Also the pictures do not display the right size in the window.
 

bishmedia

Member
Licensed User
Longtime User
Text Box Alignment

Im testing a couple of text boxes and on my Nexus the text aligns fine but on my samsung s3 its at the top and cuts off as per attached photo, below is the code that i am using.

Any thoughts??

B4X:
   txtContactName.Initialize("txtContactName")
   txtContactName.TextSize = 20
   txtContactName.TextColor = Colors.Black
   txtContactName.Gravity = Gravity.LEFT + Gravity.BOTTOM
   txtContactName.Text = ""
   txtContactName.Background=cd
   Activity.AddView(txtContactName, 5%x, 20%y, 85%x, 5%y)
 

Attachments

  • Screenshot_2013-04-15-18-09-34.jpg
    Screenshot_2013-04-15-18-09-34.jpg
    27.1 KB · Views: 845
Top