Reputation: 37
How can I do this using AsyncTask? This shows some html content in a TextView. I would like to make this with AsyncTask to display a "Loading" message because this way took too much time... :
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_stire_detail,
container, false);
// Show the dummy content as text in a TextView.
if (mItem != null) {
TextView noteView = ((TextView) rootView
.findViewById(R.id.stire_detail));
String EntireStire = "<b>" + mItem.title + " </b> <br> <img src='"
+ mItem.photo + "' > " + " <br><small>" + mItem.description
+ " <br> " + mItem.content + "</small>";
noteView.setText(Html.fromHtml(EntireStire, new MyImageGetter(),
null));
noteView.setMovementMethod(LinkMovementMethod.getInstance());
}
return rootView;
}
private class MyImageGetter implements Html.ImageGetter {
@Override
public Drawable getDrawable(String arg0) {
Bitmap bitmap;
try {
BitmapFactory.Options o = new BitmapFactory.Options();
o.inJustDecodeBounds = true;
BitmapFactory.decodeStream(
(InputStream) new URL(arg0).getContent(), null, o);
// The new size we want to scale to
final int REQUIRED_SIZE = 200;
// Find the correct scale value. It should be the power of 2.
int scale = 1;
while (o.outWidth / scale / 2 >= REQUIRED_SIZE
&& o.outHeight / scale / 2 >= REQUIRED_SIZE)
scale *= 2;
// Decode with inSampleSize
BitmapFactory.Options o2 = new BitmapFactory.Options();
o2.inSampleSize = scale;
// HKEY_CURRENT_USER\Software\Google\Chrome\Metro
bitmap = BitmapFactory.decodeStream(
(InputStream) new URL(arg0).getContent(), null, o2);
@SuppressWarnings("deprecation")
Drawable drawable = new BitmapDrawable(bitmap);
drawable.setBounds(0, 0, drawable.getIntrinsicWidth(),
drawable.getIntrinsicHeight());
return drawable;
} catch (Exception e) {
Drawable d = getResources().getDrawable(R.drawable.ic_launcher);
d.setBounds(0, 0, d.getIntrinsicWidth(), d.getIntrinsicHeight());
return d;
}
}
}
I have used the Master/Detail Flow template from Android.
Upvotes: 2
Views: 870
Reputation: 5055
Use Future
A Future represents the result of an asynchronous computation. Methods are provided to check if the computation is complete, to wait for its completion, and to retrieve the result of the computation.
Further information can be found here http://developer.android.com/reference/java/util/concurrent/Future.html
Upvotes: 0
Reputation: 39538
in your onCreate, you need to set the set to "loading.." or something, and start the task:
DownloadWebPageTask task = new DownloadWebPageTask();
task.execute(new String[] { "http://www.myUrl.com" });
And then:
private class DownloadWebPageTask extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... urls) {
//Download your pictures and prepare the text
return response;
}
@Override
protected void onPostExecute(String result) {
textView.setText(result);
//Better:
ImageView.setbackground(....)
}
}
}
Extra note
I would personally use an ImageView for the picture! textView is not optimal for what you need and will not scale easily.
Upvotes: 1
Reputation: 523
Is there a reason you're using a TextView instead of a WebView?
Based on what you're doing, I would say a webview is more appropriate, but if you're set on doing it this way, you should check out this answer.
Upvotes: 0
Reputation: 92
This answered question could help you: AsyncTask Android example And this is a good AsyncTask example: http://samir-mangroliya.blogspot.in/p/android-asynctask-example.html
Upvotes: 0