This guide shows you how to use the prebuilt UI components to quickly integrate Agentforce chat functionality into your Android app.
AgentforceLauncherContainer
Use the AgentforceLauncherContainer Composable in your screen. It displays a Floating Action Button (FAB) that, when tapped, presents the full chat container.
1import androidx.compose.runtime.Composable2import com.salesforce.android.agentforcesdk.ui.AgentforceLauncherContainer3import androidx.lifecycle.viewmodel.compose.viewModel45@Composable6fun MyScreen(viewModel: MyViewModel = viewModel()){7 // ... Your existing screen content (e.g., inside a Scaffold)89 // Add the Agentforce Launcher10 AgentforceLauncherContainer(client = viewModel.agentforceClient)11}
AgentforceConversationContainer
To display the chat UI, you can use the AgentforceConversationContainer Composable. This Composable provides the complete chat interface that you can integrate into your app.
The AgentforceConversationContainer Composable takes the following parameters:
conversation: The AgentforceConversation object that you created
onClose: A lambda that will be called when the user closes the chat view
Full Activity Example
The following example shows a complete Activity implementation that integrates the Agentforce chat UI:
1class MyActivity : ComponentActivity(){23 private var agentforceClient: AgentforceClient? = null4 private var conversation: AgentforceConversation? = null56 override fun onCreate(savedInstanceState: Bundle?){7 super.onCreate(savedInstanceState)89 // Initialize the Agentforce client and start a conversation10 initializeAgentforce()1112 setContent{13 var showChat by rememberSaveable{mutableStateOf(false)}1415 Box(Modifier.fillMaxSize()){16 // Your app content17 Button(onClick = { showChat = true}){18 Text("Open Chat")19}2021 // Show chat overlay22 if(showChat){23 conversation?.let{ conv ->24 agentforceClient?.AgentforceConversationContainer(25 conversation = conv,26 onClose = { showChat = false}27)28}29}30}31}32}3334 private fun initializeAgentforce(){35 val authProvider = MyAuthCredentialProvider()3637 val configuration = AgentforceConfiguration.builder(38 authCredentialProvider = authProvider39)40 .setApplication(application)41 .setSalesforceDomain("https://your-instance.salesforce.com")42 .build()4344 val mode = AgentforceMode.FullConfig(configuration)4546 agentforceClient = AgentforceClient().also{47 it.init(48 authCredentialProvider = authProvider,49 agentforceMode = mode,50 application = application51)52}53 conversation = agentforceClient?.startAgentforceConversation()54}55}
Handle UI Events
To handle UI events, implement the AgentforceUIDelegate interface. This interface provides callbacks for modifying messages before sending, handling sent messages, and managing agent switches.
1class YourActivity : AgentforceUIDelegate{2 override suspend fun modifyUtteranceBeforeSending(utterance: AgentforceUtterance): AgentforceUtterance{3 // Modify the utterance if needed4 return utterance5}67 override fun didSendUtterance(utterance: AgentforceUtterance){8 // Handle sent utterance9}1011 override fun userDidSwitchAgents(newConversation: AgentforceConversation){12 // Handle agent switch13}14}
The AgentforceUIDelegate interface provides the following methods:
modifyUtteranceBeforeSending(utterance: AgentforceUtterance): Called before an utterance is sent to the agent. Allows you to modify the utterance before it is sent.
didSendUtterance(utterance: AgentforceUtterance): Called after an utterance has been sent to the agent.
userDidSwitchAgents(newConversation: AgentforceConversation): Called when the user switches to a different agent.