I am using a ProgressDialog when I call an API and I close it after getting a response from the server but I want this ProgressDialog to appear only for 20 seconds. If the API response does not appear in 20 seconds then this ProgressDialog will close and then I will close my Activity too and showing 1 message. This is my approach: 
myProgressDialog is defined in class level:
ProgressDialog myProgressDialog;
myProgressDialog= new ProgressDialog(FlyerActivity.this);
myProgressDialog.setMessage("Loading");
myProgressDialog.setCancelable(false);
myProgressDialog.show();
myAPIcallingMethod();
myAPIcallingMethod Implementation:
private void myAPIcallingMethod(){
    someAPICall;
    if (myProgressDialog.isShowing()){
        myProgressDialog.dismiss();
    }
}
Now here in someAPICall I want if API response is not coming after 20 seconds then this ProgressDialog will disappear.
                        
Use this