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.lovina.app"
)
)
}

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.lovina.app"
)
)
}

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.lovina.app",
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.

White-Label Theming

Apply a custom brand color to the widget header and chat bubbles:

import android.graphics.Color

val config = LovinaChatConfig(
websiteToken = "your-token",
baseUrl = "https://chat.lovina.app",
customColor = Color.parseColor("#6366F1") // Indigo
)

The SDK automatically detects whether the custom color is light or dark and adjusts text contrast accordingly.

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()
}