Skip to main content

Android Usage

The Lovina Chat SDK provides LovinaChatWidget, a FrameLayout subclass that wraps a WebView. You can embed it in any Activity or Fragment layout.

XML Layout

Add the widget to your layout XML:

<com.lovina.chatsdk.LovinaChatWidget
android:id="@+id/lovina_chat"
android:layout_width="match_parent"
android:layout_height="match_parent" />

Activity Setup

import com.lovina.chatsdk.*

class ChatActivity : AppCompatActivity() {

private lateinit var chatWidget: LovinaChatWidget

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_chat)

chatWidget = findViewById(R.id.lovina_chat)

// Listen for events
chatWidget.onEvent = { event ->
when (event) {
is ChatEvent.Loaded -> {
Log.d("Chat", "Widget loaded, token: ${event.authToken}")
}
is ChatEvent.MessageReceived -> {
Log.d("Chat", "New message: ${event.message}")
}
is ChatEvent.UnreadCountChanged -> {
updateBadge(event.count)
}
is ChatEvent.Closed -> {
finish()
}
is ChatEvent.Error -> {
Log.e("Chat", "Error [${event.code}]: ${event.message}")
}
}
}

// Configure and load
chatWidget.configure(
LovinaChatConfig(
websiteToken = "your-website-token",
baseUrl = "https://chat-prod.ailovina.com"
)
)
}

override fun onDestroy() {
chatWidget.destroy()
super.onDestroy()
}
}

Fragment Setup

The same pattern works inside a Fragment:

class ChatFragment : Fragment(R.layout.fragment_chat) {

private var chatWidget: LovinaChatWidget? = null

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
chatWidget = view.findViewById(R.id.lovina_chat)

chatWidget?.configure(
LovinaChatConfig(
websiteToken = "your-website-token",
baseUrl = "https://chat-prod.ailovina.com"
)
)
}

override fun onDestroyView() {
chatWidget?.destroy()
chatWidget = null
super.onDestroyView()
}
}

User Identification

Pass user information to associate messages with a known contact:

val config = LovinaChatConfig(
websiteToken = "your-token",
baseUrl = "https://chat-prod.ailovina.com",
user = LovinaChatUser(
identifier = "user-12345",
name = "Jane Doe",
email = "jane@example.com",
phoneNumber = "+6281234567890",
identifierHash = "hmac-hash-for-verification",
customAttributes = mapOf(
"plan" to "premium",
"signup_date" to "2025-01-15"
)
)
)

The identifierHash field is an HMAC hash used to verify the user's identity. Generate it server-side using your Lovina identity verification secret.

Theming and White-Label Branding

Customize colors, typography, and branding via theme and branding. Theme colors are hex strings, not Color ints:

val config = LovinaChatConfig(
websiteToken = "your-token",
baseUrl = "https://chat-prod.ailovina.com",
theme = LovinaThemeConfig(
primaryColor = "#6366F1" // Indigo
),
branding = LovinaBrandingConfig(
brandName = "Acme Support",
poweredBy = false
)
)

See Configuration for the full set of theme, branding, texts, and features options.

Session Management

Conversation sessions are persisted automatically via SharedPreferences. Returning users resume their existing conversation.

To clear a session and start fresh:

chatWidget.clearSession()
chatWidget.reload()

Lifecycle

Always call destroy() when the hosting Activity or Fragment is destroyed to clean up the WebView:

override fun onDestroy() {
chatWidget.destroy()
super.onDestroy()
}