In this tutorial we will learn, how to create a simple home screen app widget that contains a button and opens the MainActivity when we click it.
For this we will create a widget layout, an AppWidgetProvider class and an AppWidgetProviderInfo xml file. In the AppWidgetProvider java file we will then override onUpdate and create a PendingIntent which brings us to our MainActivity. We set this intent on our widget button with help of the RemoteViews class and then update our widget with the appWidgetManager object. We repeat this process for all instances of our widget by looping through the appWidgetIds int array.
In the app widget info xml file, we define the update frequency with updatePeriodMillis, the widgetCategory, minHeight, minWidth, minResizeWidth, initialLayout and more.
Lastly we register our AppWidgetProvider in the AndroidManifest.xml file as a broadcast receiver with the APPWIDGET_UPDATE intent filter. We register our appwidget info object in the meta data tag with android.appwidget.provider.
Our widget will have a previewImage which we create with help of the Widget Preview app and it will be resizable.
Watch the whole playlist:
App Widget Guidelines:
❗ Subscribe to the channel:
🎓 Find more tutorials and courses on my website:
💻 Hire me for your next project:
📨 Subscribe to the Coding in Flow newsletter:
💚 Donate to Coding in Flow with a message:
❓ Ask your programming questions in our chat:
📣 Follow Coding in Flow on other sites:
⌨️ My gear and tools:
(above links are affiliate links, which means that I earn a small commission if you buy through them with NO additional costs to you.)