How to Create a News App in Android?

Networking is an integral part of android development, especially when building data-driven clients. The java class mainly used for network connections is HttpUrlConnection . The class requires manual management of data parsing and asynchronous execution of requests. For network operations, we are better off using a library called Fast Android Networking Library which can take care of sending and parsing network requests. In this article, we will step through the process of building an android news application. Fast Android Networking Library supports all types of HTTP/HTTPS requests and in our app, we will use it for

Here is what the application will look like in the end:

Step By Step Implementation

Step 1: Create a New Project

To learn how to create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio and choose Java as the programming language.

Step 2: Add dependencies

Select Gradle Scripts > build.gradle(Module:app) and add the below dependency in the dependencies section.

implementation 'com.amitshekhar.android:android-networking:1.0.2'
implementation 'com.amitshekhar.android:jackson-android-networking:1.0.2'

After adding the dependencies, sync your project by clicking Sync Now.

Step 3: Add permissions

Select manifests > AndroidManifest.xml and add the internet permission above the application tag.

Step 4: Get an API Key

Visit newsapi and register. The key will be displayed after registration and also sent to your email inbox.

Step 5: Create a new empty activity and add a WebView to the layout

Go to app > java > right-click > New > Activity > Empty Activity and name it as WebActivity. This activity will display web pages of articles as part of the activity layout. To add a WebView to the activity_web.xml file. Navigate to app > res > layout > activity_web.xml and add the below code to that file.

Step 6: Working with the WebActivity.java file

Below is the code for WebActivity.java .

Step 7: Add a RecyclerView to the activity_main.xml file

Select app > res > layout > activity_main.xml and add the below code to that file. The RecyclerView is used to efficiently display a list of data and in our case, it will display a list of news articles.

Step 8: Create a new custom layout

Navigate to File > New > XML > Layout XML File and name it as article_item. The article_item.xml file will be used for each row within the news article list. Below is the code for the article_item.xml file.

Step 9: Create a NewsArticle Model class

Navigate to app > Java > package > Right Click on package> New > Java Class and name it as NewsArticle. We created this model class for storing news article data like title, content, description e.t.c.

In our model class, there are mainly three types of methods :

Below is the code for the NewsArticle.java file.

Step 10: Create an ArticleAdapter class

Navigate to app > Java > package > Right Click on package> New > Java Class and name it as ArticleAdapter.The ArticleAdapter class is responsible for getting data from the NewsArticle model and displaying it on view. The adapter will provide access to the data items and is answerable for creating a view for each news article in the data set. Below is the code for the ArticleAdapter.java file.

Step 11: Working with the MainActivity.java file

In our activity, we will make GET requests to the news API using Android Fast Library and populate the RecyclerView with news articles. Firstly, we will create a string variable and set it to the API key (the key we created in step 4). For example if your API key = b971358de21d4af48ae24b5faf06bbok. Then declare the variable as :

private static String API_KEY="b971358de21d4af48ae24b5faf06bbok";

Below is the code for the MainActivity.java file.

Output: