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 app tutorial, we’ll be understanding how to embed Android webview in your native Android App.
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.
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" />
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 app from scratch, and need technical help, talk with experts or hire Android app development company to proceed with a professional approach under the right hands of experts.
Grab a free copy of web view example demo from Github.
Want to Develop Android App From Scratch? Contact Us Now