How do you measure a string

ChrisJohnson.8155ChrisJohnson.8155 USMember ✭✭
edited May 2014 in Xamarin.Android

I've looked all over the documentation and couldn't find anything.

How do you measure a specified string?

I need to know because I want to lay out many TextViews in a left-to-right/top-to-bottom fashion, just like a book. I couldn't find a premade layout so I'm using a RelativeLayout but I need to determine the X, Y position values for each TextView. I need to use multiple TextViews because each individual word needs to be able to be touched/hit and I need to know the index of the word that was touched/hit.


  • CheesebaronCheesebaron DKInsider, University mod

    So how I understand it is that if a page has 500 words you want to have 500 instances of TextView? Are you drunk?!

    I think your way of thinking about this problem is using a wrong approach. Instead of having 500 TextView instance you can use... 1! Yes 1 should be sufficient enough to solve your problem, then the job of determining which word is selected should happen inside the Touch event, where you can get the x and y positions of the touch.

    Here is my small test case:

    <?xml version="1.0" encoding="utf-8"?>
    <TextView xmlns:android=""
        android:text="Bacon ipsum dolor sit amet tongue ball tip ham hock, bresaola flank salami venison t-bone chicken hamburger chuck corned beef. Boudin pork chop beef ribs ribeye. Short loin ham bacon beef ribs sausage prosciutto landjaeger frankfurter chuck. Swine kevin chicken leberkas shoulder cow tongue pig. Ball tip shank pastrami salami pork belly, capicola swine spare ribs kielbasa meatball pork loin tenderloin. Meatball filet mignon boudin sausage bresaola brisket. Jerky drumstick shankle, pork loin ground round jowl tail hamburger tri-tip venison ham hock short loin. Sausage kevin turducken, tenderloin chuck jerky bacon beef. Rump fatback strip steak kevin hamburger turducken andouille short ribs ground round tongue shoulder. Beef ribs pork belly t-bone, prosciutto kevin venison rump. Sirloin capicola kevin, andouille ground round tenderloin cow."
        android:inputType="textMultiLine" />


    var textView = FindViewById<TextView>(Resource.Id.textView1);
    textView.Touch += (sender, args) =>
        var layout = textView.Layout;
        if (layout == null) return;
        var line = layout.GetLineForVertical((int)args.Event.GetY());
        var offset = layout.GetOffsetForHorizontal(line, (int)args.Event.GetX());
        // "offset" is the character index of the touched area
        Toast.MakeText(this, "Touched letter: " + textView.Text[offset], ToastLength.Short).Show();

    This gets the letter touched to a certain precision (depends on the touch slop etc.). From this you simply need to traverse left and right until you hit a space, comma or punctuation or any other stop character to get your word.

  • ChrisJohnson.8155ChrisJohnson.8155 USMember ✭✭

    Brilliant, thanks!

Sign In or Register to comment.