1

I get following requirement.

  1. I need to build a tree with leaves placed left and right, from the screen top to the bottom. I can not put the leaves in a ListView because tow leaves will be in same offset.

  2. I don't know the height of the items.

  3. I need to pre-calculate the height of dynamic content, such as strings with different lengths.

QUESTION:

How can i pre-calculate the height of a sting which will be put in a TextView widget described as follows:

   <TextView
    android:id="@+id/note_content"
    android:layout_width="40sp"
    android:layout_height="wrap_content"
    android:maxLines="5" />

enter image description here

kvh
  • 2,118
  • 19
  • 29
  • You problem is better handled after the view has been drawn on screen. Use `ViewTreeObserver` to make changes to `TextView` placement. – Vikram Jul 19 '13 at 12:21
  • I am trying to find a way before i draw the view to the screen, will measure() work? – kvh Jul 19 '13 at 12:23
  • http://stackoverflow.com/questions/8345384/splitting-text-across-multiple-textviews-fragments-using-viewpager/17536992#17536992 – avimak Jul 19 '13 at 12:23

2 Answers2

0

Use this for calculate height and width your textview

    textView.getViewTreeObserver().addOnGlobalLayoutListener(
            new OnGlobalLayoutListener() {
                public void onGlobalLayout() {
                 h=textView.getHeight();
                 w=textView.getWidth();
        textView.getViewTreeObserver()
                            .removeGlobalOnLayoutListener(this);
    }

But in xml do not assign textview as a wrap content. Because it will give you value 0.

T_V
  • 17,440
  • 6
  • 36
  • 48
  • this event will triggered after i put the TextView to the view hierarchy. I need to know the height of TextView and i can layout it in my customized view group. – kvh Jul 19 '13 at 12:28
0

Create textView, set LayoutParams, set text, call it's measure method and read measured values. BTW you can measure text, not textView (see StaticLayout).

I think you better create your custom layout where you can implement all your "leaves measument" algorithms. Check this lesson. Custom layout is very easy )

Leonidos
  • 10,482
  • 2
  • 28
  • 37