Dynamically calculate view element size

I have an unknown number of TextView elements, some EditText elements, etc. that are all placed in one linear layout ... And I want to calculate the height of the layout at runtime so that I can get the exact pixel size for different resolution screens. Some editboxes may contain more text than others, so their size will be larger ... Is it possible to calculate the size of the layout at run time?

thank

+3
source share
2 answers

To get the height of the layout, you must write the code in the onWindowFocusChanged () method. First get the layout in the method onCreate(), and then write the following code.

@Override
public void onWindowFocusChanged(boolean hasFocus) {
    // TODO Auto-generated method stub
    super.onWindowFocusChanged(hasFocus);
    System.out.println("...111Height..."+mainLayout.getMeasuredHeight());
}

Thanks Deepak

+3

, :

String s = tv.getText().toString(); //tv is my textview name
float currentWidth = tv.getPaint().measureText(s);

, :

float phoneDensity = this.getResources().getDisplayMetrics().density;

currentWidth * phoneDensity, .

0

All Articles