3

I want to load a webview in a recyclerview.

What I've tried opens web page in a default browser(say chrome).

Could anyone help me to do it?

B001ᛦ
  • 2,036
  • 6
  • 23
  • 31
Entertainment S
  • 113
  • 1
  • 1
  • 10

2 Answers2

3

Inherited WebView from Android library:

import android.content.Context;
import android.util.AttributeSet;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;


public class MyWebView extends WebView {

  public MyWebView(Context context) {
    super(context);
    initDefaultSetting();
  }

  public MyWebView(Context context, AttributeSet attrs) {
    super(context, attrs);
    initDefaultSetting();
  }

  public MyWebView(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
    initDefaultSetting();
  }

  public MyWebView(Context context, AttributeSet attrs, int defStyleAttr,
      int defStyleRes) {
    super(context, attrs, defStyleAttr, defStyleRes);
    initDefaultSetting();
  }


  private void initDefaultSetting() {
    WebSettings webSettings = this.getSettings();
    webSettings.setJavaScriptEnabled(true);
    setWebChromeClient(new WebChromeClient());
    setWebViewClient(new MyWebViewClient());
  }

  /**
   * Load Web View with url
   */
  public void load(String url) {
    this.loadUrl(url);
  }

}

Inherited and override a method from WebViewClient from a library

Class: MyWebViewClient

public class MyWebViewClient extends WebViewClient {

  @Override
  public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
    view.loadUrl(request.getUrl().toString());
    return true;

  }
}

Layout: row_my_web.xml

<FrameLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="300dp">

  <com.silicus.fluidforms.MyWebView
    android:id="@+id/myWebView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>

</FrameLayout>

Class: ParentAdapter

import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.silicus.fluidforms.MyWebView;
import com.silicus.fluidforms.R;
import com.silicus.fluidforms.expandable.ParentAdapter.MyViewHolder;


    public class ParentAdapter extends RecyclerView.Adapter<MyViewHolder> {


      private String urlList[] = {
          "https://stackoverflow.com/questions/2835556/whats-the-difference-between-setwebviewclient-vs-setwebchromeclient",
          "https://developer.android.com/guide/webapps/webview",
          "https://www.google.com",
          "https://www.bling.com"};


      @NonNull
      @Override
      public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(parent.getContext())
            .inflate(R.layout.row_my_web, parent, false);
        return new MyViewHolder(itemView);
      }

      @Override
      public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
        holder.myWebView.load(urlList[position]);
      }

      @Override
      public int getItemCount() {
        return urlList.length;
      }

      class MyViewHolder extends RecyclerView.ViewHolder {

        MyWebView myWebView;

        MyViewHolder(View itemView) {
          super(itemView);
          myWebView = itemView.findViewById(R.id.myWebView);
        }
      }
    }
Ganesh Tikone
  • 1,047
  • 1
  • 8
  • 15
1

If you mean that instead of loading the page it was showing the popup asking you how you want to open the URL, you can fix it by adding a WebViewClient to the web view

WebViewClient webViewClient = new WebViewClient();
webView.setWebViewClient(webViewClient);
MeatPopsicle
  • 832
  • 13
  • 28