× {{alert.msg}} Never ask again
Get notified about new tutorials RECEIVE NEW TUTORIALS

Numbered Spannable Android EditText

Giorgio Saad
Aug 08, 2016
<pre><strong>int </strong><strong>oldLineCount </strong>= -<span style="color:#0000ff">1</span>; Boolean <strong>isMachineEdit </strong>= <strong>false</strong>; <strong>private </strong>TextWatcher <strong>tw </strong>= <strong>new </strong>TextWatcher() { <strong>public void </strong>afterTextChanged (Editable s) { } <strong>public void </strong>beforeTextChanged (CharSequence s, <strong>int </strong>start, <strong>int </strong>count, <strong>int </strong>after) { } <strong>public void </strong>onTextChanged (CharSequence s, <strong>int </strong>start, <strong>int </strong>before, <strong>int </strong>count) { <strong>if </strong>(!<strong>isMachineEdit</strong>) { <strong>isMachineEdit </strong>= <strong>true</strong>; Boolean stringCleaned = <strong>false</strong>; <strong>int </strong>lastSelection = <strong>tv</strong>.getSelectionEnd(); <strong>if </strong>(s.toString().contains(<strong>"</strong><strong>\n\n</strong><strong>"</strong>)) { s = s.toString().replace(<strong>"</strong><strong>\n\n</strong><strong>"</strong>, <strong>"</strong><strong>\n</strong><strong>"</strong>); stringCleaned = <strong>true</strong>; } String[] splitter = s.toString().split(<strong>"</strong><strong>\n</strong><strong>"</strong>); Log.<em>d</em>(<strong>"Splitter Length "</strong>, <strong>"" </strong>+ splitter.<strong>length</strong>); <strong>int </strong>currentLength = splitter.<strong>length</strong>; <strong>if </strong>(!stringCleaned) { <strong>if </strong>(currentLength == <strong>oldLineCount</strong>) { <strong>isMachineEdit </strong>= <strong>false</strong>; <strong>return</strong>; } } <strong>oldLineCount </strong>= currentLength; CharSequence result = <strong>""</strong>; <strong>int </strong>index = <span style="color:#0000ff">1</span>; <strong>for </strong>(<strong>int </strong>i = <span style="color:#0000ff">0</span>; i &lt; splitter.<strong>length</strong>; i++) { Log.<em>d</em>(<strong>"Index : " </strong>+ i, splitter[i]); <strong>if </strong>(!splitter[i].equals(<strong>""</strong>) &amp;&amp; !splitter[i].equals(<strong>"</strong><strong>\n</strong><strong>"</strong>)) { SpannableString s1 = <strong>new </strong>SpannableString(splitter[i] + <strong>"</strong><strong>\n</strong><strong>"</strong>); s1.setSpan(<strong>new </strong>NumberingSpan(<span style="color:#0000ff">0</span>, <span style="color:#0000ff">20</span>, index++), <span style="color:#0000ff">0</span>, splitter[i].length(), <span style="color:#0000ff">0</span>); result = TextUtils.<em>concat</em>(result, s1); } } <em>/*if (s.toString().length() &gt; 1) { </em><em> if (s.toString().substring(s.toString().length() - 1).equals("\n")) { </em><em> SpannableString s1 = new SpannableString(""); </em><em> s1.setSpan(new NumberingSpan(0, 20,index), 0, 0, 0); </em><em> result = TextUtils.concat(result, s1); </em><em> } </em><em> }*/ </em><em> </em><strong>if </strong>(stringCleaned) { <em>//. result = result.subSequence(0 , result.length() - 2); </em><em> </em>} <strong>tv</strong>.setText(result); <strong>if </strong>(!stringCleaned) { <strong>tv</strong>.setSelection(lastSelection); } <strong>else </strong><strong> </strong>{ <strong>tv</strong>.setSelection(result.length()); } <strong>isMachineEdit </strong>= <strong>false</strong>; } } }; </pre>

Get New Tutorials Delivered to Your Inbox

New tutorials will be sent to your Inbox once a week.

comments powered by Disqus