I am inflating a view using the call
final View view = mActivity.getLayoutInflater().inflate(R.layout.photo_view, null, false);
The view is being inflated for the sole purpose of turning it into a Bitmap.
The following call is not working. Now if I make the call inside a DialogFragment's onCreateView
it works -- so I know the code snippet itself works. But if I am just inflating the view for Bitmap conversion then the method public void onGlobalLayout()
is never called.
ViewTreeObserver viewTreeObserver = view.getViewTreeObserver();
if (viewTreeObserver.isAlive()) {
Log.i(TAG, "waiting for right time");
view.getViewTreeObserver().addOnGlobalLayoutListener(new
ViewTreeObserver.OnGlobalLayoutListener() {
@SuppressLint("NewApi")
@Override
public void onGlobalLayout() {
if (view.getLayoutParams().width <= 0 || view.getLayoutParams().height <= 0) {
int height = getDeviceHeight(mActivity);
int width = getDeviceWidth(mActivity);
LayoutParams layout = view.getLayoutParams();
layout.height = height;
layout.width = width;
view.setLayoutParams(layout);
}
doMyWork(view);
if (Utils.hasJellyBean()) {
view.getViewTreeObserver().removeOnGlobalLayoutListener(this);
} else {
view.getViewTreeObserver().removeGlobalOnLayoutListener(this);
}
}
});
}
I need this method because I want to have a non-zero dimension to create my bitmap from otherwise I get null for say, view.getDrawingCache()
. So how do I force the execution of ViewTreeObserver.OnGlobalLayoutListener