You’ve used Facebook, right?
Almost everybody has.
But the question is: have you ever clicked on any link shared by someone?
What happens when you click it?
The URL gets loaded within the Android app.
But why developers at Facebook implemented this feature?
There must be a reason, right?
Of course, there is. And the primary goal of Android Webview is to keep the users within the app.
And in today’s Android webview tutorial, we’ll be understanding how to embed Android webview in your native Android App.
Let’s Get Started
Create a new project under file menu, modify the project details, and choose location for the demo project.
In the next tab, choose Mini Support SDK for your project.
Lastly, select Add No Activity and click on finish button.
Start Code Integration
activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <Button android:id="@+id/buttonUrl" android:layout_gravity="center" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="www.google.com" /> </LinearLayout>
Web_view.xml
<?xml version="1.0" encoding="utf-8"?> <WebView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/webView" android:layout_width="fill_parent" android:layout_height="fill_parent" />
Want To Create An Android Application?
Validate your app idea and get a free quote.
MainActivity.java
package com.webviewdemo; import android.app.Activity; import android.content.Context; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; public class MainActivity extends Activity { private Button button; public void onCreate(Bundle savedInstanceState) { final Context context = this; super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); button = (Button) findViewById(R.id.buttonUrl); button.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { Intent intent = new Intent(context, WebViewActivity.class); startActivity(intent); } }); } }
WebViewActivity.java
package com.webviewdemo; import android.app.Activity; import android.os.Bundle; import android.webkit.WebView; public class WebViewActivity extends Activity { private WebView webView; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.web_view); webView = (WebView) findViewById(R.id.webView); webView.getSettings().setJavaScriptEnabled(true); webView.loadUrl("https://www.google.com"); } }
AndroidManifest.xml
//Android Webview requires INTERNET permission.
<uses-permission android:name="android.permission.INTERNET" />
Now once the user has given Internet permission, run the demo.
And Done!
As simple as that!
However, if you’re building an Android app from scratch, and need technical help, talk with experts or hire android app developers to proceed with a professional approach under the right hands of experts.
Grab a free copy of Android webview example from Github.
Also Read:
- How To Integrate UIWebView to Load URLs Within Your iOS App
- iOS Tutorial: How to create a simple browser using WKWebView
This page was last edited on December 28th, 2020, at 8:53.