Develop a widget for Flex Window
develop a widget for flex window objective learn how to create a calendar widget on the flex window of galaxy z flip5 overview the flex window of galaxy z flip5 provides a wider screen than the previous model, galaxy z flip4 it provides more functionalities, such as having customizable clocks, rich wallpapers, and informative notification list the widget that you are going to develop for this code lab can provide a simple interface by executing simple tasks directly visible on the flex window it can guide users to open their z flip5 to perform complex actions on the main screen set up your environment you will need the following samsung galaxy z flip5 android studio latest version recommended java se development kit jdk 11 or later remote test lab if physical device is not available sample code here is a sample code for you to start coding in this code lab download it and start your learning experience! flex window widget sample code 404 29 kb start your project after downloading the sample code containing the project files, open your android studio and click open to open an existing project locate the downloaded android project capsule_samplewidget from the directory and click ok make the appwidgetprovider noteto know the basics about creating a widget in android, you may refer to this android developer guide the android appwidget provider defines the properties of the widget while com samsung android appwidget provider implements the methods of the appwidgetprovider class to make the appwidgetprovider, you need to declare the location of the xml file in androidmanifest xml to do this go to app > manifests and open androidmanifest xml look for the <receiver> tag with the simplecalendarwidgetprovider identifier add the following meta-data just above the </receiver> tag <meta-data android name="android appwidget provider" android resource="@xml/meta_info_calendar_widget" /> <meta-data android name="com samsung android appwidget provider" android resource="@xml/samsung_meta_info_sample_widget" /> set the size of the widget go to app > res > xml and open meta_info_calendar_widget xml add the following attributes to set the size of the flex window and set it as a lock screen android minwidth="352dp" android minheight="339dp" android resizemode="horizontal|vertical" android widgetcategory="keyguard" add the samsung appwidgetproviderinfo meta-data go to app > res > xml and open samsung_meta_info_calendar_widget xml add the following attribute to configure the display of the flex window <samsung-appwidget-provider display="sub_screen"> </samsung-appwidget-provider> start the activity from the widget the launchdisplayid indicates whether the widget is on the cover screen or on the main screen if you want to launch an activity from the widget, simply follow these steps to specify the screen display id go to app > java > com samsung android samplewidget > provider open simplecalendarwidgetprovider kt and assign 0 to the launchdisplayid if you want to display the widget on the main screen, and assign 1 if you want it to run on the flex window here, let's start a calendar activity on the main screen launchdisplayid = main_screen_id allow modification of the widget settings create a widget configuration activity to allow modification of the widget settings the appwidgetproviderinfo object describes the meta-data for the widget, such as its layout and update frequency go to app > res > xml and open meta_info_calendar_widget xml declare the configuration activity in the <appwidget-provider> tag by placing the android configure attribute android configure="com samsung android samplewidget activity editsettingactivity" create ongoing notifications first, enable the code to send notification in simplecalendarwidgetprovider kt in simplecalendarwidgetprovider kt, call the makenotification function from the notificationutil class to provide a notification for the sample widget val notificationutil = notificationutil context val notification = notificationutil makenotification pendingintent getactivity context, 0, intent context, editsettingactivity class java , pendingintent flag_cancel_current or pendingintent flag_immutable , it size notificationutil sendnotification notificationutil notification_id, notification set the notification's properties in the same function in notificationutil kt setsmallicon r drawable ic_cover_screen setcolor color parsecolor "#338577" setcontenttitle “sample widget” setongoing true after creating the notification, it should go to the main window of the flex window you can now check and see that there's an ongoing notification notethis feature only works with one ui 6 0 enable the calendar widget now, you can build the project in android studio and test it on your device to enable the widget in your galaxy z flip5, you have to go to settings > cover screen and tap widgets enable the widget that you've created in this code lab you can now see the widget on the flex window of your galaxy z flip5 check other features of the widget you can also test other features of the widget included in the sample project background of the widget in this code lab, you can check the sample project on how to change the background of the widget to be opaque or transparent vertical scrolling also, vertical scrolling is already implemented in the sample project you can check its implementation in the code this allows you to preview more notifications as possible in the flex window you're done! congratulations! you have successfully achieved the goal of this code lab now, you can develop a widget for the flex window by yourself! if you face any trouble, you may download this file flex window widget complete project 404 27 kb to learn more about developing apps for galaxy foldable devices, visit www developer samsung com/galaxy-z