2

I want to display multi line texts using a textview. Per line only 20 characters should be displayed, How is is it possible.

I have set android:maxLines="5" android:maxLength="20" but it's not working.

here only 20 characters displayed other characters are not displayed. Any suggestions.

Rajeev Sahu
  • 1,732
  • 6
  • 26
  • 39
  • dont know about available functions but i achived this (10 chars/line) using three text boxes.. – jaimin Aug 01 '14 at 12:26
  • possible duplicate of [android: limit of 10 characters per line TextView](http://stackoverflow.com/questions/21364923/android-limit-of-10-characters-per-line-textview) – Pararth Aug 01 '14 at 12:27

4 Answers4

1

There is no in build function using which you can do this. Also android:maxLength is property used to set maximum number of characters allowed in text view, so pl remove that. To archive what you want you can follow the link given by user2450263. It is for 10 characters per line and you want is 20. So below is modified code from the link.

public String getTenCharPerLineString(String text){

    String tenCharPerLineString = "";
    while (text.length() > 20) {

        String buffer = text.substring(0, 20);
        tenCharPerLineString = tenCharPerLineString + buffer + "\n";
        text = text.substring(20);
    }

    tenCharPerLineString = tenCharPerLineString + text.substring(0);
    return tenCharPerLineString;
}
Community
  • 1
  • 1
Rajen Raiyarela
  • 5,526
  • 4
  • 21
  • 41
0

if you strongly want to achieve this then this is the way

use 3 EditText boxes(number of lines you want )
let say et1, et2, et3.

and add addTextChangedListener(new TextWatcher(){}); like this way

et1.addTextChangedListener(new TextWatcher(){

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count,
                int after) {
            // TODO Auto-generated method stub

        }
        @Override
        public void onTextChanged(CharSequence s, int start, int before,
                int count) {
            if(et1.getText().toString().length()==20){              
            et2.requestFocus();
            }
        }

        @Override
        public void afterTextChanged(Editable s) {
            // TODO Auto-generated method stub

        }

    });

et2.addTextChangedListener(new TextWatcher(){

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count,
                int after) {
            // TODO Auto-generated method stub

        }
        @Override
        public void onTextChanged(CharSequence s, int start, int before,
                int count) {
            if(et2.getText().toString().length()==20){              
            et3.requestFocus();
            }
        }

        @Override
        public void afterTextChanged(Editable s) {
            // TODO Auto-generated method stub

        }

    });

and then finally String result = et1.getText.toString()+ et2.getText.toString()+ et3.getText.toString()

jaimin
  • 563
  • 8
  • 25
0

Try this method to do it run-time rather from xml

public void doEllipsize(final TextView tv, final int maxLine) {
        ViewTreeObserver vto = tv.getViewTreeObserver();
        vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {

            @SuppressWarnings("deprecation")
            @Override
            public void onGlobalLayout() {
                ViewTreeObserver obs = tv.getViewTreeObserver();
                obs.removeGlobalOnLayoutListener(this);
                if (maxLine <= 0) {
                    int lineEndIndex = tv.getLayout().getLineEnd(0);
                    String text = tv.getText().subSequence(0, lineEndIndex - 3) + "...";
                    tv.setText(text);
                } else if (tv.getLineCount() >= maxLine) {
                    int lineEndIndex = tv.getLayout().getLineEnd(maxLine - 1);
                    String text = tv.getText().subSequence(0, lineEndIndex - 3) + "...";
                    tv.setText(text);
                }
            }
        });
    }

How to use?

textView = (TextView)findViewById(R.id.textView);
doEllipsize(tv,5);
Biraj Zalavadia
  • 28,348
  • 10
  • 61
  • 77
-1

Remove android:maxLength="20"

See this:

<TextView
android:id="@+id/textview"
android:layout_height="fill_parent"
android:layout_width="wrap_content"
android:maxLines="5"
android:lines="5"
android:text="Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat."></TextView>
Neal
  • 379
  • 4
  • 9