After that, click on the second option and now your App is connected to Firebase. After connecting your app to Firebase, you will get to see the below screen.
Verify that the dependency for the Firebase Firestore database has been added to your Gradle file. Navigate to the app > Gradle Scripts and check whether the necessary dependency is present in the dependencies section. If not, add it to your build.gradle file.
Include the Firebase Firestore dependency in your Gradle Scripts to enable Firebase functionality in your app.
Update your AndroidManifest.xml file to include permissions for internet access:
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
Configure your activity_main.xml layout to incorporate a GridView for displaying data fetched from Firebase.
Example code snippet for activity_main.xml:
<GridView android:id="@id/idGVCourses" android:layout_width="match_parent" android:layout_height="match_parent" android:numColumns="2" />
Create a new Java class to handle and manage the data retrieved from Firebase in your Android app.
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; } |
} |
<?xml version="1.0" encoding="utf-8"?> | |||||||
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" | android:layout_width="match_parent" | android:layout_height="wrap_content" | android:layout_gravity="center" | android:gravity="center" | android:orientation="vertical" | android:padding="4dp"> | |
<!-- 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/black" /> |
</LinearLayout> |
CoursesGVAdapter
class extends ArrayAdapter<DataModal>
to manage a list view.DataModal
objects.getView
method inflates the layout for each list item.DataModal
at a specific position is retrieved.Step 8: Implementing functionalities in the MainActivity.java file.
Go to the MainActivity.java file and refer to the following code. Below is a summarized explanation of the code present in the MainActivity.java file:
After understanding the code, the next step involves adding data to Firebase Firestore in an Android application.