1
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:orientation="vertical"
android:weightSum="3" >

<TextView
    android:id="@+id/info"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:ellipsize="end"
    android:gravity="bottom|center"
    android:maxLines="10"
    android:scrollbars="vertical"
    android:text="@string/hello_world"
    android:textSize="20sp" />

<SeekBar
    android:id="@+id/seek_bar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />

</LinearLayout>

I set a TextView and a seekbar, then set the listener of seekbar to append a line of text into the textview each time the value changes, as the textview get more and more text, the app totally slow down, I try to use things like maxlines or ellipsize to limit, to truncate the text, but seems not working.

Are there any build in things I can use?

here is the java code:

this.seek.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {

        @Override
        public void onStopTrackingTouch(SeekBar seekBar) {
            // TODO Auto-generated method stub
            info.append("\n" + "Stop" + "Current: " + seekBar.getProgress());
        }

        @Override
        public void onStartTrackingTouch(SeekBar seekBar) {
            // TODO Auto-generated method stub
            info.append("\n" + "Start" + "Current: " + seekBar.getProgress());
        }

        @Override
        public void onProgressChanged(SeekBar seekBar, int progress,
                boolean fromUser) {
            // TODO Auto-generated method stub
            info.append("\n" + "Changing" + "Current: " + seekBar.getProgress());
        }
    });
igonejack
  • 2,366
  • 20
  • 29
  • Maybe your string is too length. Try to set the length of the string it may be help and the advantage is it takes less times for loading limited length. – Amsheer Jul 17 '14 at 07:54
  • thank you, You mean it's too long? Yes I agree, and I want to find android build in thing to limit it. – igonejack Jul 17 '14 at 08:00
  • Too long means length is big.The normal way to set max lines is android:maxLines="10". That is you are using. If it won't work then try to set the limit of string. I'm just give comment for you it's not a perfect solution. Thjanks. – Amsheer Jul 17 '14 at 08:03
  • 1
    Why not check the length of the text in the seekbar listener and, if it is already too long, then just don't append the text? – Turix Jul 17 '14 at 08:04
  • @Amsheer `android:maxLines` just constrains how big the box is in the display, not how long the string is that it contains. For an `EditText`, `android:maxLength` constrains the length of the text. But since s/he is using a `TextView` and not an `EditText`, I think s/he will need to constrain the length programmatically in the seekbar listener. – Turix Jul 17 '14 at 08:06

3 Answers3

0

Probably the fact that you are using weight "1" collides with the fixed size you want to have. I would suggest to try using "wrap_content" as height value.

Hope it would solve you problem.

Edit:

Follow this link and try implementing the solution provided by this guy :D
Replace your textview with the one in the discussion.

android ellipsize multiline textview

This seems to be what you are looking for.

Community
  • 1
  • 1
AlexBalo
  • 1,258
  • 1
  • 11
  • 16
  • Can you append the whole xml? – AlexBalo Jul 17 '14 at 08:16
  • Ok, I put them on now – igonejack Jul 17 '14 at 08:26
  • Seems the weight_sum is not needed. Have you tried to do it programmatically? – AlexBalo Jul 17 '14 at 08:29
  • Unfortunately seems to be a bug and you are not able to limit the number. Why not trying using the solution provided by this guy? http://stackoverflow.com/questions/2160619/android-ellipsize-multiline-textview. You can use his class. The community is confirming the code as a working solution. :D – AlexBalo Jul 17 '14 at 09:26
0
    final int maxLength = 50;
    String mLongText = "I am very long sentence, but allowed maximum length is 50, trim me if I am more than 50chars";
    if(mLongText!="" && mLongText.length() > maxLength)
    {
        mLongText = mLongText.substring(0, maxLength);
    }
    mTextView.setText(mLongText);

EDIT:

You can set maximum number of lines by mTextView.setMaxLines(10);. This will show a max of 10 lines. In xml android:maxLines="10"

You have used android:ellipsize="end" which means that ... will appended at the end.

EX: ABCDEFGHIJKLMNOPQRSTUVWXYZ will be displayed as ABCDEFGHIJ...

android:ellipsize="start" will be displayed as ...QRSTUVWXYZ

Refer this to get more info

Community
  • 1
  • 1
VenomVendor
  • 15,064
  • 13
  • 65
  • 96
0

This example cut your text for 3 lines max

final TextView title = (TextView)findViewById(R.id.text);
title.setText("A really long text");
ViewTreeObserver vto = title.getViewTreeObserver();
vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {

    @Override
    public void onGlobalLayout() {
        ViewTreeObserver obs = title.getViewTreeObserver();
        obs.removeGlobalOnLayoutListener(this);
        if(title.getLineCount() > 3){
            Log.d("","Line["+title.getLineCount()+"]"+title.getText());
            int lineEndIndex = title.getLayout().getLineEnd(2);
            String text = title.getText().subSequence(0, lineEndIndex-3)+"...";
            title.setText(text);
            Log.d("","NewText:"+text);
        }

    }
});
Zar E Ahmer
  • 33,936
  • 20
  • 234
  • 300