Skip to main content

WebView in Android Example




public class MainActivity extends AppCompatActivity {
    WebView webView;

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

        webView = (WebView) findViewById(R.id.webView1);
        webView.getSettings().setJavaScriptEnabled(true);
        WebSettings settings = webView.getSettings();
        settings.setBuiltInZoomControls(true);
        settings.setSupportZoom(true);
        startWebViews("http://google.co.in");
    }


    private void startWebViews(String url) {

        //Create new webview Client to show progress dialog 
        //When opening a url or click on link

        webView.setWebViewClient(new WebViewClient() {
            ProgressDialog progressDialog;

            //If you will not use this method url links are opeen in 
           new brower not in webview 
           public boolean shouldOverrideUrlLoading(WebView view, String url) {


                view.loadUrl(url);
                return true;
            }

            //Show loader on url load 
          public void onLoadResource(WebView view, String url) {
                if (progressDialog == null) {
                    // in standard case YourActivity.this 
                    progressDialog = new ProgressDialog(MainActivity.this);
                    progressDialog.setMessage("Please wait...");
                    progressDialog.show();
                }
            }

            public void onPageFinished(WebView view, String url) {
                try {
                    if (progressDialog != null) {
                        progressDialog.dismiss();
                    }


                } catch (Exception exception) {
                    exception.printStackTrace();
                }
            }

        });

        // Javascript inabled on webview 
        webView.getSettings().setJavaScriptEnabled(true);

        // Other webview options
        //Load url in webview 
       webView.loadUrl(url);


    }
}


========================================================================================


<?xml version="1.0" encoding="utf-8"?> 
<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">

    <WebView 
 android:id="@+id/webView1" 
 android:layout_width="fill_parent" 
 android:layout_height="fill_parent"/>
</RelativeLayout>

=========================================================================================
Give Permission in Android Manifiest File.

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
 package="com.webviewDemo">

    <uses-permission android:name="android.permission.INTERNET" />

    <application 
 android:allowBackup="true" 
 android:icon="@mipmap/app_icon" 
 android:label="@string/app_name" 
 android:supportsRtl="true" 
 android:theme="@style/AppTheme">
        <activity 
 android:name=".SplaceScreen" 
 android:screenOrientation="portrait">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity 
 android:name=".MainActivity" 
 android:screenOrientation="portrait" />
    </application>
 </manifest>



Comments

Popular posts from this blog

Android Logging using Timber Library

  Timber     is a logger with a small, extensible API which provides utility on top of Android's normal Log class. Before  the release, we’ll cleanup the log statements by removing them manually (even though logs can be disabled in release build). This tedious process can be avoided easily by using Timber. Timber provides lots of other options as well. Let’s see how it can be used in our projects to maintain the logs better. 1. Timber Below are few debug statements printed using default Log class. int a = 100 ; Log.e( "TAG" , String.format( "Integer a value is: %d" , a));   String name = "Android Codest" ; Log.e( "TAG" , String.format( "Find source from: %s" , name)); The above same statements can be printed using Timber as below. int a = 100 ; Timber.d( "Integer a value is: %d" , a);   String name = " Android Codest " ; Timber.d( " Find source from : %s" , name); You can notice here, the TAG is not p...

Convert your apk into Android App Bundle

Android App Bundle In Google I/O 2018, a new publishing format has been introduced for Android applications called Android App Bundle. It is a new upload format that includes all your app’s compiled code and resources, but defers APK generation and signing to Google Play. Traditionally, Android apps are distributed using a special file called an Android Package(.apk). How to build an app bundle? You can easily build your app bundle using Android Studio(3.2 Canary 14+) or using command line interface. The generated app bundle will be stored at app/build/outputs/bundle/buildVariant/bundle.aab. Android Studio : Go to Build > Build Bundle(s)/APK(s) and select Build Bundle(s). Console : ./gradlew bundle Dynamic Delivery with Split APKs A fundamental component of Dynamic Delivery is the split APK mechanism available on Android 5.0 (API level 21) and higher. Split APKs are very similar to regular APKs — they include compiled DEX bytecode, resources, and an Android manifest. However, the ...

Butter Knife View Library for Android

Android  ButterKnife  library is a view injection library that injects views into android activity / fragments using annotations. For example,  @BindView  annotation avoids using  findViewById()  method by automatically type casting the view element. Not just view binding, butterknife provides lot of other useful options like binding  strings ,  dimens ,  drawables ,  click events  and lot more.  1. Adding ButterKnife Dependency First thing you have to do is, add ButterKnife in your project by adding the below dependencies in your project’s  app/build.gradle  file. Once added, sync your project, you are good to go. android { .. . // Butterknife requires Java 8. compileOptions { sourceCompatibility JavaVersion . VERSION_1_8 targetCompatibility JavaVersion . VERSION_1_8 } } dependencies { implementation ' com.jakewharton:butterknife:10.2.2 ' annotationProcessor ' com.jakewharton:butterknife-com...