1

I want to see the sum of textview's height and of textview's padding. I set padding with method:

textView.setPadding(0,x,0,x);

But if I use

textView.getHeight();

I will receive 0.

If I use

ViewGroup.LayoutParams params = (ViewGroup.LayoutParams) textView.getLayoutParams();
Log.d(TAG,params.height+"");

I will receive -2 that's impossible.

What have I to do?

Edit: This is the XML code

<TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/try"
            android:textColor="#fff"
            android:id="@+id/tv"
            android:gravity="center_horizontal" />

Edit2: I use this code for setting width of text view and it work correctely.

Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
final int width = size.x;
final int height = size.y;
ViewGroup.LayoutParams params = (ViewGroup.LayoutParams) textView.getLayoutParams();
params.width = width/2;

I don't want to set the height programmately because I set a padding. But I want to get the measure

Edit3:

I try to use

Rect bounds = new Rect();
textView.getPaint().getTextBounds(textView.getText(), 0,textView.getText().length(), bounds); 
bounds.height(); //This should give you the height of the wrapped_content

as Slartibartfast told me. But there is a problem:

Probably this is the measurement of text size... But it isn't the correct measurement of height of textview. I tried to sum this number and the 2 measure of padding and try to set:

params.height = bounds.height() + 2*x;

The view is smaller than the view without this line of code.

pippo15
  • 113
  • 1
  • 4
  • 17
  • -2 usually means you've used MATCH_PARENT or WRAP_CONTENT (these are assigned constants) as the height property. Can you paste the xml for the textview as well? – Akhil Cherian Verghese Feb 23 '15 at 09:24
  • Done edit! This is my code! Is possible to calculate the real height in a wrap_content view? – pippo15 Feb 23 '15 at 09:37

1 Answers1

0

Take a look at : How to retrieve the dimensions of a view?

Basically the size is known only after the layout is finished, one way is to create a custom text view and use onSizeChanged method

EDIT:

How about

Rect bounds = new Rect();
textView.getPaint().getTextBounds(textView.getText(), 0,textView.getText().length(), bounds); 
bounds.height(); //This should give you the height of the wrapped_content
Community
  • 1
  • 1
Vrashabh Irde
  • 14,129
  • 6
  • 51
  • 103
  • I have read that getHeight() is deprecated and I have to use getSize(Point x). But I don't understand how to use it for this situation. I use it for setting the width of TextView in the way that now I will edit – pippo15 Feb 23 '15 at 09:41