How to implement Follow feature in your Android app

Italo Orihuela
Amity Developers
Published in
2 min readAug 21, 2023

--

For social media apps, the ability to follow and unfollow users is a crucial feature. It allows users to stay updated with the activities of others and fosters a sense of community. In this tutorial, we will guide you on how to implement this feature in your Android application using the Amity SDK. We will cover how to send follow requests, handle follow statuses, and withdraw follow requests. By the end of this tutorial, you will be able to enhance your app’s social interactions by allowing users to connect with each other.

Pre-requisites:

Before we begin, make sure you have the following:

- The Amity SDK installed and set up in your Android project.

- A valid `userId` of the user you want to follow.

- An instance of `AmityUserRepository` to manage user relationships.

Authentication and Log in

Before you can send a follow request, you need to authenticate and log in to your application. Here is how you can run the `AppMain` class in Android:

class ChatApp : Application() {
//
override fun onCreate() {
super.onCreate()
//
AmityCoreClient.setup(
apiKey = "apikey",
endpoint = AmityEndpoint.EU // optional param, defaulted as SG region
)
}
}

Next, you need to authenticate the user. Here is how you can do it:

fun authenticate() {
AmityCoreClient.login(userId = "userId 1")
.displayName(displayName = "John Doe") // optional
.authToken(authToken = "token") // optional
.build()
.submit()
.doOnComplete {
//success
}
.subscribe()
}

Step 1: Create a Function to Send Follow Request

First, create a function named `sendFollowRequest`. This function will take two parameters: an instance of `AmityUserRepository` and a `userId`, which is the ID of the user you want to follow.

fun sendFollowRequest(userRepository: AmityUserRepository, userId: String) {
// Rest of the code will go here
}

Step 2: Send Follow Request

Inside the `sendFollowRequest` function, use the `follow` method to send a follow request to the user.

userRepository
.relationship()
.user(userId = userId)
.follow()
// Rest of the code will go here

Step 3: Handle Success Response

Finally, handle the success response using the `doOnSuccess` method. The `doOnSuccess` method will return an instance of `AmityFollowStatus`, which you can use to determine the status of the follow request.

.doOnSuccess { followStatus: AmityFollowStatus ->
// Handle success here
}
.subscribe()

Full Code Snippet

Here is the complete code snippet for sending a follow request:

fun sendFollowRequest(userRepository: AmityUserRepository, userId: String) {
userRepository
.relationship()
.user(userId = userId)
.follow()
.doOnSuccess { followStatus: AmityFollowStatus ->
// Handle success here
}
.subscribe()
}

Final Thoughts

Implementing the “follow” feature in your Android application can significantly enhance user interactions and engagement. With the Amity SDK, you can easily integrate this feature and provide a seamless social experience to your users. Remember to handle success responses appropriately to ensure a smooth user experience. Don’t wait any longer, start powering up your community today!

--

--