Here is an example of how to create a simple webview app in Android Studio:
- Create a new project in Android Studio and name it “WebViewApp”.
- In the MainActivity.java file, add the following code to create a webview and load a website:
import android.os.Bundle;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
private WebView webView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
webView = (WebView) findViewById(R.id.webView);
webView.setWebViewClient(new WebViewClient());
webView.loadUrl("https://www.google.com");
}
}
3. In the activity_main.xml file, add the following code to define the webview layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<WebView
android:id="@+id/webView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
4. In the AndroidManifest.xml file, add the following code to give the app permission to access the internet:
<uses-permission android:name="android.permission.INTERNET" />
5. Run the app on an emulator or a physical device and you should see the Google website loaded in the webview.
You can also add other features like back, forward, and refresh buttons, or even open multiple webpage at the same time by using tabbed browsing like features.