Get width and height when view is been creating

Calling method getWidth(), getHeight() after onCreate return 0 as the view is not yet positioned correctly in layout. One of the easiest approach to get the width and height is postpone call:

valueTextView = (TextView)findViewById(R.id.include_value);
valueTextView.post(new Runnable() {
    @Override
    public void run() {
        int w = valueTextView.getWidth();
        int h = valueTextView.getHeight();
    }
});

 

 

Tags