Basic WebView in Android
This post will show you how you can add a web page into an android app. This app will load this blog into the android app using an element called a
So, without wasting any time, let's get started!
First, make a new Android Studio project. Use an Empty Activity when prompted. Wait for the build to complete and then start coding the app as follows:
First, open
Open
<WebView />
.So, without wasting any time, let's get started!
First, make a new Android Studio project. Use an Empty Activity when prompted. Wait for the build to complete and then start coding the app as follows:
First, open
AndroidManifest.xml
file and add the following permission just before the <application />
element:
<uses-permission android:name="android.permission.INTERNET" />
Open
activity_main.xml
file and remove its content. Add the following code there:
<LinearLayout xmlns:android = "https://schemas.android.com/apk/res/android" android:layout_width = "match_parent" android:layout_height = "match_parent" android:orientation = "vertical" /> <WebView android:layout_height = "match_parent" android:layout_width = "match_parent" android:id = "webView" /> </LinearLayout>And then open
MainActivity.java
file and code it as follows:
package ...; //your package import ...; //Android Studio automatically imports package, if error occurs, press alt+enter and select import package. public class MainActivity extends AppCompatActivity { WebView webView; WebSettings webSettings; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); webView = findViewById(R.id.webView); webSettings = webView.getSettings(); webSettings.setJavascriptEnabled(true); webView.loadUrl("https://blog.zoneofcode.com"); } }This will load this blog into the app. This was very basic. Now lets analyse the code. First we add the INTERNET permission in the app. This will allow our app to use internet, otherwise we would get an ERR_NAME_NOT_RESOLVED error. Then we add a
<WebView />
element to add a webview to our app. Then we code the WebView in JAVA and use its settings to enable JavaScript. And finally, we load our website into the app. That's it. Now, you know how you can load a website simply to the app. Don't forget to add a comment to let me know about your experience with WebViews!
Comments
Post a Comment
Please be polite to others and the blog owner while posting comments. You are requested to ask question only and give recommendations on this blog and our posts. This section is not meant for simply talking with each other.