Saturday, April 27, 2013

Embed WebView in Fragment

This exercise embed WebView in Fragment.

Embed WebView in Fragment


package com.example.androidwebviewfragment;

import android.os.Bundle;
import android.app.Activity;
import android.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView;
import android.webkit.WebViewClient;

public class MainActivity extends Activity {

 static public class MyWebViewFragment extends Fragment {
  
  WebView myWebView;
  final static String myBlogAddr = "http://android-er.blogspot.com";
  String myUrl;
  

  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container,
    Bundle savedInstanceState) {
   View view = inflater.inflate(R.layout.layout_webfragment, container, false);
   myWebView = (WebView)view.findViewById(R.id.mywebview);
   
   myWebView.getSettings().setJavaScriptEnabled(true);                
   myWebView.setWebViewClient(new MyWebViewClient());
   
   if(myUrl == null){
    myUrl = myBlogAddr;
   }
   myWebView.loadUrl(myUrl);
       
         return view;

  }
  
  private class MyWebViewClient extends WebViewClient {
         @Override
         public boolean shouldOverrideUrlLoading(WebView view, String url) {
          myUrl = url;
             view.loadUrl(url);
             return true;
         }
     }

  @Override
  public void onActivityCreated(Bundle savedInstanceState) {
   super.onActivityCreated(savedInstanceState);
   setRetainInstance(true);
  }

 }

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
 }

 @Override
 public void onBackPressed() {
  MyWebViewFragment fragment = 
    (MyWebViewFragment)getFragmentManager().findFragmentById(R.id.myweb_fragment);
  WebView webView = fragment.myWebView;
  
  if(webView.canGoBack()){
   webView.goBack();
  }else{
   super.onBackPressed();
  }
 }

}


Layout, activity_main.xml.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <fragment
        android:name="com.example.androidwebviewfragment.MainActivity$MyWebViewFragment"
        android:id="@+id/myweb_fragment"
        android:layout_height="match_parent"
        android:layout_width="match_parent" />

</RelativeLayout>


Layout of our fragment, layout_webfragment.xml.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <WebView
        android:id="@+id/mywebview"
        android:layout_height="match_parent"
        android:layout_width="match_parent" />

</LinearLayout>


Permission of "android.permission.INTERNET" is need.

- I can't implement using WebViewFragment, so this exercise extends Fragment, not WebViewFragment.
- In this implement, the WebView can load the last loaded address after orientation changed, but cannot keep the navigation history.

download filesDownload the files.

3 comments:

Unknown said...

Sir how can i add a refresh button over there .
i have tried this but creates error i think u know what to do



Code which contained error .......
..............................
Button button = (Button) findViewById(R.id.reload_btn);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
webView.loadUrl( "javascript:window.location.reload( true )" );
}
});

Anonymous said...

Thanks Man!!

I was trying to open webview in fragments with sliding menu. somehow other codes didnt work.

Yours was what i was looking for.

Thanks again

Siddhartha Kumar Pradhan said...

Thank you sir