0

I've a "text view" with 6 lines. I want to show the number of each line with a Toast when I click on each of them. When I use "Spans" it just shows the last line number. How can write a loop for Spans to return all lines number. Any other suggestions? Thanks for your help.

My Code:

public class Activity1 : AppCompatActivity
{
    TextView textView;
    int count = 6; //Number of lines
    int next;
    string txt;

    protected override void OnCreate(Bundle savedInstanceState)
    {
        base.OnCreate(savedInstanceState);
        SetContentView(Resource.Layout.activity_main);

        textView = FindViewById<TextView>(Resource.Id.textView1);
        txt = Resources.GetString(Resource.String.test);

        SpannableString ss = new SpannableString(txt);
        var clickableSpan = new MyClickableSpan();
        clickableSpan.Click += v => Toast.MakeText(this, "line : 1", ToastLength.Short).Show();
        ss.SetSpan(clickableSpan, 0, a, SpanTypes.ExclusiveExclusive);
        textView.TextFormatted = ss;
        textView.MovementMethod = new LinkMovementMethod();

        //For all lines
            //var clickableSpan1 = new MyClickableSpan();
            //var clickableSpan2 = new MyClickableSpan();
            //var clickableSpan3 = new MyClickableSpan();
            //var clickableSpan4 = new MyClickableSpan();
            //var clickableSpan5 = new MyClickableSpan();
       ...
    }
    private class MyClickableSpan : ClickableSpan
    {
        public Action<View> Click;

        public override void OnClick(View widget)
        {
            if (Click != null)
                Click(widget);
        }
        public override void UpdateDrawState(TextPaint textPaint)
        {
            textPaint.UnderlineText = false;
        }
    }
}

My Code for loop: From this answer

            for (int i = 0; i < ss.Length(); i = next)
            {
                // find the next span transition
                next = ss.NextSpanTransition(i, ss.Length(), Class.FromType(typeof(CharacterStyle)));
                // get all spans in this range
                int numOfSpans = 0;
                //CharacterStyle[] spans = ss.GetSpans(i, next, Class.FromType(typeof(CharacterStyle))); //return null!!!
                for (int j = 0; j < count; j++)
                {                        
                      numOfSpans++;
                }
                var clickableSpan = new MyClickableSpan();
                clickableSpan.Click += v => Toast.MakeText(this, "line : " + numOfSpans, ToastLength.Short).Show(); //return last number of lines : 6
                ss.SetSpan(clickableSpan, i, next, SpanTypes.ExclusiveExclusive);
                textView.TextFormatted = ss;
                textView.MovementMethod = new LinkMovementMethod();
            }
Theeyn
  • 5
  • 3
  • Do you mean you want to get the position of the textview text like the screenshot i pvovided in the link below? https://stackoverflow.com/a/62926729/11850033 – Wendy Zang - MSFT Dec 08 '20 at 09:20

1 Answers1

0

I am not sure what you want to achieve so please update your description:)

From what I can see the error is here textView.TextFormatted = ss; when you set equals to ss it will override the text for the textview in each step of the loop.

I've wrote you a code that textview has 6 spans and when you click on each of them it will show you the number of the span.

public class MainActivity : AppCompatActivity
    {
        TextView textView;
        int count = 6; //Number of lines
        string txt;
        List<string> spans;
        int startIndex = 0;

        protected override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);
            SetContentView(Resource.Layout.activity_main);

            textView = FindViewById<TextView>(Resource.Id.textView1);
            txt = Resources.GetString(Resource.String.test);

            //Split your txt on the number of spans - count??
            spans = new List<string>()
            {
                "Span 1" + System.Environment.NewLine,
                "Span 2" + System.Environment.NewLine,
                "Span 3" + System.Environment.NewLine,
                "Span 4" + System.Environment.NewLine,
                "Span 5" + System.Environment.NewLine,
                "Span 6" + System.Environment.NewLine
            };

            SpannableStringBuilder spannableString = new SpannableStringBuilder();

            for (int i = 0; i < count; i++)
            {
                ISpannable wordtoSpan = new SpannableString(spans[i]);

                var clickableSpan = new MyClickableSpan(ShowToastForSpan, i);

                spannableString.Append(spans[i], clickableSpan, SpanTypes.ExclusiveExclusive);
            }

            textView.TextFormatted = spannableString;

            textView.MovementMethod = LinkMovementMethod.Instance;
        }

        private void ShowToastForSpan(int spanNumber)
        {
            Toast.MakeText(this, $"Span number {spanNumber + 1} was clicked", ToastLength.Long).Show();
        }
    }

    class MyClickableSpan : ClickableSpan
    {
        private Action<int> _onSpanClicked;
        private int _spanNumber;
        Random rnd = new Random();

        public MyClickableSpan(Action<int> onSpanClicked, int spanNumber)
        {
            _onSpanClicked = onSpanClicked;
            _spanNumber = spanNumber;
        }

        public override void OnClick(View widget)
        {
            _onSpanClicked.Invoke(_spanNumber);
        }

        public override void UpdateDrawState(TextPaint textPaint)
        {
            textPaint.Color = Color.Argb(255, rnd.Next(256), rnd.Next(256), rnd.Next(256));
            textPaint.UnderlineText = false;
        }
    }
Dharman
  • 30,962
  • 25
  • 85
  • 135
Stefanija
  • 1,388
  • 2
  • 12
  • 25
  • Thanks a lot.. If I extend "span1" string such as "span 1 span 1 span 1 span 1span 1" and adjust " textView" font on 60sp, I will have 4 lines in "textView" and by clicking on each line it returns the same number. My txt string includes a short story which by changing it's font, the number of lines must be changed too... – Theeyn Dec 06 '20 at 11:55