RSS Reading In Kotlin: Easy Android Programming

Prologue

success_anil
The Startup
Published in
7 min readFeb 4, 2020

--

RSS reading is one of the most important and widely used features across software apps in web or mobile techs. To keep the things in control we would be concentrating our blog to kotlin.

To start with I would like to emphasize on a saying

You can’t build a great building on a weak foundation

Therefore, we will first be creating a very basic app for exploring RSS Reading functionality in Kotlin. In the later part of this blog, we would be using MVVM to improve our code structure.

During our journey, we will encounter a few helper libraries like CardView, RecyclerView, Glide, Jsoup in between.

So Let’s start the process, you may like to see what we are going to develop

Process

I assume that readers know how to checkout projects from a Github repository. Even if they don’t know, they may read more at this and this link. Once you git checkout public repository available at the link and open it in your Android Studio, you will see below code structure in the src folder.

Further, this blog is not about recycler view or any other UI component. If required I will write another blog for explaining them.

So we have

Activity called MainActivity

Fragment called RSSFragment (kind of list fragment)

Recyclerview Adapter called MyItemRecylcerviewAdapter

Model class RSSItem to hold particular RSS item data.

Parser class for parsing inputstream.

Then we have AppGlideModule class required by Glide.

Below is the code for different files , I will explain them one by one

MainActivity.kt

we are mere invoking RSSFragment in onCreate method.

Distributing code among fragments

Fragments are Lightweight, reusable UI components that can be used for creating UI in Android. Invoking an activity is a heavy process for Android OS as compared to fragment. Android OS may choose to reject activity invoking requests by the application.

You must have encountered the above scenario in real life when your phone stops responding to touch events on screen. Moreover, only the activity is not the only culprit here.

As per android docs

You can think of a fragment as a modular section of an activity, which has its own lifecycle, receives its own input events, and which you can add or remove while the activity is running (sort of like a “sub-activity” that you can reuse in different activities).

Therefore we will be using fragment here to build our application.

Once you scan the above code, you will find it’s very simple. No complex task is going on, but yet few lines may increase your thrust to know more. 🙂

In our onActivityCreated method, we are calling an AsyncTask to bring the RSS feed data from the network so that UI Thread remains free for doing some cool things like showing animations. More specifically, below is the asynctask class.

Nothing complex here, we are just making a network request for getting input stream from the given URL. Please ensure you have given proper permissions in Android Manifest.xml to access the internet.

In the above code, we get an inputstream from a server on hitting the given RSS link. To make the code easy to understand we are not ignoring network error conditions here.

Once we get inputstream we pass it to our parser class

Our RSS Parser is based on XMLPullparser in android. To know more about XMLPullparser

So now RSSParser call will parse inputstream and stuff all the items in an ArrayList. If we put focus on the parse method we will see, XMLPullparser raised events for the start and end XML tags present in RSS feed.

At last here are the Gradle files that show different dependencies used in our application. below is root level Gradle file

app-level Gradle file

Code improvement using MVVM would be explained in another blog. Soon I will update the link here.

Thanks for reading RSS Reading in Kotlin blog.

The post originally published at Link.

Happy Coding.

--

--

success_anil
The Startup

Learning via repetition. Long but sturdy, reliable way to learn.