I am going to share very simple and important code for web-view in android which will open inside the application not on default browser. Please don't forget to add Internet permission in manifest file.
1-Print Screen:
2-WebViewDemoActivity.java
package com.web.demo;
import android.app.Activity;
import android.os.Bundle;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class WebViewDemoActivity extends Activity {
WebView webView;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
webView = (WebView) findViewById(R.id.yourwebview);
// force web view to open inside application
webView.setWebViewClient(new MyWebViewClient());
openURL();
}
private void openURL() {
webView.loadUrl("http://google.co.in");
}
private class MyWebViewClient extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
}
}
3-main.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" >
<WebView android:id="@+id/yourwebview"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
4-manifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.web.demo"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="15" />
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:name=".WebViewDemoActivity"
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>
good
ReplyDeleteThanks for your positive comment..
ReplyDeleteThanks Manish, I've tried this but my remote css & javascripts doesn't loaded?
ReplyDeleteHi Manish,
ReplyDeleteThis is Amod Chaurasia from Pune. I found your post as very effective. I am having similar type of requirement in my project. Can you please help me out by sending zip code to me? My mail ID is amod2anand@gmail.com. Thanks in advance.
I have lost my repository, please copy paste from above.
Delete