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

-Java 소스
public class WebViewActivity extends Activity {
WebView mWeb;
ProgressBar progressBar;
String site;

/** 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);

site = "http://www.google.com";

mWeb = (WebView) findViewById(R.id.web1);
progressBar = (ProgressBar) this.findViewById(R.id.pro);
// mWeb.setWebViewClient(new WebViewClient());
WebSettings set = mWeb.getSettings();
set.setJavaScriptEnabled(true);
set.setBuiltInZoomControls(true);
mWeb.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
mWeb.setWebViewClient(new WebViewClient() {
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
};

public void onPageStarted(WebView view, String url,
android.graphics.Bitmap favicon) {
super.onPageStarted(view, url, favicon);
progressBar.setVisibility(View.VISIBLE);
};

public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
progressBar.setVisibility(View.INVISIBLE);
};

public void onReceivedError(WebView view, int errorCode,
String description, String failingUrl) {
super.onReceivedError(view, errorCode, description, failingUrl);
Toast.makeText(WebViewActivity.this, "로딩오류" + description,
Toast.LENGTH_SHORT).show();
};
});

mWeb.setWebChromeClient(new WebChromeClient() {
@Override
public void onProgressChanged(WebView view, int newProgress) {
progressBar.setProgress(newProgress);
}
});

mWeb.loadUrl(site);
}
}
-------------------------------------------------------------------------------------------------------
- xml 소스
 <?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">
<LinearLayout android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_weight="1"
android:orientation="vertical">

<ProgressBar android:id="@+id/pro"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="fill_parent" android:layout_height="4dp"
android:layout_gravity="top" android:max="100" android:paddingLeft="3dp"
android:paddingRight="3dp" />

<WebView android:id="@+id/web1" android:layout_width="fill_parent"
android:layout_height="fill_parent" android:layout_weight="1" />
</LinearLayout>
</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>
--------------------------------------------------------------------------------------------------------
- 실행화면


신고
Posted by Namseungil
JAVA/Android2012.02.08 15:23
-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>
-------------------------------------------------------------------------------------------------------
- 실행 화면
 

신고
Posted by Namseungil