Android Question Return Text of each line by reflection lib

Discussion in 'Android Questions' started by ArminKH, Jan 26, 2015.

  1. ArminKH

    ArminKH Well-Known Member

    Hi all
    I want to return the text of each line in a view(like label) by using reflection library.(i can do this by while--loop and some calculation but looking for a faster way to do it)
    Pls help me to find out the best method of doing this just by reflection library
    Last edited: Jan 26, 2015
  2. stevel05

    stevel05 Expert Licensed User

    Why must you use the reflection library? The easiest way is to use Regex.Split. Split the text on CRLF and you end up with an array with one line per index.
    Dim Lines() As String = Regex.Split(CRLF,Label.Text)
    Peter Simpson and ArminKH like this.
  3. ArminKH

    ArminKH Well-Known Member

    Because each line is not finished by a crlf
    Maybe your way is true just to find paragraphes
  4. stevel05

    stevel05 Expert Licensed User

    OK understood, an interesting one. Try this:

    Sub Globals
    'These global variables will be redeclared each time the activity is created.
        'These variables can only be accessed from this module.

    Private Label1 As Label
    End Sub

    Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:

        Label1.Text = 
    "test of extractable text, see if we can get single lines"
    End Sub

    Sub Activity_Resume

    End Sub

    Sub Activity_Pause (UserClosed As Boolean)

    End Sub

    Sub DoMeasure
    Dim Lines() As String = GetText(Label1)
    Log("No Lines " & Lines.Length)
    For Each S As String In Lines
    End Sub
    Sub GetText(Source As JavaObjectAs String()

    Dim Text As String = source.RunMethod("getText",Null)

    Dim Layout As JavaObject

    Dim LineCount As Int = Source.RunMethod("getLineCount",Null)
    Dim Lines(LineCount) As String

        Layout = Source.RunMethod(

    Dim Start,LineEnd As Int
        Start = 
    For i = 0 To LineCount - 1
            LineEnd = Layout.RunMethod(
            Lines(i) = Text.SubString2(Start,LineEnd)
            Start = LineEnd
    Return Lines
    End Sub
    CallSubDelayed is used as it is not possible to interrogate the layout while it is being created.

    You just need to create a Layout in the designer with an EditText1 and load it. It also works with labels.

    Based on code found here:
    Last edited: Jan 26, 2015
  5. ArminKH

    ArminKH Well-Known Member

    this is exactly the best answer to my question
    Thank :)
    The javaobject lib has not any method for access to text of each line directly?liked "getLineText"???
    And in where page i can see all methods of reflection or javaobject? I think the methods are secret for beginners :)
    Last edited: Jan 26, 2015
  6. stevel05

    stevel05 Expert Licensed User

    JavaObject, like reflection, only has methods that allow it to be used as a gateway to access other methods that are in Java. With it you can access a large percentage of the underlying Java methods. So you need to have an idea of what you want to achieve, find some Java code that does what you want, then set about finding out how to access it through JavaObject or Reflection.
    ArminKH likes this.
  7. ArminKH

    ArminKH Well-Known Member

    Ok understood
    Tnx again 4 your quick response mr estevel :)
  8. Mahares

    Mahares Well Known Member Licensed User

    Since @stevel05 clever code is intended to break a label into several lines, I added a small code to it to accommodate the height of the label and show all the text once the label is broken up into multilines:
    Dim StrUtils As StringUtils  'in Globals . Require StringUtils Lib

    "","SetTextHeight")   ' In Activity_Create  below CallSubDelayed(Me,"DoMeasure")

    Sub SetTextHeight
        Label1.Height = StrUtils.MeasureMultilineTextHeight(Label1, Label1.Text)
    End Sub
    erfan and ArminKH like this.
  9. ArminKH

    ArminKH Well-Known Member

    Can u please edit above codes and convert that to reflection library?
    Because my app crashed(force close) on android 2.1 and 2.3
  10. ArminKH

    ArminKH Well-Known Member

    If is possible 4 u please test that on android 2.1 :-(
  11. stevel05

    stevel05 Expert Licensed User

    What error are you getting? There is no point in converting a java library to Reflection, it will have the same result. Sorry, I don't have a 2.1 device to test it on.
    ArminKH likes this.
  12. ArminKH

    ArminKH Well-Known Member

    please see attached project
    all things are okay on android above 3 , but on android 2.1 and 2.3 the error is"java.lang.reflect.InvocationTargetException"
    please test it on emulator

    Attached Files:

      File size:
      12.5 KB
  13. ArminKH

    ArminKH Well-Known Member

  14. ArminKH

    ArminKH Well-Known Member

    full error is


    at android.text.StaticLayout.getLineStart(
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(
    at anywheresoftware.b4j.object.JavaObject.RunMethod(
    at b4a.example.main._save(
    at b4a.example.main._button1_click(
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(
    at anywheresoftware.b4a.BA.raiseEvent2(
    at anywheresoftware.b4a.BA.raiseEvent2(
    at anywheresoftware.b4a.BA.raiseEvent(
    at anywheresoftware.b4a.objects.ViewWrapper$1.onClick(
    at android.view.View.performClick(
    at android.view.View.onTouchEvent(
    at android.widget.TextView.onTouchEvent(
    at android.view.View.dispatchTouchEvent(
    at android.view.ViewGroup.dispatchTouchEvent(
    at android.view.ViewGroup.dispatchTouchEvent(
    at android.view.ViewGroup.dispatchTouchEvent(
    at android.view.ViewGroup.dispatchTouchEvent(
    at android.view.ViewRoot.handleMessage(
    at android.os.Handler.dispatchMessage(
    at android.os.Looper.loop(
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(
    at dalvik.system.NativeStart.main(Native Method)
    Caused by: java.lang.ArrayIndexOutOfBoundsException
    ... 33 more
  15. DonManfred

    DonManfred Expert Licensed User

    The error is
    Caused by: java.lang.ArrayIndexOutOfBoundsException
    ArminKH likes this.
  16. ArminKH

    ArminKH Well-Known Member

    ok this error happen because java object not return any value on android 2.1
    what is the solution?please see attached project
    every thing is perfect on +3 android devices
  17. DonManfred

    DonManfred Expert Licensed User

    then i belive this api does not exists on that device (android version) or it maybe needs to be called in another way.
    Maybe you can found more info about it on the google developer site
    ArminKH likes this.
  18. stevel05

    stevel05 Expert Licensed User

    Have you tried this on a real device? I had a 2.1 test device on the emulator ( which I don't use anymore as it's slow and inconsistent), it has a problem getting the text length, it is returned as a silly figure: 1136198696

    Try putting:

    After the call to getText. It's the same if you call Label1.Text.Length.

    After changing the calls to use reflection, If I then run an Android 3 device in the emulator and run the app on there it works, if I then run it on the 2 device while the 3 device is still loaded, it works.

    Bottom line: Try it on a real device!
    ArminKH and DonManfred like this.
  19. DonManfred

    DonManfred Expert Licensed User

    A real device should always be preffered in comparison to the fucking instable and slow android emulator.
    stevel05 and ArminKH like this.
  20. stevel05

    stevel05 Expert Licensed User

    If you want to try it the code with reflection is:

    Private Sub Save
    Dim Source=Label1 As JavaObject
    Dim Text As String=Source.RunMethod("getText",Null)
    Dim Layout As JavaObject=Source.RunMethod("getLayout",Null)
    Dim LineCount As Int=Source.RunMethod("getLineCount",Null)
    Dim TextByLine(LineCount) As String

    Dim LineStart,LineEnd As Int
    Dim R As Reflector
        R.Target = Layout
    Dim A As Int
    For A=0 To LineCount - 1
    "getLineStart",A,"")'Layout.RunMethod("getLineStart",Array As Object(A))
                LineEnd=R.RunMethod2("getLineEnd",A,"")'Layout.RunMethod("getLineEnd",Array As Object(A))
    End Sub
    ArminKH likes this.
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice