View instance without button click in android

79 Views Asked by At

I am using vidyo to implement video calls in my android app. How do I create a view instance on loading a new intent?

I have a button, and on clicking it I am able to do required action. It is defined as such.

public void Start(View v){
   vc = new Connector(videoFrame, Connector.ConnectorViewStyle.VIDYO_CONNECTORVIEWSTYLE_Default,
                16, "warning all@VidyoConnector info@VidyoClient", "", 0);
   vc.showViewAt(videoFrame, 0, 0, videoFrame.getWidth(), videoFrame.getHeight());
   }

But now, I want the same action to be performed without button click.

It should happen in onCreate(), but this is failing. I am unable to view my camera preview. What else can be done?

1

There are 1 best solutions below

4
Muhannad Fakhouri On BEST ANSWER

It's probably because your videoFrame is not layouted yet. You need to make sure that the view finished layouting before using getWidth and getHeight methods, to do that you might use e.g. OnglobalLayoutListener.

videoFrame.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
    @Override
    public void onGlobalLayout() {
// YOUR CODE GOES HERE
videoFrame.getViewTreeObserver().removeOnGlobalLayoutListener(this);
    }
});