Android Question Problem adjusting the width of a HorizontalScrollView

Glober

Member
Licensed User
Longtime User
I am puzzled why I cannot adjust the width of a HolizontalScrollView to make a clean fit every time. I have a varying number of columns of digits with a fixed width (10%x, but 80dip gives the same result) so that 10 columns fit on the screen. If there are more than 10 columns I need to scroll horizonally to see the additional columns. That all works fine. But I want to be able to adjust the width of the HorizontalScrollView so that the colums fit exactly no matter how many there are. I am working with a screen 1200x1848 /1.5 in portrait format.
My first attempt: I set the dip value of the HorizontalScrollView at NumberofColumns*80dip. 10 columns gives 800dip = 1200 pixels. But no, the value is to low, it doesn't scroll. By trial and error I found that 12 columns require a value of 1752dip - a perfect fit, so I change my calculation to NumberofColumns*146dip. But why should it be 146dip? For me it makes no sense. That works fine for 11,12,13 columns, but the more columns I have the more empty space I have at the end of the horizontal scroll. It seems that the value 146dip is too large after all, but if I reduce the figure to, say, 140dip then the 12th column is no longer accessible. I must have missed a vital point along the way, but no idea where. Can anyone help?
 

klaus

Expert
Licensed User
Longtime User
Can you post the code that fills the HolizontalScrollView.
Without seeing what you have done in your code it's difficult to give you a concrete advice.
I set the dip value of the HorizontalScrollView at NumberofColumns*80dip. 10 columns gives 800dip = 1200 pixels.
What value did you set to NumberofColumns*80dip ?
HorizontalScrollView1.Panel.Width = NumberofColumns*80dip should work if you set the Left properties of the columns correctly.
Sometimes adding a DoEvents after setting the width helps.
 
Upvote 0

Glober

Member
Licensed User
Longtime User
Thanks Klaus. Its a bit more complicated because I want to scroll horizontally and vertically. Bit I think I am halfway there now.
Actually as far as I can see the width of the HorizontalScrollView is defined when it is initialized and that is enough:
scr_HoriDimsTables.Initialize(HoriWidth*1dip,"") 'The HoriWidth is calculated using the layout values:
HoriWidth=(TotalColumns*Main.lv_Width/Main.lv_density/NoOfColumnsOnScreen 'Then I add the view:
Activity.AddView(scr_HoriDimsTables,0%x,0%y,100%x,100%y) 'Then I Initialize the VerticalScrollView the same way:
scr_VertiDimsTables.Initialize(TotalRows*Main.lv_Height/Main.lv_density/NoOfRowsOnScreen*1dip) 'Add the vertical view:
scr_HoriDimsTables.Panel.addview(scr_VertiDimsTables,0,0,300%x,100%y)

That works fine with density=2
For some reason I can't understand it doesn't work with density = 1.5
The Horiwidth is calculated correctly but when I initialize the HoriDimsTable and add the table values the table width is truncated at 66,6% of the width.
If I initialize the HoriDimsTable with the correct dip value itself (e.g. 960dip) then the display is correct. If I initialize using the variable HoriWidth or the formula the display is truncated (even though the HoriWidth gets the correct value 960)
I can do a workaround (if lv_density=1.5 then HoriWidth=HoriWidth*1.5) and that works but it seems totally illogical that it should work. Unfortunately I could not test the code on a device with Density 1

Any ideas?
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
Sorry , I don't understand what exactly you want to do ?
You speak about HorizontalScrollView and then you say you want to scroll in both directions !???
A HorizontalScrollView scrolls only horizontally !
As already requested post your project as a zip file.
Your explanations are very cofusing.
 
Last edited:
Upvote 0

Glober

Member
Licensed User
Longtime User
Sorry for the confusion. Here is some code which illustrates what I am trying to do and demonstrates the problem.
This code works more or less fine on my Galaxy Nexus (720x1184 density 2). The width fits the screen nicely for various values of the columns variable. The height leaves empty space at the bottom, which I presently don't understand, but I can live with that.
But if I use the same code on my Asus Transformer (1200x1848 density 1.5) the tables are truncated both in the width and the height. And, even more puzzling, if I increase the values by 1.5 (see the 2 lines in commentary) then I have a perfect fit (except for the space at the bottom). Somehow I'm missing something vital to understand why this works differently from how I would expect.
 

Attachments

  • Scrolltest.zip
    6.3 KB · Views: 317
Upvote 0

WAZUMBi

Well-Known Member
Licensed User
Longtime User
You could place a child panel inside the parent and based on the parent_touch event.
Then scroll the child panel (and anything contained in it) to the current touch location without having to use any scrollviews at all.
This will simulate a horizontal and vertical scroll.

I used this method in a math table app I made for my son a while back.
While graphically not a pretty app the scroll is functional and smooth.

https://play.google.com/store/apps/details?id=math.tables
 

Attachments

  • mathTable.zip
    374.3 KB · Views: 296
Upvote 0

Glober

Member
Licensed User
Longtime User
Thanks Klaus, that's just what I was looking for. Works perfectly! Seems I must pay more attention to the libraries........

Thanks also Wazumbi for the nice idea!
 
Upvote 0
Top