Download the free trial version
Basic4android Video
Features
Tutorials and manuals
Showcase
Screenshots

Go Back   Android Development Forum - Basic4android > Basic4android > Basic4android Getting started & Tutorials
Documentation Wiki Register Members List B4P Search Today's Posts Mark Forums Read

Basic4android Getting started & Tutorials Android development starts here. Please do not post questions in this sub-forum.

ScrollView example

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 11-16-2010, 11:36 AM
Erel's Avatar
Administrator
 
Join Date: Apr 2007
Posts: 15,689
Awards Showcase
Basic4ppc Founder 
Total Awards: 1
Default ScrollView example

The ScrollView is a very useful container which allows you to show many other views on a small screen.
The ScrollView holds an inner panel view which actually contains the other views.
The user vertically scrolls the inner panel as needed.

In this example we will load images from the sdcard and add those to a ScrollView.



Adding views to a ScrollView is done by calling:
Code:
ScrollView.Panel.AddView(...)
In order to avoid "out of memory" errors we use LoadBitmapSample instead of LoadBitmap. LoadBitmapSample accepts two additional parameters: MaxWidth and MaxHeight. When it loads a bitmap it will first get the bitmap original dimensions and then if the bitmap width or height are larger than MaxWidth or MaxHeight the bitmap will be subsampled accordingly. This means that the loaded bitmaps will have lower resolution than the original bitmap. The width / height ratio is preserved.

The following code sets the inner panel height and adds an ImageView for each loaded bitmap:
Code:
    ScrollView1.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))
    iv.Background = bd 
'set the background of the image view.
    'add the image view to the scroll bar internal panel.
    ScrollView1.Panel.AddView(iv, 5dip5dip + i * 200dip, ScrollView1.Width - 10dip190dip)
Next
The code that is loading the bitmaps looks for jpg files under /sdcard/Images

If you want to run this program on the emulator you will first need to create this folder and copy some images to it.
This is done with the "adb" command, that comes with Android SDK.
Open a shell console (Windows Start - Run - Cmd).
Go to the sdk tools folder and issue:
Code:
c:\android-sdk-windows\tools> adb -e shell mkdir /sdcard/Images
The -e parameter tells adb to send the command to the only connected emulator.
The command is mkdir with the name of the folder.
Note that Android file system is case sensitive.

Now we need to copy some images to this folder.
This is done with the push command:
Code:
adb -e push "C:\temp" /sdcard/Images
This will copy all files under c:\temp to the Images folder.

The emulator is very slow compared to a real device. While on a real device 50 large images load in 2-3 seconds. It can take a long time for the emulator to load a few small images. I recommend you to copy 2 - 3 small images at most.

Also the experience of the ScrollView on the emulator cannot be compared to a real device (with capacitive screen).

The program is available here: http://www.basic4ppc.com/android/fil...ScrollView.zip
Reply With Quote
  #2 (permalink)  
Old 06-08-2011, 11:37 AM
Basic4ppc Veteran
 
Join Date: Dec 2010
Location: Colorado, USA
Posts: 204
Default Scrollview example

Is there a way to implement a scrollview where the user can drag his finger over the scrollview to make it scroll instead of touching a button? If so, is there an example of this? Thanks,
Reply With Quote
  #3 (permalink)  
Old 06-08-2011, 11:57 AM
Erel's Avatar
Administrator
 
Join Date: Apr 2007
Posts: 15,689
Awards Showcase
Basic4ppc Founder 
Total Awards: 1
Default

The buttons are just an additional way to move scroll the ScrollView. You can also scroll the view with your finger.
Reply With Quote
  #4 (permalink)  
Old 06-22-2011, 11:58 PM
Knows the basics
 
Join Date: Jun 2011
Location: Sydney, Australia
Posts: 78
Default

Tried this on the emulator....

"C:\Program Files\Android\android-sdk\platform-tools>adb -e shell mkdir /sdcard/images"

Received warning:
"mkdir failed for /sdcard/images, Read-only file system"


When I run it on a (new) phone, I also get a warning about the folder not being present.

Steve
Reply With Quote
  #5 (permalink)  
