Table of contents
Open Table of contents
To create app widget — the steps we need to follow.
- Add the Glance Dependency in Android Module level build.gradle file.
//For AppWidgets support
implementation ("androidx.glance:glance-appwidget:1.0.0")
// For interop APIs with Material 2
implementation ("androidx.glance:glance-material:1.0.0")
// For interop APIs with Material 3
implementation ("androidx.glance:glance-material3:1.0.0")
- Create the widget with the content inside it.
import android.content.Context
import androidx.compose.runtime.Composable
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.glance.Button
import androidx.glance.GlanceId
import androidx.glance.GlanceModifier
import androidx.glance.GlanceTheme
import androidx.glance.LocalContext
import androidx.glance.action.actionStartActivity
import androidx.glance.appwidget.GlanceAppWidget
import androidx.glance.appwidget.GlanceAppWidgetReceiver
import androidx.glance.appwidget.provideContent
import androidx.glance.background
import androidx.glance.layout.Alignment
import androidx.glance.layout.Column
import androidx.glance.layout.fillMaxSize
import androidx.glance.layout.padding
import androidx.glance.text.FontWeight
import androidx.glance.text.Text
import androidx.glance.text.TextStyle
import androidx.glance.unit.ColorProvider
class MyHealthWidget : GlanceAppWidget() {
override suspend fun provideGlance(context: Context, id: GlanceId) {
provideContent {
GlanceTheme {
Content()
}
}
}
@Composable
fun Content() {
Column(
modifier = GlanceModifier.fillMaxSize().background(R.color.white),
verticalAlignment = Alignment.Top,
horizontalAlignment = Alignment.CenterHorizontally
) {
Text(
text = LocalContext.current.getString(R.string.widget_text),
modifier = GlanceModifier.padding(12.dp),
style = TextStyle(
fontWeight = FontWeight.Medium,
color = ColorProvider(Color.White),
fontSize = 24.sp
)
)
Button(
onClick = actionStartActivity<MainActivity>(),
text = LocalContext.current.getString(R.string.widget_button_text),
modifier = GlanceModifier.padding(12.dp)
)
}
}
}
- Create the receiver for the app widget.
class MyHealthWidgetReceiver() : GlanceAppWidgetReceiver() {
override val glanceAppWidget: GlanceAppWidget = MyHealthWidget()
}
- Initialize the strings for the app widget
<resources>
<string name="app_widget_name">MyHealth</string>
<string name="widget_text">Evaluate your emotional level</string>
<string name="widget_button_text">Sync</string>
</resources>
- Create the xml file -> my_widget_info.xml
<?xml version="1.0" encoding="utf-8"?>
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
android:initialLayout="@layout/glance_default_loading_layout"
android:description="@string/app_widget_name"
android:minWidth="100dp"
android:minHeight="30dp"
android:resizeMode="horizontal|vertical"
android:widgetCategory="home_screen"
/>
- Now update the AndroidManifest.xml file.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<uses-permission android:name="android.permission.INTERNET" />
<application
android:name=".MyHealthApp"
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules"
android:icon="@drawable/logo"
android:label="@string/app_name"
android:roundIcon="@drawable/logo"
android:supportsRtl="true"
tools:targetApi="31">
<activity
android:name=".MainActivity"
android:exported="true"
android:theme="@style/Theme.MyHealthApp">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<meta-data
android:name="android.app.lib_name"
android:value="" />
</activity>
<receiver android:name=".MyHealthWidgetReceiver" android:exported="true">
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE"/>
</intent-filter>
<meta-data
android:name="android.appwidget.provider"
android:resource="@xml/my_widget_info"/>
</receiver>
</application>
</manifest>
- MainActivity.kt file
class MainActivity : AppCompatActivity() {
private lateinit var binding: ActivityMainBinding
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding=ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)
}
}
- Now run the app and enjoy the app widget.
Please go through the video for reference : video
Happy learning !