0

I got a horizontally oriented LinearLayout with an ImageView and a TextView. TextView has a text of 1- or 2-digits number.

What I need is to set text size of a TextView to be exactly same in a hight of an ImageView's image.

I know I can get ImageView's Image height , but how can I make textSize as high accordingly?

Vlad Alexeev
  • 2,072
  • 6
  • 29
  • 59

4 Answers4

1

View.getHeight():

Return the height of your view. Returns

Returns
    The height of your view, in pixels.

TextView.setTextSize(float):

Set the default text size to the given value, interpreted as "scaled pixel" units. This size is adjusted based on the current density and user font size preference.

Parameters
   size    The scaled pixel size.

This basically means you need to convert the height (px) to text height (sp):

float density = getResources().getDisplayMetrics().scaledDensity;
float textHeight = imageHeight / density;
Community
  • 1
  • 1
Simas
  • 43,548
  • 10
  • 88
  • 116
0

Give constant height-width to both(image view and textview)... or try scaleType="fitxy"...

Mayur R. Amipara
  • 1,223
  • 1
  • 11
  • 32
0

You can try:

float height = imageView.getHeight();
TextView tv = new TextView(context);
tv.setTextSize(height)

I'm not sure this will do exactly what you want, but it is the most obvious starting point I see.

Nathaniel D. Waggoner
  • 2,856
  • 2
  • 19
  • 41
0

well, I've found myself the following :

first we need to find a real height of an ImageView's Image - it's not as high as the ImageView, so it's explained well here : How to get the width and height of an android.widget.ImageView?

next we set the height of the text :

textView.setTextSize(TypedValue.COMPLEX_UNIT_PX, 
imageView.getMeasuredHeight()*(imageView.getMeasuredHeight()/
imageView.getMeasuredWidth()));

which means we are setting textSize in raw pixels, then we take height and multiply it with the picture's ratio of height to width so TextView's text size will be exactly high as the image

Community
  • 1
  • 1
Vlad Alexeev
  • 2,072
  • 6
  • 29
  • 59