Saturday, February 17, 2018

How to get the line number from string in Android?

Leave a Comment

How can I get line number form a string?
Suppose I have a string like this

String testString  = "Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis p"; 

And also I have a view that will show the string. In a different case, I want to get the line number first before rendering.
I want to get the line number from this string in Android. I got several ways to get the line number. But In every solution, we can get the line number after view rendering. But I want it before rendering of the view. I want to get the line number for 20 or more strings.
How can I get this any idea?

5 Answers

Answers 1

The line break calculation occures on layouting. Therefore:

findViewById(R.id.myTextView).addOnLayoutChangeListener(new View.OnLayoutChangeListener() {         @Override         public void onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) {             int lineCount = ((TextView)v).getLayout().getLineCount();         } }); 

Answers 2

Try this code:

Rect bounds = new Rect(); Paint paint = new Paint(); //Current size will be either the screen width if you give the width to match the screen or it will be the size what you gave for the textfield.  paint.setTextSize(currentSize); paint.getTextBounds(testString, 0, testString.length(), bounds);  int width = (int) Math.ceil((float) bounds.width() / currentSize); 

Now divide the width of text with the width of your TextView and get the total lines. This works before rendering of view.

Answers 3

You can use a android.text.Layout, or more specifically a android.text.StaticLayout. It's not a ViewGroup but a class used by views like TextView to manage the text layout. It can give you a lot of information about the (text) layout, like the position of each line, or conversely the offset in the text of a specific line.

Obviously it needs to know the available width, so unless you know it beforehand (e.g. your text uses the whole screen width) you still have to wait for a (view) layout.

    float density =  getResources().getDisplayMetrics().density;      // information about the font     TextPaint paint = new TextPaint();     paint.setTextSize(18 * density);      int width = (int) (300 * density);     Layout.Alignment alignment = Layout.Alignment.ALIGN_NORMAL;      StaticLayout layout = new StaticLayout(text, paint, width, alignment, 1, 0, false);      int lines = layout.getLineCount(); 

Answers 4

Not sure I understand your question entirely. But you could try using reflection to get line number of the method that wraps it.

Method m; // the method object ClassPool pool = ClassPool.getDefault(); CtClass cc = pool.get(m.getDeclaringClass().getCanonicalName()); CtMethod javassistMethod = cc.getDeclaredMethod(m.getName()); int linenumber = javassistMethod.getMethodInfo().getLineNumber(0); 

Could probably modify this for variables as well. Not sure if that solves your issue, or even what you are trying to do lol, but once compiled, getting line numbers is not as easy as reading raw files especially if you enable obfuscation.

Answers 5

@Override protected void onCreate(Bundle savedInstanceState) {     super.onCreate(savedInstanceState);     setContentView(R.layout.activity_main);      // Get the widgets reference from XML layout     mTextView = (TextView) findViewById(R.id.tv);     // Get TextView Line numbers     int lines = mTextView.getLineCount();     //your other code } 
If You Enjoyed This, Take 5 Seconds To Share It

0 comments:

Post a Comment