In the android app, When I start the activity its showing Black Screen or application hang on for few seconds. I want till the Black Screen I want to show the Progress bar. I tried many times but could not do this.
String xml = "<?xml version=\"1.0\" encoding=\"utf-8\" ?>"
+ "<category><Id>" + catId + "</Id></category>";
StringBuilder resXML = new Connection().getResponseString("http://192.168.1.14/virtualMirror/productlisting.php", xml); // TODO URL change
if(!resXML.equals("")) {
XMLParser parser = new XMLParser();
Document doc = parser.getDomElement(resXML.toString()); // getting DOM element
NodeList nodeList = doc.getElementsByTagName("Product");
Intent intent = new Intent(this, ProductListing.class);
Bundle bundle = new Bundle();
bundle.putLong("CategoryId", catId);
bundle.putString("CategoryName", catName);
intent.putExtras(bundle);
startActivity(intent);
}
Use AsyncTask.
AsyncTask enables proper and easy use of the UI thread. This class allows to perform background operations and publish results on the UI thread without having to manipulate threads and/or handlers.
The AsyncTask executes everything in
doInBackground()inside of another thread, which does not have access to the GUI where your views are.preExecute()andpostExecute()offer you access to GUI before and after the heavy lifting occurs in this new thread, you can even pass the result of the long operation to postExecute() to then show any results of processing.And Use this class in your
onCreate()method.