How to get the height of edit text dynamically?

There are a number of editing texts this image,

which I create dynamically using RelativeLayout (which is defined in XML). The first edit text is also defined in XML, and the others are based on this first edit text. The problem is that since you can see the last edit text is changing its height and unlike others, but how can I check it? How can I get the height of this edit text and add it to the next line on the screen?

+3


source to share


2 answers


you can try this to get the size dynamically:

getMeasuredHeight () : Please go to Google for more information.

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    EditText ed = (EditText) findViewById(R.id.edit1);
    ed.setTextSize(20);       
    ed.setText("here stackoverflow");       
    ed.measure(0, 0);
    int height = edit.getMeasuredHeight();

      

I usually use it for these reasons, read carefully, it will help you in the future:



The size of the view is expressed in width and height. The view actually has two pairs of width and height values.

The first pair is known as measured width and measured height. These dimensions determine how large the view wants to be inside its parent (see Layout for more details.) The measured dimensions can be obtained by calling getMeasuredWidth () and getMeasuredHeight ().

The second pair is simply known as the width and height, or sometimes the pattern width and the pattern height. These dimensions determine the actual size of the view on screen, drawing time, and after layout. These values ​​may, but need not be, differ from the measured width and height. The width and height can be obtained by calling getWidth () and getHeight ().

+4


source


You can try something like this.



EditText editText = (EditText) findViewById(R.id.your_view_id); 
int height = editText.getHeight(); // This will give height
EditText editTextLast = (EditText) findViewById(R.id.your_view_id);
editTextLast.setHeight(height); // this will set the height

      

+1


source







All Articles