Android SharedPreferences Tutorial and Example

Building A Simple WebView Browser In Android

A WebView lets an Android application display web pages inside its own interface. With a small amount of Kotlin, you can create a useful in-app browser with an address field, page navigation, loading feedback, and familiar controls such as back, forward, refresh, and stop.

This project is a practical way to learn Android UI components, activity lifecycle events, runtime permissions, and basic web navigation. The result can be tested on an emulator or a physical device in Sydney, Melbourne, Brisbane, or anywhere with a reliable mobile connection.

Set Up The Android Project

Create a new project in Android Studio using the Empty Views Activity template. Kotlin is a sensible choice for a current Android project, although the same concepts can be implemented in Java. Give the application a clear package name and select a minimum SDK that suits the devices you want to support.

A WebView needs internet access, so add this permission to AndroidManifest.xml:

<manifest xmlns:android="http://schemas.android.com/apk/res/android">
    <uses-permission android:name="android.permission.INTERNET" />

    <application
        android:theme="@style/Theme.SimpleBrowser"
        android:label="Simple Browser">
        <activity
            android:name=".MainActivity"
            android:exported="true">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
</manifest>

The permission is normal rather than dangerous, so Android does not display a runtime permission dialogue. Use HTTPS pages whenever possible, particularly when your application may be used on public Wi-Fi at a Melbourne café or a university campus.

Design The Browser Screen

The layout can contain a horizontal address bar followed by a WebView. A toolbar with navigation buttons may be added below or above the page. For a beginner-friendly version, start with an EditText, a Button, and a WebView inside a vertical layout.

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <EditText
            android:id="@+id/addressInput"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:hint="Enter a web address"
            android:inputType="textUri"
            android:singleLine="true" />

        <Button
            android:id="@+id/goButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Go" />
    </LinearLayout>

    <WebView
        android:id="@+id/browserView"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1" />
</LinearLayout>

A weighted height allows the WebView to occupy the remaining screen space. The address field can accept a complete URL, while the activity can add https:// when the user enters a domain such as example.com. This avoids an unnecessarily strict input experience.

Load Pages With Kotlin

In MainActivity.kt, enable JavaScript only when the websites your app needs depend on it. Many modern sites use scripts for menus, forms, or dynamic content, but JavaScript also increases the attack surface. Set a WebViewClient so links stay inside the app rather than opening a separate browser.

class MainActivity : AppCompatActivity() {

    private lateinit var browserView: WebView
    private lateinit var addressInput: EditText

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        browserView = findViewById(R.id.browserView)
        addressInput = findViewById(R.id.addressInput)
        val goButton: Button = findViewById(R.id.goButton)

        browserView.settings.javaScriptEnabled = true
        browserView.webViewClient = WebViewClient()

        goButton.setOnClickListener {
            loadAddress(addressInput.text.toString())
        }

        addressInput.setOnEditorActionListener { _, _, _ ->
            loadAddress(addressInput.text.toString())
            true
        }

        loadAddress("https://www.google.com")
    }

    private fun loadAddress(value: String) {
        val input = value.trim()
        if (input.isEmpty()) return

        val url = if (input.startsWith("http://") ||
            input.startsWith("https://")) {
            input
        } else {
            "https://$input"
        }

        addressInput.setText(url)
        browserView.loadUrl(url)
    }
}

When testing locally, remember that an Android emulator cannot usually reach a development server through localhost in the same way as your computer. Use 10.0.2.2 for the standard Android emulator, or the machine’s local network address for a physical device.

Add Navigation And Useful Feedback

A browser feels much more polished when it exposes the WebView history. Add buttons for back, forward, refresh, and stop, then connect them to canGoBack(), goBack(), canGoForward(), goForward(), reload(), and stopLoading(). Override the activity’s back action so it moves through web history before closing the screen.

A WebChromeClient can report loading progress to a ProgressBar, while onPageFinished() can update the address field with the final URL. This is useful when a page redirects from one address to another or when a website uses a local Australian domain such as .com.au.

Feature Android API Purpose
Open a page loadUrl() Displays a web address in the WebView
Back navigation goBack() Returns to the previous history entry
Forward navigation goForward() Restores a page after going back
Reload content reload() Requests the current page again
Stop loading stopLoading() Cancels an active navigation
Loading status WebChromeClient Provides progress and browser events

For applications that search a private catalogue or product collection, the WebView can display a custom web interface backed by an API. A useful reference on implementing full-text search with PostgreSQL and Node.js can help when the browser becomes part of a larger search experience.

Handle Security And Device Behaviour

Do not load arbitrary untrusted content with powerful JavaScript bridges. Avoid exposing Kotlin or Java objects through addJavascriptInterface() unless the pages are fully controlled and the interface is carefully restricted. Keep clear URL rules if the app is intended to access only approved services, such as a school portal, internal dashboard, or Australian retail site.

You should also consider cookies, file uploads, pop-ups, mixed content, and certificate errors. Never bypass SSL certificate warnings in production. If the app handles logins, confirm that passwords and session cookies are transmitted over HTTPS. Test page layouts on small phones, tablets, and devices with large text settings.

The activity can restore the WebView state after rotation:

override fun onSaveInstanceState(outState: Bundle) {
    browserView.saveState(outState)
    super.onSaveInstanceState(outState)
}

override fun onRestoreInstanceState(savedInstanceState: Bundle) {
    super.onRestoreInstanceState(savedInstanceState)
    browserView.restoreState(savedInstanceState)
}

For Australian users, test on both Wi-Fi and mobile networks because regional coverage can vary outside major areas such as Perth, Adelaide, and Canberra. A visible progress indicator and a friendly error message are especially helpful when a page takes longer to load.

Refine The User Experience

The basic project can grow into a focused browser rather than a general replacement for Chrome. Add a clear button, keyboard-friendly submit action, URL validation, bookmarks using SharedPreferences, and a download handler where appropriate. A toolbar icon can provide settings for JavaScript, desktop-site mode, or clearing browsing data.

Keep the interface familiar and accessible. Use a readable contrast ratio, content descriptions for icon buttons, and touch targets large enough for comfortable use. Australian spelling in labels, local date formats, and support for common .com.au addresses can make the application feel more natural to its intended audience.

Useful refinements include:

  • Show a progress bar while a page is loading.
  • Display a friendly offline or connection error screen.
  • Restrict navigation to approved HTTPS domains when required.
  • Save bookmarks and the last visited page with SharedPreferences.
  • Test redirects, back navigation, rotation, and poor network conditions.

Run the app on several Android versions before release. Check that external links behave as intended, that the keyboard does not hide the address field, and that pressing the system back button follows the expected history behaviour. With these foundations, a simple WebView browser becomes a compact project that demonstrates layouts, activity state, networking, and practical Android application design.