Android Question [Closed] scrollview with a single label...

Cableguy

Expert
Licensed User
Hi guys...

For some of you this will be a breeze to accomplish, but to me, expert at being no noob... it's kinda complicated.

So, I have a scrollview with a single label.
The purpose of this label is to act as a logger, so at each new entry, a new line is added...
I need to make sure that the last line of my label remains visible, that is to say, I need the scrollview to auto scroll to the last log entered.. but still be able to pull up or down to see the rest of the logs...

I have come to the conclusion that the best is to set both inner panel and label to single line height and at each entry, recalculate the panel/label height according to the new string height...

This is my theory... but I just can't get a grip to code it!

Is my line of thought correct? Or am I missing some auto scroll mechanism already present in the scrollview?
Ia there any beter/easier way to do this?
 

udg

Expert
Licensed User
Looking at ScrollView definition, it seems you've got a few alternatives.
FullScroll(true)
ScrollPosition=position
ScrollToNow(position)

First one takes you to the bottom of the sv.
 

thedesolatesoul

Expert
Licensed User
You can scroll the scrollview to wherever you want: http://www.b4x.com/android/help/views.html#scrollview
Or use ScrollView.FullScroll(True)

You will indeed start off with single line height, and as you add more lines, you change the height of both the label and scrollview inner panel.

Potentially you can use a listview as well which will be easier (as long as each line is a single line) and use ListView.setSelection to scroll to it.
 

Cableguy

Expert
Licensed User
Hi again,

I dont understand why I get this error...


B4X:
** Activity (main) Pause, UserClosed = false **
** Activity (main) Create, isFirst = true **
Error occurred on line: 204 (main)
java.lang.IllegalArgumentException: Layout: -1 < 0
   at android.text.Layout.<init>(Layout.java:141)
   at android.text.StaticLayout.<init>(StaticLayout.java:107)
   at android.text.StaticLayout.<init>(StaticLayout.java:93)
   at android.text.StaticLayout.<init>(StaticLayout.java:71)
   at android.text.StaticLayout.<init>(StaticLayout.java:51)
   at anywheresoftware.b4a.objects.StringUtils.MeasureMultilineTextHeight(StringUtils.java:32)
   at java.lang.reflect.Method.invokeNative(Native Method)
   at java.lang.reflect.Method.invoke(Method.java:515)
   at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:636)
   at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:302)
   at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:238)
   at java.lang.reflect.Method.invokeNative(Native Method)
   at java.lang.reflect.Method.invoke(Method.java:515)
   at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:121)
   at b4a.example.main.afterFirstLayout(main.java:98)
   at b4a.example.main.access$100(main.java:16)
   at b4a.example.main$WaitForLayout.run(main.java:76)
   at android.os.Handler.handleCallback(Handler.java:733)
   at android.os.Handler.dispatchMessage(Handler.java:95)
   at android.os.Looper.loop(Looper.java:136)
   at android.app.ActivityThread.main(ActivityThread.java:5586)
   at java.lang.reflect.Method.invokeNative(Native Method)
   at java.lang.reflect.Method.invoke(Method.java:515)
   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1268)
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1084)
   at dalvik.system.NativeStart.main(Native Method)
All I have is declared a StingUtils to get the required height:

B4X:
Label6.Height = LogHeight.MeasureMultilineTextHeight(Label6, Label6.Text)
label6 has the inner scrollview panel as parent

[EDIT]
After searching a bit more, I found that for some strange reason, the inner panel is NOT automatically initiated, and for this reason, panel.width nor panel.height have usable values, allthoug NO ERROR is thrown. Its only with specialised objects like StringUtils that a "Not so clear" error is seen by B4A.
The solution is to use the base ScrollView width and height values, if you are trying to set relative positions.
 
Last edited:
Top