2

There are a number of edit texts on this image,

which I create dynamically using RelativeLayout (whis is defined in XML). The first edit text is also defined in XML, and the other ones are created based on this first edit text. The problem is that, as you can see the last edit text is changing its height and it's unlike the other ones, 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?

rafsanahmad007
  • 23,683
  • 6
  • 47
  • 62

2 Answers2

3

you can try this to get size dynamically:

getMeasuredHeight() :please google it 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 because of this reasons, read carefully,it'll help you in future:

The size of a view is expressed with a width and a height. A view actually possess two pairs of width and height values.

The first pair is known as measured width and measured height. These dimensions define how big a view wants to be within 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 width and height, or sometimes drawing width and drawing height. These dimensions define the actual size of the view on screen, at drawing time and after layout. These values may, but do not have to, be different from the measured width and height. The width and height can be obtained by calling getWidth() and getHeight().

Shivam Sharma
  • 1,015
  • 11
  • 19
1

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
Adaikalaraj
  • 398
  • 3
  • 9
  • I've tried to use getHeight function, but it returns 0. In XML I set wrap_content as the width and height of the first edit text – Hidayət Rzayev Apr 21 '17 at 17:45