Many news applications use the Expandable type of view for displaying the news along with the categories. This type of feature will help to make the UI look better and also provides additional content in the same UI. This type of UI is used when you display different types of content inside your application and filtering it with different categories. In this article, we will take a look at creating a similar type of expandable News Feed Example in our Android App.
What we are going to build in this article?
We will be building a simple application in which we will be displaying a Recycler View in which we will show news categories and clicking on the news category we will display the news for that specific category in the recycler view inside that. A sample video is given below to get an idea about what we are going to do in this article. Note that we are going to implement this project using the Java language.
Step by Step Implementation
Step 1: Create a New Project
To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio. Note that select Java as the programming language.
Step 2: Working with the activity_main.xml file
Navigate to the app > res > layout > activity_main.xml and add the below code to that file. Below is the code for the activity_main.xml file.
Step 3: Creating a modal class for storing our news
Navigate to the app > java > your app’s package name > Right-click on it > New > Java class and name it as NewsModal and add the below code to it.
Step 4: Creating a modal class for storing our News Categories
Similarly, create another Java class and name it as CategoriesModal and add the below code to it.
Step 5: Creating a layout file for our news item
Navigate to the app > res > layout > Right-click on it > New > Layout Resource File and name it as news_rv_item and add the below code to it.
Step 6: Creating a new layout file for news categories
Similarly, create another Layout resource File and name it as news_categories_rv_item and add the below code to it.
Step 7: Creating a new Adapter class for setting data in news RecyclerView
Create another Java class and name it as NewsRVAdapter and add the below code to it. Comments are added in the code to get to know in more detail.
Step 8: Creating an adapter class for our categories RecyclerView
Create another Java class name it as CategoriesRVAdapter and add below code to it. Comments are added in the code to get to know in more detail.
Step 9: Working with the MainActivity.java file
Go to the MainActivity.java file and refer to the following code. Below is the code for the MainActivity.java file. Comments are added inside the code to understand the code in more detail.
Now run your app and see the output of the app.
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.