# Android Introduction

The Android SDK enables you to build any type of chat or messaging experience for Android. It consists of three major components:

- **Client** : The client handles all API calls and receives events.

- **Offline** : The offline library stores the data, implements optimistic UI updates, handles network failures, and exposes `StateFlow` objects that make it easy to build your own UI on.

- **UI** : The UI package includes ViewModels and custom views for common things like a channel list, message list, message input etc.

We typically recommend building a prototype with the full UI package. This is the fastest way to ensure that you have the integration set up correctly in combination with the backend and other teams. After that, about half of our customers launch in production with our UI package.

There are some limitations to Android UI reusability, so if you have more complicated design needs, you'll want to drop down a level to the _offline_ package. For most applications, you won't have to use the client directly (note that it's a lot of work to keep the chat state updated manually, which you'd have to do when using the client).

Before going through these docs, we recommend trying out the [Android Chat tutorial](https://getstream.io/tutorials/android-chat/#kotlin) and taking a look at the [Android Chat Sample App](https://github.com/GetStream/stream-chat-android#sample-app). You can also check out our [API docs](https://getstream.github.io/stream-chat-android/) for more information, as well our source code [on GitHub](https://github.com/GetStream/stream-chat-android).

The [online API tour](https://getstream.io/chat/get_started/) is also a nice way to learn about how the API works. It's in-browser, and therefore Javascript based, but the ideas are pretty much the same in Kotlin.

## Getting Started

This guide quickly brings you up to speed on [Stream’s Chat API](https://getstream.io/chat/). The API is flexible and allows you to build any type of chat or messaging. Note that if you build on [our UI components](https://github.com/GetStream/stream-chat-android/tree/main/stream-chat-android-ui-components), you won't need to call into this API too often after the initialization of the library.

Add one of the three packages below to your dependencies for your module/app level `build.gradle` file:

<codetabs>

<codetabs-item value="groovy" label="Groovy">

```groovy
repositories {
  google()
  mavenCentral()
  maven { url "https://jitpack.io" }
  jcenter()
}

dependencies {
  // Client + offline + UI components
  implementation "io.getstream:stream-chat-android-ui-components:$stream_version"
  // Client + offline
  implementation "io.getstream:stream-chat-android-offline:$stream_version"
  // Client only
  implementation "io.getstream:stream-chat-android-client:$stream_version"
}
```

</codetabs-item>

</codetabs>

For the latest version, check our [GitHub releases](https://github.com/GetStream/stream-chat-android/releases) page.

### Chat Client

Let's get started by initializing the client and connecting the current user:

<codetabs>

<codetabs-item value="kotlin" label="Kotlin">

```kotlin
val apiKey = "{{ api_key }}"
val token = "{{ chat_user_token }}"
// Step 1 - Set up the OfflinePlugin for offline storage
val offlinePluginFactory = StreamOfflinePluginFactory(
  config = Config(
    backgroundSyncEnabled = true,
    userPresence = true,
    persistenceEnabled = true,
    uploadAttachmentsNetworkType = UploadAttachmentsNetworkType.NOT_ROAMING,
  ),
  appContext = applicationContext,
)

// Step 2 - Set up the client, together with offline plugin, for API calls
val client = ChatClient.Builder(apiKey, applicationContext)
  // Change log level
  .logLevel(ChatLogLevel.ALL)
  .withPlugin(offlinePluginFactory)
  .build()

// Step 3 - Authenticate and connect the user
val user = User(
  id = "summer-brook-2",
  name = "Paranoid Android",
  image = "https://bit.ly/2TIt8NR",
)

client.connectUser(
  user = user,
  token = token, // or client.devToken(userId); if auth is disabled for your app
).enqueue { result ->
  if (result.isSuccess) {
    // Handle success
  } else {
    // Handler error
  }
}
```

</codetabs-item>

<codetabs-item value="java" label="Java">

```java
String apiKey = "{{ api_key }}";
String token = "{{ chat_user_token }}";
// Step 1 - Set up the OfflinePlugin for offline storage
Config config = new Config(true, true, true, UploadAttachmentsNetworkType.NOT_ROAMING);
StreamOfflinePluginFactory offlinePluginFactory = new StreamOfflinePluginFactory(config, applicationContext);

// Step 2 - Set up the client, together with offline plugin, for API calls
ChatClient client = new ChatClient.Builder(apiKey, applicationContext)
    // Change log level
    .logLevel(ChatLogLevel.ALL)
    .withPlugin(offlinePluginFactory)
    .build();

// Step 3 - Authenticate and connect the user
User user = new User();
user.setId("summer-brook-2");
user.setName("Paranoid Android");
user.setImage("https://bit.ly/2TIt8NR");

client.connectUser(user, token).enqueue((result) -> {
  if (result.isSuccess()) {
    // Handle success
  } else {
    // Handler error
  }
});
```

</codetabs-item>

</codetabs>

The user token is typically provided by your backend when you login or register in the app. If authentication is disabled for your app, you can also use a `ChatClient#devToken` to generate an insecure token for development. Of course, you should never launch into production with authentication disabled.

For more complex token generation and expiration examples, have a look at [Token Expiration](/chat/docs/android/tokens_and_authentication/).

### Channels

Let’s continue by creating your first channel. A channel contains messages, a list of members who are permanently associated with the channel, and a list of watchers currently watching the channel. The example below shows how to set up a channel:

<codetabs>

<codetabs-item value="kotlin" label="Kotlin">

```kotlin
val channelClient = client.channel(channelType = "messaging", channelId = "travel")

val extraData = mutableMapOf<String, Any>(
  "name" to "Awesome channel about traveling"
)

// Creating a channel with the low level client
channelClient.create(memberIds = emptyList(), extraData = extraData).enqueue { result ->
  if (result.isSuccess) {
    val channel: Channel = result.data()
    // Use channel by calling methods on channelClient
  } else {
    // Handle result.error()
  }
}

// Watching a channel's state using the offline library
scope.launch {
  client.watchChannelAsState(cid = "messaging:travel", messageLimit = 0).collect { channelState ->
    if (channelState != null) {
      // StateFlow objects to observe
      channelState.messages
      channelState.reads
      channelState.typing
    } else {
      // User not connected yet.
    }
  }
}
```

</codetabs-item>

<codetabs-item value="java" label="Java">

```java
ChannelClient channelClient = client.channel("messaging", "travel");

Map<String, Object> extraData = new HashMap<>();
extraData.put("name", "Awesome channel about traveling");
List<String> memberIds = new ArrayList<String>();

// Creating a channel with the low level client
channelClient.create(memberIds, extraData).enqueue((result) -> {
  if (result.isSuccess()) {
    Channel channel = result.data();
    // Use channel by calling methods on channelClient
  } else {
    // Handle result.error()
  }
});

// Watching a channel's state using the offline library
StateFlow<ChannelState> channelStateFlow = ChatClientExtensions.watchChannelAsState(client, "messaging:travel", 0);
LiveData<ChannelState> channelStateLiveData = FlowExtensions.asLiveData(channelStateFlow);

channelStateLiveData.observe(lifecycleOwner, channelState -> {
  if (channelState != null) {
    // StateFlow objects to observe. Use FlowExtensions.asLiveData(stateFlow); to LiveData conversion.
    channelState.getMessages();
    channelState.getReads();
    channelState.getTyping();
  } else {
    // User not connected yet.
  }
});
```

</codetabs-item>

</codetabs>

The first two arguments are the channel type and the channel ID ( `messaging`  and  `travel`  in this case). In this case, the channel ID is explicit, but channels can also be created by specifying a list of members, in that case, an ID will be generated automatically.

The channel type controls the settings we’re using for this channel. There are four default types of channels:

- livestream
- messaging
- team
- commerce

These options provide you with the most sensible defaults for the given use cases. You can also define custom channel types if the defaults don’t work for your use case. See [Channel Types](/chat/docs/android/channel_features/) for more info.

The `extraData` argument is an object containing custom channel data. You can add as many custom fields as you would like, as long as the total size of the object is _less than 5KB_. This applies not only to channels, but also messages, users, attachments, and reactions.

### Messages

Now that we have the channel set up, let's send our first chat message:

<codetabs>

<codetabs-item value="kotlin" label="Kotlin">

```kotlin
val message = Message(
  text = "I’m mowing the air Rand, I’m mowing the air.",
  cid = "messaging:travel",
  extraData = mutableMapOf("customField" to "123")
)

channelClient.sendMessage(message).enqueue { result ->
  if (result.isSuccess) {
    val message: Message = result.data()
  } else {
    // Handle result.error()
  }
```

</codetabs-item>

<codetabs-item value="java" label="Java">

```java
Map<String, Object> extraData = new HashMap<>();
extraData.put("customField", "123");

Message message = new Message();
message.setText("I’m mowing the air Rand, I’m mowing the air.");
message.setCid("messaging:travel");
message.setExtraData(extraData);

channelClient.sendMessage(message).enqueue((result) -> {
  if (result.isSuccess()) {
    Message sentMessage = result.data();
  } else {
    // Handle result.error()
  }
});
```

</codetabs-item>

</codetabs>

When you send a message to a channel, Stream Chat automatically broadcasts to all the people that are watching this channel and updates in real-time.

### Querying Channels

The client.queryChannels method enables you to retrieve a list of channels. You can specify a filter and sort order. The offline support library keeps the channels list updated as new messages, reactions and new channels arrive.

<codetabs>

<codetabs-item value="kotlin" label="Kotlin">

```kotlin
val filter = Filters.and(
  Filters.eq("type", "messaging"),
  Filters.`in`("members", "john"),
)
val sort = QuerySortByField<Channel>().descByName("lastMessageAt")

val request = QueryChannelsRequest(
  filter = filter,
  offset = 0,
  limit = 10,
  querySort = sort
).withWatch().withState()

client.queryChannels(request).enqueue { result ->
  if (result.isSuccess) {
    val channels: List<Channel> = result.data()
  } else {
    // Handle result.error()
  }
}
```

</codetabs-item>

<codetabs-item value="java" label="Java">

```java
FilterObject filter = Filters.and(Filters.eq("type", "messaging"), Filters.in("members", "john"));
QuerySorter<Channel> sort = QuerySortByField.descByName("lastMessageAt");

int offset = 0;
int limit = 10;
int messageLimit = 0;
int memberLimit = 0;
QueryChannelsRequest request = new QueryChannelsRequest(filter, offset, limit, sort, messageLimit, memberLimit).withWatch().withState();

client.queryChannels(request).enqueue((result) -> {
  if (result.isSuccess()) {
    List<Channel> channels = result.data();
  } else {
    // Handle result.error()
  }
});
```

</codetabs-item>

</codetabs>

To learn more about which fields you can query and sort on have a look at the [query channels](/chat/docs/android/query_channels/) documentation.

## Conclusion

Now that you understand the building blocks of a fully functional chat integration, let’s move on to the next sections of the documentation, where we dive deeper into details on each API endpoint.

---

This page was last updated at 2025-08-15T14:14:12.367Z.

For the most recent version of this documentation, visit [https://getstream.io/chat/docs/android/](https://getstream.io/chat/docs/android/).