본문 바로가기

JAVA/Android

안드로이드 WebView에 Progress바 달기 예제 따라하기

-WebView를 이용할때 얼마나 페이지가 넘어가는지를 확인 할 수 있게 Progress바를 달아보자.
-이 예제를 통해 하이브리드 App을 구현해 보자.

-Java 소스
public class WebViewActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
getWindow().requestFeature(Window.FEATURE_PROGRESS); // 프로그레스
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

String s = "http://seungngil.tistory.com";

WebView mWeb;
mWeb = (WebView) findViewById(R.id.web1);
mWeb.setWebViewClient(new WebViewClient());

WebSettings set = mWeb.getSettings();
set.setJavaScriptEnabled(true);
set.setBuiltInZoomControls(true);
mWeb.loadUrl(s);

mWeb.getSettings().setJavaScriptEnabled(true);
final Activity activity2 = this;
mWeb.setWebChromeClient(new WebChromeClient() {
public void onProgressChanged(WebView view, int progress) {
activity2.setProgress(progress * 100);
}
});
mWeb.setWebViewClient(new WebViewClient() {
public void onReceivedError(WebView view, int errorCode,
String description, String fallingUrl) {
Toast.makeText(activity2, "로딩오류" + description,
Toast.LENGTH_SHORT).show();
}
});
}
}
--------------------------------------------------------------------------------------------------------
-xml 소스
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello" />

    <WebView
        android:id="@+id/web1"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_weight="1" />

</LinearLayout> 
--------------------------------------------------------------------------------------------------------
- AndroidManifest.xml 
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="WebView.org"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="7" />

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

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:name=".WebViewActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>
-------------------------------------------------------------------------------------------------------
- 실행 화면