views:

82

answers:

1

How much space does a TextView take?

When I declare a TextView, is it possible to calculate how much space (height and width) it is going to take when actually rendered on the phone?

I have noticed that based on the different screen sizes of phones (or density), the TextView is rendered accordingly. I want to be able to calculate the exact height and width rendered.

Thanks Chris

A: 

Any View that is added and rendered has dimensions that are predetermined by the View's LayoutParams, which is basically an object that holds the x, y, width, and height. You're able to set the layout params manually when you add your TextView to your layout. Here's an example that adds a 30x40 ImageView to a RelativeLayout at (50,60).

// Some existing RelativeLayout from your layout xml
RelativeLayout rl = (RelativeLayout) findViewById(R.id.my_relative_layout);

ImageView iv = new ImageView(this);

RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(30, 40);
params.leftMargin = 50;
params.topMargin = 60;
rl.addView(iv, params);
Andy Zhang
So does that mean the TextView gets rendered as a 30X40 on every phone? Irrespective of the device's height/width/density?
Chris
Yes, that is correct. Which means it's usually better practice to determine your views' layouts with respect to the device's display dimensions instead of hardcoded values.
Andy Zhang