Vlad Alexeev
Vlad Alexeev

Reputation: 2214

setTextSize accordingly to ImageView's image size

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?

Upvotes: 0

Views: 590

Answers (4)

Vlad Alexeev
Vlad Alexeev

Reputation: 2214

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

Upvotes: 0

Simas
Simas

Reputation: 44118

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;

Upvotes: 1

Nathaniel D. Waggoner
Nathaniel D. Waggoner

Reputation: 2886

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.

Upvotes: 0

Mayur R. Amipara
Mayur R. Amipara

Reputation: 1223

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

Upvotes: 0

Related Questions