Tags

, , , , , , , ,

Last week I was preparing for a presentation on Xamarin for the newly launched CIS Fortnight meetup where everyone from my department gather for some technical and soft skill sessions. After I got Student Indie subscription from Xamarin, I used to contribute to Xamarin.Android and just a night before the presentation I got a Facebook message from a Czech Republic girl asking how to count the lines of a TextView. I was also not much familiar with that TextView line counting thing. So I tried to go from the sketch to make a solution for her and send it.

That solution contains an app that simply has a Button and a TextView with some predefined text on it. The .Click event of the Button shows the linecount in a Toast

Firstly my Main.axml file goes like this

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <Button
        android:id="@+id/myButton"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello" />
    <TableLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:shrinkColumns="0">
        <TableRow>
            <TextView
                android:id="@+id/question"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Get the line number corresponding to the specified vertical position. If you ask for a position above 0, you get 0; if you ask for a position below the bottom of the text, you get the last line." />
        </TableRow>
    </TableLayout>
</LinearLayout>

Here, to create a TextView that supports text wrapping, I used TableLayout.

My MainActivity.cs is something similar to,

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
using System;

using Android.App;
using Android.Content;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.OS;
using Android.Text;

namespace CountLines
{
[Activity (Label = "CountLines", MainLauncher = true, Icon = "@drawable/icon")]
public class MainActivity : Activity
{
int count = 1;

protected override void OnCreate (Bundle bundle)
{
base.OnCreate (bundle);

// Set our view from the "main" layout resource
SetContentView (Resource.Layout.Main);

// Get our button from the layout resource,
// and attach an event to it
Button button = FindViewById<button> (Resource.Id.myButton);
TextView txtview = FindViewById (Resource.Id.question);</button>

button.Click += delegate {
int height = txtview.Height;
int scrollY = txtview.ScrollY;
Layout layout = txtview.Layout;
int lineCount = layout.GetLineForVertical (scrollY + height)+1;

Toast.MakeText(this,lineCount.ToString(),ToastLength.Long).Show();
};

}
}
}

Here, you can see from line 31 to 34 is the place where the magic happens. Height property gets the height of the View super class, here this works for TextView which is inherited from the View class. ScrollY returns the top edge of the displayed part of your view, in pixels. Here our TextView is not scroll able but still I have put this for future consideration. Now the magic method GetLineForVertical help us to get line for the sum of pixel count of the Height and ScrollY. That will be less then 1 of the real lines count so we can add 1 to get the real line count.

Thats all folks, if you are interested on my presentation I have done for the meetup, check it below.

&& you can see me here at your left side of the screen with the proud C# shirt from Xamarin.

Happy coding 🙂

Advertisements