[android-developers] Re: Neeeeeed HELP......How to get the text's length after TextView.setText()?

2008-12-03 Thread Mark Murphy

sweety wrote:
 I did something like this within my code:
 
 public void test(){
 TextView tv = (TextView)findViewById(R.id.text_view);
 tv.setText(blablabla);
 int width = tv.getWidth();
 int height = tv.getHeight();
 }
 
 When test() is called(I am sure it's not called in the view or its
 parent's constructor), Both width and height is zero!!! How can get
 the width and height? Because the text might be quite long and be
 ellipsized by TextView so Paint.measureText() and Paint.getTextBound()
 can't fulfill this need.

Most likely, your views are not yet laid out when you are calling 
test(). Either delay your call to test() until the views are laid out 
(e.g., onResume() might be late enough), or manually request a layout() 
of your root view.

-- 
Mark Murphy (a Commons Guy)
http://commonsware.com

Android Training on the Ranch! -- Mar 16-20, 2009
http://www.bignerdranch.com/schedule.shtml

--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google
Groups Android Developers group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
[EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
-~--~~~~--~~--~--~---



[android-developers] Re: Neeeeeed HELP......How to get the text's length after TextView.setText()?

2008-12-03 Thread Dianne Hackborn
On Wed, Dec 3, 2008 at 9:31 AM, Mark Murphy [EMAIL PROTECTED] wrote:

 Most likely, your views are not yet laid out when you are calling
 test(). Either delay your call to test() until the views are laid out
 (e.g., onResume() might be late enough), or manually request a layout()
 of your root view.


I'm not sure what you mean by manually request a layout, but I think that is
probably something I would recommend against.

As a general rule, if you are trying to extract view width and height
information outside of the normal layout flow, you are probably doing
something wrong, and will break in various situations (such as if the system
needs to resize your window for some reason, like say an input method being
displayed).  The view hierarchy is designed to drive layout, with callbacks
into View and ViewGroup for the objects to work together as the layout
changes.  For things that depend on layout, you should hook there, so you
can correctly handle any time layout changes.

-- 
Dianne Hackborn
Android framework engineer
[EMAIL PROTECTED]

Note: please don't send private questions to me, as I don't have time to
provide private support.  All such questions should be posted on public
forums, where I and others can see and answer them.

--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google
Groups Android Developers group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
[EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
-~--~~~~--~~--~--~---