/stream-chat-android

Stream Chat official Android SDK. The tutorial covers how to build your own chat experience using either Java or Kotlin.

Primary LanguageKotlinOtherNOASSERTION

Official Android SDK for Stream Chat

Build status

4.x

4.x is based on Kotlin and splits out the client, offline support and UX components. It adds seamless offline support, improved performance, makes it easier to integrate, and has better test coverage.

stream-chat-android is the official Android SDK for Stream Chat, a service for building chat and messaging applications. This library includes both a low-level chat SDK and a set of reusable UI components. Most users start with the UI components, and fall back to the lower level API when they want to customize things.

Quick Links

Java/Kotlin Chat Tutorial

The best place to start is the Android Chat Tutorial. It teaches you how to use this SDK and also shows you how to make frequently required changes. You can use either Java or Kotlin depending on your preference.

Sample App

This repo includes a fully functional example app featuring threads, reactions, typing indicators, optimistic UI updates and offline storage. To run the sample app, start by cloning this repo:

git clone git@github.com:GetStream/stream-chat-android.git

Next, download Android Studio and open up the stream-chat-android folder. You'll want to run the stream-chat-android-sample app. The Gradle sync process can take some time when you first open the project.

Docs

This SDK consists of the following modules / artifacts:

The SDK provides:

  • A low-level client for making API calls and receiving chat events
  • Offline support and LiveData APIs module
  • Ready to use ViewModels for displaying a list of channels and a conversation
  • Four reusable chat views:

The documentation for LiveData and the custom views is available here: https://getstream.io/chat/docs/android_overview/?language=kotlin

Chat client

The low-level Chat API docs are available for both Kotlin and Java.

Offline support and LiveData APIs

The Offline support and LiveData APIs docs are available for both Kotlin and Java.

Supported features

  • Channels list UI
  • Channel UI
  • Message reactions
  • Link preview
  • Image, video and file attachments
  • Editing and deleting messages
  • Typing indicators
  • Read indicators
  • Push notifications
  • Image gallery
  • GIF support
  • Light and dark themes
  • Style customization
  • UI customization
  • Threads
  • Slash commands
  • Markdown message formatting

Installing the Kotlin/Java Chat SDK

Step 1: Add mavenCentral to your repositories in your project level build.gradle file:

allprojects {
    repositories {
        mavenCentral()
    }
}

Step 2: Add the library as a dependency in your module level build.gradle file:

See the releases page for the latest version number.

android {
    ...
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }

    // for Kotlin projects
    kotlinOptions {
        jvmTarget = '1.8'
    }
}

dependencies {
    implementation "io.getstream:stream-chat-android:$stream_version"

    // for Java projects
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
}

Setup Stream Chat

Make sure to initialize the SDK only once; the best place to do this is in your Application class.

class App : Application() {
    override fun onCreate() {
        super.onCreate()

        val apiKey: String = ...
        val user = User().apply {
            id = ...
            image = ...
            name = ...
        }

        val client = ChatClient.Builder(apiKey, this).build()
        val domain = ChatDomain.Builder(client, user, this).offlineEnabled().build()
        val ui = ChatUI.Builder(this).build()
    }
}

With this, you will be able to retrieve instances of the different components from any part of your application using instance(). Here's an example:

class MainActivity: AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        val chatClient = ChatClient.instance()
        val chatDomain = ChatDomain.instance()
        val chatUI = ChatUI.instance()
    }
}

Online status

Connection status to Chat is available via ChatDomain.instance().online which returns a LiveData object you can attach observers to.

ChatDomain.instance().online.observe(...)

Markdown support

Markdown support is based on Markwon 4. The SDK doesn't support all Markwon features, support is limited to these plugins:

If you want to use a library other than Markwon or extend the Markwon plugins, you can use the code below to customize Markdown rendering when you build your ChatUI instance:

val ui = ChatUI.Builder(context)
    .withMarkdown { textView, text ->
        // do custom rendering here
        textView.text = text
    }
    .build()

Debug and development

Logging

By default, logging is disabled. You can enable logs and set a log level when initializing ChatClient:

val client = ChatClient.Builder(apiKey, context)
    .logLevel(ChatLogLevel.ALL)
    .build()

If you need to intercept logs, you can also pass in your own ChatLoggerHandler:

val client = ChatClient.Builder(apiKey, context)
    .logLevel(ChatLogLevel.ALL)
    .loggerHandler(object : ChatLoggerHandler {
        override fun logD(tag: Any, message: String) {
            // custom logging
        }

        ...
    })
    .build()