Himanshu Dudhat
Himanshu Dudhat

Reputation: 1609

How to get maximum width of multiline TextView in android?

I am new in android and facing the problem to get the maximum width of the multiline TextView.

I used textview.getWidth() and textview.getMeasuredWidth() but both gives garbage value.

I used also textview.length(), but retuns zero.

Please note that i have not set the maximum width of the TextView.I want to get it runtime.

Upvotes: 0

Views: 1319

Answers (2)

kabuko
kabuko

Reputation: 36302

I would try to use getWidth(). If you want to do that though, you have to be careful when you call it. It only makes sense after the view hierarchy is laid out. That means that trying to check onCreate, onStart or even onResume will probably not get the results that you want. As an experiment, you can call it in a click handler of a button and you should see the expected value correctly being returned. I can't really say any more about where you should get it without knowing what you want to use it for.

Upvotes: 0

Pinki
Pinki

Reputation: 21929

TextView_name.setLayoutParams(new LayoutParams(250,LayoutParams.WRAP_CONTENT));

by using the above code we can set the width for textview dynamically

Upvotes: 1

Related Questions