Reputation: 303
In Android development, I am loading a drawable from a url with this code:
public Drawable getImageFromURL(String sURL) {
Object content = null;
try {
URL url = new URL(sURL);
content = url.getContent();
} catch (Exception ex) {
ex.printStackTrace();
}
InputStream is = (InputStream) content;
return Drawable.createFromStream(is, "src");
}
But I want to display this ProgressBar while it loads the drawable:
final AlertDialog alertDialog = new AlertDialog.Builder(this).create();
progressBar = ProgressDialog.show(TabMenu.this, getString(R.string.name), getString(R.string.specials));
progressBar.setIcon(R.drawable.icon_template);
How do I do that? I call getImageFromURL() in the onCreate() methode of the Activity. Thanks for your help!
Upvotes: 2
Views: 984
Reputation: 56
private Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
// do something
switch (msg.what){
case 1:
//ALL FUNCTION(S) -----------------------------
//-----------------------------------------------------
//-----------------------------------------------------
p_dialog.dismiss();
break;
// if error
case 0:
Toast.makeText(aktiviti.this, "Error", Toast.LENGTH_SHORT).show();
p_dialog.dismiss();
break;
}
}
};
Button new_button = (Button) findViewById(R.id.new_button);
new_button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
p_dialog = ProgressDialog.show((activity_name).this, "", "Loading Image...");
new Thread(new Runnable() {
public void run() {
processThread();
}}).start();
}
private void processThread() {
try{
handler.sendEmptyMessage(1);
}
catch (Exception e){
handler.sendEmptyMessage(0);
}
}
}
});
Good Luck!
Upvotes: 1
Reputation: 5322
You need to download the image in a new thread. You can use a AsyncTask for simplicity as in this example which implements also the progressbar
Upvotes: 1