How to Create an Android App That Displays a Webpage (Blank Container)
To create an Android app that simply loads a webpage into a blank container (using a WebView), here’s a simple example in Android Studio:
- Create a new project:
- Open Android Studio.
- Choose “New Project.”
- Select “Empty Activity.”
- Give it a name, for example,
WebContainerApp. - Set the minimum API level according to your needs and finish the setup.
- Edit the layout:
The defaultactivity_main.xmlwill be used to create a container for the webpage. Here’s how to configure it: Openres/layout/activity_main.xmland modify it to look like this:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<WebView
android:id="@+id/webview"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
- Update the
MainActivity.java(orMainActivity.ktfor Kotlin): Open theMainActivity.javafile and add the necessary code to load a webpage into theWebView. In Java:
package com.example.webcontainerapp;
import android.os.Bundle;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
WebView webView = findViewById(R.id.webview);
webView.setWebViewClient(new WebViewClient());
WebSettings webSettings = webView.getSettings();
webSettings.setJavaScriptEnabled(true);
// Load a webpage, e.g., Google
webView.loadUrl("https://www.google.com");
}
}
In Kotlin:
package com.example.webcontainerapp
import android.os.Bundle
import android.webkit.WebSettings
import android.webkit.WebView
import android.webkit.WebViewClient
import androidx.appcompat.app.AppCompatActivity
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val webView: WebView = findViewById(R.id.webview)
webView.webViewClient = WebViewClient()
val webSettings: WebSettings = webView.settings
webSettings.javaScriptEnabled = true
// Load a webpage, e.g., Google
webView.loadUrl("https://www.google.com")
}
}
- Update the Android Manifest:
To allow the app to access the internet, you’ll need to add the internet permission to theAndroidManifest.xml. Add this line inside the<manifest>tag:
<uses-permission android:name="android.permission.INTERNET" />
- Run the App:
After building and running the app, it will display a blank container that loads the webpage you specified.
You can now expand on this by adding features such as a progress bar, URL navigation, or more complex functionality.
