Arjun Chandrasekhara
Arjun Chandrasekhara

Reputation: 42

Get Height and Width of a child layout in contraintlayout

I have a constraint layout in which there is a child layout (frame_layout). The width and height are set as 0dp with match_constraint. How can we get the height and width of this frame_layout once the layout is loaded on the screen ? Using layoutparams gives 0dp. getWidth or getHeight again gives 0dp.

Upvotes: 0

Views: 1743

Answers (1)

Sunny
Sunny

Reputation: 14828

final FrameLayout layout = (FrameLayout) findViewById(R.id.YOUR_VIEW_ID);
ViewTreeObserver vto = layout.getViewTreeObserver(); 
vto.addOnGlobalLayoutListener (new OnGlobalLayoutListener() { 
    @Override 
    public void onGlobalLayout() {
        layout.getViewTreeObserver().removeOnGlobalLayoutListener(this); 
        int width  = layout.getMeasuredWidth();
        int height = layout.getMeasuredHeight(); 

    } 
});

Upvotes: 3

Related Questions