Skip to content

Instantly share code, notes, and snippets.

@simform-solutions
Created April 9, 2018 14:10
Show Gist options
  • Save simform-solutions/21c810c87883a9ba25764bd5c64b5874 to your computer and use it in GitHub Desktop.
Save simform-solutions/21c810c87883a9ba25764bd5c64b5874 to your computer and use it in GitHub Desktop.
Get text on each line into array from multiline textView.
import android.os.Build
import android.view.ViewTreeObserver
import android.widget.TextView
/**
*Created by Amit.Bhati on 08/04/18.
*/
class MultiLinesTextViewUtils(builder: Builder) {
private var getLines: GetLines = builder.getListener()!!
//Builder to set textView and listener
class Builder {
private var textView: TextView? = null
private var getLines: GetLines? = null
fun setTextView(textView: TextView): Builder {
this.textView = textView
return this
}
fun setListener(getLines: GetLines): Builder {
this.getLines = getLines
return this
}
fun getListener() = this.getLines
fun create(): MultiLinesTextViewUtils {
val multiLinesTextViewUtils = MultiLinesTextViewUtils(this)
multiLinesTextViewUtils.getLines(textView!!)
return multiLinesTextViewUtils
}
}
//Listener to get Lines from textView
interface GetLines {
fun setOnGeMultiLineListener(lines: ArrayList<CharSequence>)
}
fun getLines(view: TextView) {
val lines = ArrayList<CharSequence>()
view.getViewTreeObserver().addOnGlobalLayoutListener(object : ViewTreeObserver.OnGlobalLayoutListener {
override fun onGlobalLayout() {
//Remove listener once it get calls
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
view.getViewTreeObserver().removeOnGlobalLayoutListener(this);
} else {
view.getViewTreeObserver().removeGlobalOnLayoutListener(this);
}
val layout = view.layout
if (layout != null) {
// Get the number of lines currently in the layout
val count = layout.lineCount
// Get the text from the layout.
val text = layout.text
// Initialize a start index of 0 and iterate for all lines
var i = 0
var startIndex = 0
while (i < count) {
// Get the end index of the current line (use getLineVisibleEnd()
// instead if you don't want to include whitespace)
val endIndex = layout.getLineEnd(i)
// Add the subSequence between the last start index
// and the end index for the current line.
lines.add(text.subSequence(startIndex, endIndex))
// Update the start index, since the indices are relative
// to the full text.
startIndex = endIndex
i++
}
}
//Return arrayList to get all lines
getLines.setOnGeMultiLineListener(lines)
}
})
}
}
//Copy the below lines to recieve listener containing array of string
/*MultiLinesTextViewUtils.Builder()
.setTextView(textView)
.setListener(object : MultiLinesTextViewUtils.GetLines {
override fun setOnGeMultiLineListener(lines: ArrayList<CharSequence>) {
//You will get all lines separately into ArrayList.
}
})
.create()*/
@simform-solutions
Copy link
Author

Multi line textView with following lines-

device-2018-04-09-195351
OutPut array-
[Text in line one
, Text in line two
, Text in line three
, Text in line four]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment