How to Tell When a Layout Has Been Drawn

How can you tell when a layout has been drawn?

You can add a tree observer to the layout. This should return the correct width and height. onCreate() is called before the layout of the child views are done. So the width and height is not calculated yet. To get the height and width, put this on the onCreate() method:

    final LinearLayout layout = (LinearLayout) findViewById(R.id.YOUR_VIEW_ID);
ViewTreeObserver vto = layout.getViewTreeObserver();
vto.addOnGlobalLayoutListener (new OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
layout.getViewTreeObserver()
.removeOnGlobalLayoutListener(this);
} else {
layout.getViewTreeObserver()
.removeGlobalOnLayoutListener(this);
}
int width = layout.getMeasuredWidth();
int height = layout.getMeasuredHeight();

}
});

How do I know if a view has been drawn

since getVisibility() returns a predefined property and u don't want to set a listener; i think the only option is View.isShown().
i hope this helps.

How do i know, when view is drawn?

Try this method..It works fine for me.

ViewTreeObserver vto = YourView.getViewTreeObserver();
vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {

//do your stuff here

}
});

How do i know, when view is drawn?

Try this method..It works fine for me.

ViewTreeObserver vto = YourView.getViewTreeObserver();
vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {

//do your stuff here

}
});

How do I know if a view has been drawn

since getVisibility() returns a predefined property and u don't want to set a listener; i think the only option is View.isShown().
i hope this helps.

Detect when the application is completely drawn and visible

You are going to wrong direction I think.

you should try .post(Runnable action).

suppose linearLayout1 should be drawing at last, then you should try this...

linearLayout1.post(new Runnable() {

@Override
public void run() {// will be call when linearLayout1 will be completely loaded
// TODO Auto-generated method stub
//you can write your code here
}
});

Do something after activity's layout has been measured but not drawn on screen

You can use View.getViewTreeObserver() for that purpose. It lets you be notified when a layout has just happened or when a draw is about to occur.

How can I tell when a Layout (and all its child Views) has fully finished inflating?

You can override onFinishInflate() in your custom layout. This is called as the last phase of inflation, after all child views have been added.



Related Topics



Leave a reply



Submit