Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create a custom WebView

I have a lot of WebView in different parts of the program, but these WebViews does not differ each other that is why I want to create a custom WebView with necessary settings. At the current moment, WebView doesn't show, but no errors. Am I doing something wrong?

public class MyWebView extends WebView {

    MyWebView mMyWebView;

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

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

     public MyWebView initView(Context context){
         LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
         MyWebView view = (MyWebView) inflater.inflate(R.layout.custom_webview, this);
         view.getSettings().setJavaScriptEnabled(true) ;
         view.getSettings().setUseWideViewPort(true);
         view.getSettings().setLoadWithOverviewMode(true);
         view.getSettings().setDomStorageEnabled(true);
        return view;
    }
}

custom_webview.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:layout_width="match_parent"
              android:layout_height="match_parent">

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

</LinearLayout>

MainActivity:

public class MainActivity extends AppCompatActivity {

  MyWebView mWebView;

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

    mWebView = new MyWebView(MainActivity.this);
    mWebView = mWebView.initView(MainActivity.this);
    mWebView = (MyWebView) findViewById(R.id.web_view);

  }
  @Override
  protected void onResume() {
    mWebView.loadUrl("https://www.google.com/");
  }

}

main_activity.xml:

 <com.mypack.webview.MyWebView
        android:id="@+id/web_view"
        android:layout_below="@+id/start_URL"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>
like image 397
Delphian Avatar asked Jan 05 '17 11:01

Delphian


People also ask

What is custom WebView?

Extensions. opensource, custom-webview.

What can I use instead of WebView?

Alternatives to WebView If you want to send users to a mobile site, build a progressive web app (PWA). If you want to display third-party web content, send an intent to installed web browsers. If you want to avoid leaving your app to open the browser, or if you want to customize the browser's UI, use Custom Tabs.

What is the difference between WebView and app?

Within a native app, the touch screen, camera, and GPS all operate well. Webview apps are a different story. They aren't designed to run on a mobile device's operating system. They're nothing more than a collection of web pages that run in an embeddable browser.


1 Answers

You should probably rewrite your "MyWebView" class. Your method "initView" generates a new MyWebView object everytime but it is never connected to your MainActivity. This could be why you don't see it.

Maybe try something like (untested)

    public class MyWebView extends WebView {

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

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

        }

        private void initView(Context context){
             // i am not sure with these inflater lines
             LayoutInflater inflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
             // you should not use a new instance of MyWebView here             
             // MyWebView view = (MyWebView) inflater.inflate(R.layout.custom_webview, this);
             this.getSettings().setJavaScriptEnabled(true) ;
             this.getSettings().setUseWideViewPort(true);
             this.getSettings().setLoadWithOverviewMode(true);
             this.getSettings().setDomStorageEnabled(true);

        }
    }

And call it with

public class MainActivity extends AppCompatActivity {

      MyWebView mWebView;

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

        mWebView = (MyWebView) findViewById(R.id.web_view);

      }
      @Override
      protected void onResume() {
        mWebView.loadUrl("https://www.google.com/");
      }

}
like image 66
Stefan Lindner Avatar answered Oct 20 '22 15:10

Stefan Lindner