Why doesn't setting text from onMeasure affect TextView?

There is a TextViewcertain size and whenever the text is set on it for too long, I would like to re-set it to some given value.

To do this, I redefine onMeasureand call setText. However, this does not affect the content TextView.

What am I missing?

EDIT: if you think this should be done in a completely different way - feel free to suggest

+5
source share
7 answers

onMeasure() . , , onMeasure() - , . 2 , , .

, , , ?

+4

View.

, . measure (int, int) .

, , setMaxWidth() setMaxHeight() setMaxLines() , - ,

0

onMeasure(int,int)

  • CALL super(int,int)
  • getMeasuredWidth() getMeasuredHeight

- :

void onMeasure(int a, int b){
    super(a,b);
    if(getMeasuredWidth()>bla)
        setText("default");
}
0

, , , , . , , .

ViewTreeObserver vto = ((TextView)findViewById(R.id.myTextView)).getViewTreeObserver();
vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {

            @Override
            public void onGlobalLayout() {

                if (null != ((TextView)findViewById(R.id.myTextView))) {
                    if (1 < ((TextView)findViewById(R.id.myTextView)).getLineCount()) {
                    ((TextView)findViewById(R.id.myTextView)).setTextSize(TypedValue.COMPLEX_UNIT_PX,
                              ((TextView)findViewById(R.id.myTextView)).getTextSize() - 2);
                    }
                }

           }
});

. -, . , 20 3,2- , 4,1- . , , , , - .

0

:

.length(), , . , onClickListener() TextView, Dialog .

, , , , , xml android:maxLines="your limit" android:textSize="some value" android:maxLength="@integer/mylimit" ( ). , ( )


- :

String myText = "bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla";
String clickForMore="...Click for more";
int limit =  myActivity.this.getResources().getInteger(R.integer.limit); //dont forget to declare it in your string.xml

, 3 , 2 ( int limit). TextView, . , ...

    myTextView.setVisibility(View.INVISIBLE);// <--- takes the space in the container
    if(clickForMore.length() < myText.length() && myText.length() > limit)// <--- avoiding negative place and checking if the entire text is inside the box or there are characters leftover.
    {
    String temp = myText.substring(0, limit-clickForMore.length());// <-- we cutting space from currently displayed (not original string)  to append the `clickForMore` string information.
    myTextView.setText(temp+clickForMore);
     myTextView.setVisibility(View.VISIBLE);
     myTextView.setOnTouchListener(new OnTouchListener() {

        @Override
    public boolean onTouch(View v, MotionEvent event) {
    // display the dialog with full text....
    return false;
                }

}); //<--- instantiate the click listener 
    }
    else
    {
       //the text fits the box and no need to slice it, just display it straight forward
    }

myText.length() > limit - , . .addTextChangeListener() (TextWatcher) afterTextChange() .

onClickListener() TextView, , , .


, . , , .

0

TextWatcher addTextChangedListener (TextWatcher watcher).

TextWatcher afterTextChanged(Editable s), , , .

EDIT: , , onMeasure : onMeasure , , , , TextView, onMeasure ...

AnyWay , , , Android TextView

0

You want to implement Textwatcherit in the method onTextChanged(...), call the TextView setEms (...) method , which will set the width TextViewto the width (nm), where n is the input for setEms(...).

...
TextView tv = (TextView) findViewById(R.id.my_text_view);
tv.addTextChangedListener(this);
...
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
    tv.setEms(count);
}
0
source

All Articles