Search⌘ K
AI Features

Article Fragment Layout

Explore how to design an article fragment layout in Android using Kotlin by integrating WebView to display news articles, a centered ProgressBar to indicate loading, and a FloatingActionButton to save favorites. You'll understand how to set up these components inside a ConstraintLayout for efficient and user-friendly UI.

WebView in Android

WebView is an extension of Android’s View class that is used to render web pages directly in our application without the need to launch the browser, although they don’t offer all of the functions of a browser.

We’ll learn how WebView function by creating a basic standalone project that will only take a few minutes to set up and compile.

Simply create a new project and name it WebView, then let gradle build it. After a successful build, navigate to the activity_main.xml file and insert a WebView view as shown below. Ascertain that the layout is a ConstraintLayout.

XML
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

Let’s head over to the MainActivity.kt file and write a few lines of code to show our preview:

  • The first step is to locate our WebView using its
...