How to Create a Dynamic Widget of an Android App?
Prerequisites: How to Create a Basic Widget of an Android App?
Widgets are the UI elements provided by an application for accessing some of its features remotely either from Home Screens or Lock Screens. Widgets can be Static or Dynamic meaning that the display elements don’t change (Static) or change (Dynamic) with time. Through this article, let’s demonstrate the implementation of a Dynamic widget. In our case, we shall change the display with respect to time using a Thread. Here is a preview of the same:
Steps for Creating Dynamic Widget
Step 1: Create a New Project
To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio. Note that select Kotlin as the programming language.
Step 2: Add the App Widget to the Project
- Right-Click on the app, move the cursor to new, find the “Widget” option at the end, select it.
- Specify the required properties for the widget such as min.width and height, config file and preferred language, etc, and proceed. Files are automatically generated.
Step 3: What to program? Where to program?
- In our application, since we wish to display two messages “Just do it” & “You are awesome” (the users may choose their own messages) one after the other simultaneously, we would be implementing a thread for generating a pause (of 1 second) in between.
- The entire programming (back-end) is done in the newly created NewAppWidget.kt, Kotlin Class File in the Main Source Folder.
Changes made only to NewAppWidget.kt file
That’s it, run the code and you will see this widget in the Widgets list, bring it to Home Screen.