package zafar.learnandroid.webviewdemo;
import android.annotation.SuppressLint;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class MainFragmentActivity extends Fragment {
WebView myWebView;
final static String myBlogAddr = "http://learnandroidwithzafar.blogspot.in";
String myUrl;
public MainFragmentActivity() {
}
@SuppressLint("SetJavaScriptEnabled")
@Override public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_blog, container, false);
myWebView = (WebView) view.findViewById(R.id.home_webview);
myWebView.getSettings().setJavaScriptEnabled(true);
myWebView.setWebViewClient(new MyWebViewClient());
if (myUrl == null) {
myUrl = myBlogAddr;
}
myWebView.loadUrl(myUrl);
return view;
}
private class MyWebViewClient extends WebViewClient {
@Overridepublic boolean shouldOverrideUrlLoading(WebView view, String url) {
myUrl = url;
view.loadUrl(url);
return true;
}
}
}
Now Create xml layout as :
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<WebView
android:id="@+id/home_webview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true" />
</RelativeLayout>
Note: Don't Forget to give Internet permission in manifest file.
<uses-permission android:name="android.permission.INTERNET"/>
Screenshot
No comments:
Post a Comment