Archive

Posts Tagged ‘url’

Load WebView with ProgressDialog

July 31, 2011 10 comments

It’s very useful to determine the status while loading a web page. Here a sample to show ProgressDialog to track when WebVIew done with loading URL.

public class MainActivity extends Activity {

	WebView mWeb;
	ProgressDialog mProgress;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // no need to use title bar
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        // set webview as main content only
        mWeb = new WebView(this);
        setContentView(mWeb);
        // set Javascript
        WebSettings settings = mWeb.getSettings();
        settings.setJavaScriptEnabled(true);
        // the init state of progress dialog
        mProgress = ProgressDialog.show(this, "Loading", "Please wait for a moment...");

        // add a WebViewClient for WebView, which actually handles loading data from web
        mWeb.setWebViewClient(new WebViewClient() {
        	// load url
        	public boolean shouldOverrideUrlLoading(WebView view, String url) {
        		view.loadUrl(url);
        		return true;
        	}

        	// when finish loading page
        	public void onPageFinished(WebView view, String url) {
        		if(mProgress.isShowing()) {
        			mProgress.dismiss();
        		}
        	}
        });
        // set url for webview to load
        mWeb.loadUrl("http://www.cnn.com");
    }
}

Hope it helps!

Cheers,
Pete Houston

Grab image from URL

June 10, 2011 22 comments

Screenshot

Screenshot

Above is the sample result of grabbing an image from URL to display in an ImageView control.

Let’s roll!

Create new Android Project

Project Name: DownloadImageDisplay

Build Target: Android 2.3.3

Application Name: Download Image Display

Package Name: pete.android.study

Create Activity: MainActivity

Min SDK: 10

A – The layout

Pretty much simple:

+ One text view to display the URL of the image, if

+ One image view to display the

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >

    <TextView
	    android:id="@+id/txtUrl"
	    android:layout_width="fill_parent"
	    android:layout_height="wrap_content"
    />

    <ImageView
    	android:id="@+id/imgView"
    	android:layout_width="fill_parent"
    	android:layout_height="wrap_content"
    />

</LinearLayout>

B – Coding


package pete.android.study;

import java.io.InputStream;
import java.net.URL;

import android.app.Activity;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.TextView;

public class MainActivity extends Activity {

	// declare internal using controls
	private TextView txtUrl;
	private ImageView imgView;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        // set url text
        String url = "http://www.droidviet.com/images/up_img/dalats_com_up12_09_2010_11_091610742910.archos-android.jpg";
        txtUrl = (TextView)findViewById(R.id.txtUrl);
        txtUrl.setText(url);

        // load image view control
        imgView =(ImageView)findViewById(R.id.imgView);

        // grab image to display
        try {
        	imgView.setImageDrawable(grabImageFromUrl(url));
        } catch(Exception e) {
        	txtUrl.setText("Error: Exception");
        }
    }

    private Drawable grabImageFromUrl(String url) throws Exception {
    	return Drawable.createFromStream((InputStream)new URL(url).getContent(), "src");
    }
}

C – Note

– In order to make application to be able to connect to Internet and download files, you need to set uses-permission. Open file: AndroidManifest.xml and add this line:

<uses-permission android:name="android.permission.INTERNET"></uses-permission>

– The above code is not the best implementation

– Due to just giving an example of grabbing image, so I don’t care what kind of exception it might catch. You can find it yourselves, it’s pretty much easy to figure out.

D – What I Learned?

– Know how to get an image from an URL (without caching file)

E – Final Words

– The code seems to explain itself, and is very simple.

– Don’t mind to write a comment whatever you like to ask, to know, or to suggest, recommend.

– Hope you enjoy it!

Cheers,

Pete Houston

Categories: Tutorials Tags: , , , ,