Old 06-23-2011, 06:04 AM
Erel's Avatar
Administrator
 
Join Date: Apr 2007
Posts: 15,689
Awards Showcase
Basic4ppc Founder 
Total Awards: 1
Default

Maybe the sdcard folder is named different on your device.
You can run this B4A command to see the folder:
Code:
Log(File.DirRootExternal)
Reply With Quote
  #6 (permalink)  
Old 07-27-2011, 07:15 PM
Cableguy's Avatar
Basic4ppc Expert
 
Join Date: Apr 2007
Location: N 41º11'30.30" W 8º39'46.60"
Posts: 2,344
Awards Showcase
Forum Contributer 
Total Awards: 1
Default

Im having some trouble working out ths view...

I added the scrollview and a label in the designer, an at runtime, because in te designer I cannot set the parent to be the ScrollView.panel, I use the ScollView.Panel.AddView method to make the label be a child of of the Scrollviews Panel...

I compile and when it runs it errors saying that : The specified child already has a parent.You must call removeview() on the parent's child first.


What?! Yeah, Right...
So I added the line:

Label1.RemoveView

And recompiled...It works like that!, This should be in the help somewere...because its not that clear!!
__________________
Paulo Gomes - Porto, Portugal - Living/Working in France
Mobile Device: Samsung Galaxy S, Android 2.3.4 CUstom ROM
Laptop: Toshiba NB100-130 (running on Win7Ultimate)

My Posts helped you? Consider Buying me a Porto Glass!

Last edited by Cableguy : 07-27-2011 at 07:21 PM.
Reply With Quote
  #7 (permalink)  
Old 07-27-2011, 10:08 PM
nad nad is offline
Knows the basics
 
Join Date: Jun 2011
Posts: 81
Default

Hello Erel,

Grats and many thanks for B4A. I have been really addicted and in love for the last months.

I am trying to select one of the ImageViews in the ScrollView and have it highlighted in some way, also would like to use NO_GRAVITY for the images. I have modified your script like this


iv.Initialize("iv")

instead of

iv.Initialize("") 'not interested in any events so we pass empty string.


And adding the following iv_click sub to try to select the specific imageview



Sub iv_Click
Dim send As View
Dim Bd As BitmapDrawable
Dim iview As ImageView

send=Sender

Log("Send.tag:"&send.Tag)

bd.Initialize(Bitmaps.Get(send.Tag))

iview=ScrollView1.Panel.GetView(send.Tag)
iview.Gravity=Gravity.NO_GRAVITY
iview.Color=Colors.Gray
iview.Background=bd
End Sub

The problem i have is that when i use NO_GRAVITY the background color for (i would like to have it gray) the selected imageview gets deleted (again black). Would your please give me a hint how can i highlight the selected imageview in a scrollview? I have been researching Klaus great examples on Scrollview but (maybe i have passed something) couldnt find how to highlight an image.

I would appreciate any help.

Thanks a lot and best regards,
Reply With Quote
  #8 (permalink)  
Old 07-28-2011, 06:32 AM
Erel's Avatar
Administrator
 
Join Date: Apr 2007
Posts: 15,689
Awards Showcase
Basic4ppc Founder 
Total Awards: 1
Default

When you set the Background property you actually overwrite the complete background. So the previous Color is ignored.

Maybe you can show a semitransparent panel over the image view to highlight it.
Reply With Quote
  #9 (permalink)  
Old 07-28-2011, 08:28 PM
nad nad is offline
Knows the basics
 
Join Date: Jun 2011
Posts: 81
Default

I will try that. Thanks a lot for you answer!
Reply With Quote
  #10 (permalink)  
Old 08-08-2011, 05:09 PM
ozgureffe's Avatar
Knows the basics
 
Join Date: Jun 2009
Posts: 61
Default what is this error about?

Code:
Compiling code.                         Error
Error parsing program.
Error description: The given key was 
not present in the dictionary.
Occurred on line: 
33
Sub LoadImages
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are On


All times are GMT. The time now is 10:18 AM.


Powered by vBulletin® Version 3.6.12
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0