We will develop an application featuring a horizontal RecyclerView showcasing various computer science technologies.
To start, create a new project in Android Studio using Java as the programming language.
After creating a new project, follow these steps:
Check if the Firebase Firestore dependency is added to your Gradle file. If not, include the following line:
implementation 'com.google.firebase:firebase-firestore:22.0.1'
After adding the dependency, sync your project to proceed.
To enable data transfer with Firebase, grant necessary permissions by adding these lines to your AndroidManifest.xml file:
<uses-permission android:name="android.permission.INTERNET" /> |
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> |
In the activity_main.xml file, utilize the following code snippet:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context=".MainActivity">...</LinearLayout>
Develop a new Java class dedicated to data storage and management.
When we work with Firebase Firestore to retrieve data, we need to develop a Java class to encapsulate this data. Follow these steps:
public class DataModal {
private String name;
private String imgUrl;
public DataModal() { }
public DataModal(String name, String imgUrl) {
this.name = name;
this.imgUrl = imgUrl;
}
public String getName() { return name; }
public void setName(String name) { this.name = name; }
public String getImgUrl() { return imgUrl; }
public void setImgUrl(String imgUrl) { this.imgUrl = imgUrl; }
}
Now, let's proceed to create a layout file for each item that will be displayed in the RecyclerView.
<?xml version="1.0" encoding="utf-8"?> |
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:gravity="center" android:orientation="vertical"> |
<!-- Image view for displaying our image --> |
<ImageView android:id="@id/idIVimage" android:layout_width="100dp" android:layout_height="100dp" android:layout_margin="4dp" android:background="@color/white" android:backgroundTint="@color/white" android:padding="3dp" /> |
<!-- Text view for displaying our text --> |
<TextView android:id="@id/idTVtext" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margin="2dp" android:padding="3dp" android:text="Category Text" android:textAlignment="center" android:textColor="@color/white" /> |
</LinearLayout> |
For creating a new Adapter class, follow these steps:
Below is the code for the DataRVAdapter class along with detailed explanations:
After creating the DataRVAdapter class, you can proceed to work with the MainActivity.java file to further implement and utilize the adapter.
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.
After setting up the code and ensuring proper data retrieval, the final step involves adding the data to your Firebase Console for storage and retrieval purposes.
Video Player |
00:00 |
00:08 |
Use Up/Down Arrow keys to increase or decrease volume. |