Android
How to add a Widget
Create a Widget class
To add a widget to your app, you need to create a new class that extends Widget
class.
class CustomWidget : Widget {
override val id: String = "custom_widget_id"
override val type: String = "custom_widget_type"
override val order: Int = 0
@Composable
override fun WidgetCard() {
// Composable function to display the widget card
}
override fun onViewed() {
// Action to be performed when the widget is viewed
}
override suspend fun onRefresh() {
// Action to be performed when the widget is need to be refreshed
}
}
Create a WidgetProvider
To declare this widget to the Widget framework, you need to create a WidgetProvider
and provide
your custom widget through it.
class CustomWidgetProvider : WidgetProvider {
override fun initializer() { // Optional override
// do some initialization if required
}
fun provideWidgets(params: List<WidgetParams>): List<Widget> {
return listOf(CustomWidget())
}
}
Register the WidgetProvider
Register the WidgetProvider in the SdkInitializer (Recommended)
Then, you need to add the WidgetProvider
to the SdkInitializer
:
context.installHumaSdk {
sdk {
widgetKit {
installProviders(CustomWidgetProvider())
}
}
}
Register the WidgetProvider in the HumaWidgetKitManager
Alternatively, you can register the WidgetProvider
directly in the HumaWidgetKitManager
:
HumaWidgetKitManager.getInstance().registerWidgetProvider(CustomWidgetProvider())