Skip to content

Instantly share code, notes, and snippets.

@jsaund
Last active September 9, 2016 06:44
Show Gist options
  • Select an option

  • Save jsaund/f4473ca3ddc4c51b2bef0eb6c508ddd9 to your computer and use it in GitHub Desktop.

Select an option

Save jsaund/f4473ca3ddc4c51b2bef0eb6c508ddd9 to your computer and use it in GitHub Desktop.
public class ImageFetcherAltActivity extends AppCompactActivity {
class WorkerThread extends Thread {
void fetchImage(String url) {
handler.sendEmptyMessage(MSG_SHOW_LOADER);
// network call to load image
handler.obtainMessage(MSG_SHOW_IMAGE, imageBitmap).sendToTarget();
}
}
class UIHandler extends Handler {
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case MSG_SHOW_LOADER: {
progressIndicator.setVisibility(View.VISIBLE);
break;
}
case MSG_HIDE_LOADER: {
progressIndicator.setVisibility(View.GONE);
break;
}
case MSG_SHOW_IMAGE: {
progressIndicator.setVisibility(View.GONE);
imageView.setImageBitmap((Bitmap) msg.obj);
break;
}
}
}
}
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
// prepare the view, maybe setContentView, etc
new WorkerThread().fetchImage(imageUrl);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment