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:

  1. 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.
  1. Edit the layout:
    The default activity_main.xml will be used to create a container for the webpage. Here’s how to configure it: Open res/layout/activity_main.xml and 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>
  1. Update the MainActivity.java (or MainActivity.kt for Kotlin): Open the MainActivity.java file and add the necessary code to load a webpage into the WebView. 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")
       }
   }
  1. Update the Android Manifest:
    To allow the app to access the internet, you’ll need to add the internet permission to the AndroidManifest.xml. Add this line inside the <manifest> tag:
   <uses-permission android:name="android.permission.INTERNET" />
  1. 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.