6

I'm working on a custom view to resize the text inside a textview so it'll fit (i don't want to ellipsize).

The trouble i'm having is that when changing the text size, the textview itself is not remeasured. I've been looking at the source and saw that the setTextSize() is calling the following:

nullLayouts();
requestLayout();
invalidate();

So it should've remeasured. This could be a bug because it works fine on 2.3 and not on 1.6, 1.5 and 2.1 emulators.

Here's piece of the code, note that textview is extended:

@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
    if(w == 0 && h == 0) {
        setTextSize(TypedValue.COMPLEX_UNIT_PX, defaultSize);
    }

    updateView();
}

private void updateView() {
    int viewWidth = getViewWidth();
    float textWidth = getTextWidth();

    float textSize = textSize();
    while(textWidth > viewWidth && textSize >= MIN_TEXT_SIZE) {

        setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize-1);

        textSize = getTextSize();

        textWidth = getTextWidth();
    }
}

Can anyone give me hint in the right direction to solve this issue?

Nick
  • 1,733
  • 1
  • 14
  • 24
  • it probably has nothing to do with your problem, but why there are a `textSize()` and a `getTextSize()`? – bigstones Apr 14 '11 at 12:05
  • however, I had too problems with custom views that needed to be re-laid out. It usually turned out to be working if I run Hierarchy Viewer and forced a layout request from there. – bigstones Apr 14 '11 at 12:13
  • 1
    Yeah i noticed that as well, hierarchy viewer forces the acitivity to relayout everything. Also, i kinda found a semi solution: i call measureChildren() in the onSizeChanged() – Nick Apr 14 '11 at 14:56
  • this is how I coped with it: [my Custom View gets drawn only when added in onCreate](http://stackoverflow.com/questions/4163918/my-custom-view-gets-drawn-only-when-added-in-oncreate) (includes a link to the bug report I've found) – bigstones Apr 14 '11 at 15:34

1 Answers1

-3

In your xml file write the following code.

<TextView
  android:layout_height="40dip"
  android:layout_width="100dip"/>

In short, in your xml file set the fixed size of your TextView.

Kartik Bhatt
  • 924
  • 4
  • 16
  • 33
  • 1
    This is not really a solution to the problem here. Setting fixed size won't resize the text it will just truncate it. – Nick Aug 22 '12 at 11:45