Learn Android App Dev: Using WebView
Hey there! I am Amey Lokhande. In this post, I'll be writing on how to use WebView in your Android App. Well, WebView is generally used when a user wants to access any Website or URL directly from the App without accessing any kind of browser. WebViews are frequently used in most of the Apps these days.
In this post, I'll be writing on how to create an App to access any desired URL from the App.
So, here are the steps:
1. First of all, we will design the layout of our Android App. Drag a button, EditText and a WebView. Button will be used to view the Webpage when it is clicked. So your App will somewhat look like this:
2. Now declare the variables for your EditText, button and WebView in your Main_Activity.java.
3. Now, there's a thing which you need to do for your Webpage to open successfully. Open the Android_Manifest.XML file.
4. In AndroidManifest.XML file add the line above the <application tag.
<uses-permission android:name="android.permission.INTERNET"/>. This is the permission which we need to take for using the Internet Service in our Android App. This must be acquired for the App to work properly.
5. Now cast your variables inside the Method which will be used for showing the WebPage.
6. Now create a method for Button Click event. In that method declare a String Variable for storing the URL which the user enters in the EditText box. Now all you need to do is write your_WebView.loadURL(url_name). Here you need to enter your own WebView name and URL variable name.
7. Now you can successfully run your App.
Here's a screenshot of the running app:
Java Code:
package com.example.ameylokhande.webview;
import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.webkit.WebView; import android.widget.Button; import android.widget.EditText;
public class MainActivity extends AppCompatActivity { private static WebView my_web; private static Button btn; private static EditText url_data;
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Show_URL(); } public void Show_URL(){ my_web = (WebView)findViewById(R.id.webv); btn = (Button)findViewById(R.id.button); url_data = (EditText)findViewById(R.id.editText); btn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { String url = url_data.getText().toString(); my_web.loadUrl(url); } }); } }
Well, this is it. Thanks for reading!
Stay tuned for further updates.