<p>In this article, we will talk about Android Widgets, how to add Widgets into our application and also how to work with it with a simple example.</p>
<h3><span style="color: #000080;"><strong>What is an App Widget?</strong></span></h3>
<p><strong><span style="color: #008000;">App Widgets</span></strong> are miniature application views of an app’s most important data and functionality that is accessible right from the user’s home screen and receive periodic updates. Using widgets users can see the information they need “at-a-glance” without unlocking their phone or launching the related app. An application component that is able to hold other App Widgets is called an <strong><span style="color: #008000;">App Widget host </span></strong><span style="color: #008000;"><span style="color: #000000;">(such as the Home screen)</span></span>.</p>
<p><strong><span style="color: #0000ff;">Get GITHUB code from <a style="color: #0000ff;" href="https://github.com/arunk7839/AndroidWidgetExample">here</a>.</span></strong></p>
<p><span class="embed-youtube" style="text-align:center; display: block;"><amp-youtube data-videoid="aw3TYsAbB-I" data-param-rel="1" data-param-showsearch="0" data-param-showinfo="1" data-param-iv_load_policy="1" data-param-fs="1" data-param-hl="en-US" data-param-autohide="2" data-param-wmode="transparent" width="1200" height="675" layout="responsive"><a href="https://www.youtube.com/watch?v=aw3TYsAbB-I" placeholder><amp-img src="https://i.ytimg.com/vi/aw3TYsAbB-I/hqdefault.jpg" alt="YouTube Poster" layout="fill" object-fit="cover"><noscript><img src="https://i.ytimg.com/vi/aw3TYsAbB-I/hqdefault.jpg" loading="lazy" decoding="async" alt="YouTube Poster"></noscript></amp-img></a></amp-youtube></span></p>
<h3><span style="color: #000080;"><strong>Creating New Project</strong></span></h3>
<p><strong>1</strong>. Create a new project in <span style="color: #008000;"><strong>Android Studio</strong> </span>from <span style="color: #008000;"><strong>File ⇒ New Project</strong></span> and fill the project details.</p>
<p><strong>2</strong>. Now to set up a widget for your application thanks to Android Studio, it does this for you automatically. To create a widget right click on<span style="color: #008000;"><strong> project root folder under <span style="color: #0000ff;">Java folder</span> ->; New ->; Widget ->; App Widget</strong>.</span></p>
<p><img class=" size-full wp-image-1427 aligncenter" src="https://c1ctech.com/wp-content/uploads/2019/12/Screenshot-2019-12-12-15.57.12.png" alt="Screenshot 2019-12-12 15.57.12" width="2204" height="1378" /></p>
<p><strong>3</strong>. Once you click on it, a <span style="color: #008000;"><strong>New Android Component</strong></span> window will open. Android Studio has an amazing interface for creating widgets.</p>
<figure id="attachment_1428" aria-describedby="caption-attachment-1428" style="width: 570px" class="wp-caption aligncenter"><img class="alignnone size-full wp-image-1428" src="https://c1ctech.com/wp-content/uploads/2019/12/android_widget_creation.gif" alt="android_widget_creation" width="570" height="472" /><figcaption id="caption-attachment-1428" class="wp-caption-text">Android Studio &#8211; Create a new App Widget</figcaption></figure>
<ul class="ul1">
<li class="li1">Provide a <span style="color: #0000ff;"><strong>name</strong></span> for your widget (We give it <span style="color: #008000;"><b>MyAppWidget</b></span>).</li>
<li class="li1">Select <span style="color: #0000ff;"><strong>placement</strong></span> which defines whether your App Widget can be displayed on the home screen (<span class="s2">home_screen</span>), the lock screen (<span class="s2">keyguard</span>), or both.</li>
</ul>
<p style="padding-left: 40px;"><strong><span style="color: #000080;">Note:</span></strong> Only Android versions lower than 5.0 support lock-screen widgets. For Android 5.0 and higher, only <span class="s2">home_screen</span> is valid.</p>
<ul class="ul1">
<li class="li1">Select <span style="color: #0000ff;"><strong>Resizable</strong></span> option which specifies the rules by which a widget can be resized. You can make widgets resizeable—horizontally, vertically, none, or on both axes.</li>
<li class="li1"><span class="s2">Provide <strong><span style="color: #0000ff;">MinimumHeight</span></strong></span> and <strong><span class="s2" style="color: #0000ff;">MinimumWidth </span></strong>which specifies the minimum height and width(in cells) to which the widget can be resized.</li>
<li>You can implement an App Widget configuration Activity by checking the <span style="color: #0000ff;"><strong>configuration screen</strong></span> checkbox. This is an optional <span style="color: #008000;"><strong>Activity</strong></span> that launches when the user adds your App Widget and allows them to modify App Widget settings at create-time, such as the App Widget color, size, update period or other functionality settings. Here I will leave it as unchecked.</li>
</ul>
<p><strong>4</strong>. Now click <strong><span style="color: #008000;">finish</span></strong>. You can see some files and folder gets added in your application.</p>
<h3><span style="color: #000080;"><b>Widget Creation Steps :</b></span></h3>
<ul class="ul1">
<li class="li2">create a layout for the widget.</li>
<li class="li2">create XML for defining the widget properties.</li>
<li class="li2">create a class for the widget actions.</li>
<li class="li2">add all these to the <strong><span style="color: #008000;">AndroidManifest.xml</span></strong> file.</li>
</ul>
<h3 class="p2"><span style="color: #000080;"><b>Widget-Layout File</b></span></h3>
<p>Now you have to define the layout of your widget in your default XML file (<span style="color: #008000;"><strong>my_app_widget.xml </strong></span>).</p>
<p>By default it creates a very simple layout for a widget as shown below:</p>
<p><span style="color: #0000ff;"><strong>my_app_widget.xml</strong></span></p>
<pre><;RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:background="#09C"
 android:padding="@dimen/widget_margin">;

 <;TextView
 android:id="@+id/appwidget_text"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_centerHorizontal="true"
 android:layout_centerVertical="true"
 android:layout_margin="8dp"
 android:background="#09C"
 android:contentDescription="@string/appwidget_text"
 android:text="@string/appwidget_text"
 android:textColor="#ffffff"
 android:textSize="24sp"
 android:textStyle="bold|italic" />;

<;/RelativeLayout>;</pre>
<p class="p2">This layout is displayed as a widget on the user’s home screen.</p>
<h3 class="p2"><span style="color: #000080;"><b>Widget-XML File</b></span></h3>
<p>In <strong><span style="color: #008000;">res</span></strong> folder it creates a new <span style="color: #0000ff;"><strong>xml</strong></span> folder which contains an xml file that defines the widget properties.</p>
<p><img class="alignnone wp-image-1429" src="https://c1ctech.com/wp-content/uploads/2019/12/Screenshot-2019-12-12-19.23.23-2762959793-1576170924747.png" alt="Screenshot-2019-12-12-19.23.23.png" width="362" height="435" /></p>
<p><span style="color: #008000;"><strong>my_app_widget_info.xml</strong></span> is an xml file that contains various properties for your widget such as refresh time, preview image, minimum height/width, etc.</p>
<p><strong><span style="color: #0000ff;">my_app_widget_info.xml</span></strong></p>
<pre><;?xml version="1.0" encoding="utf-8"?>;
<;appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
 android:initialKeyguardLayout="@layout/my_app_widget"
 android:initialLayout="@layout/my_app_widget"
 android:minWidth="110dp"
 android:minHeight="110dp"
 android:previewImage="@drawable/example_appwidget_preview"
 android:resizeMode="horizontal|vertical"
 android:updatePeriodMillis="86400000"
 android:widgetCategory="home_screen">;
 
<;/appwidget-provider>;</pre>
<p class="p1">A quick look for these properties:</p>
<ul class="ul1">
<li class="li1"><span style="color: #0000ff;"><strong> initialLayout</strong></span>: points to the layout resource that defines the App Widget layout.</li>
<li class="li1"><strong><span style="color: #0000ff;"> minHeight and minWidth</span></strong>: The values for the <span style="color: #008000;"><strong><span class="s1">minWidth</span></strong></span> and <span style="color: #008000;"><strong><span class="s1">minHeight</span></strong></span> attributes specify the minimum amount of space the App Widget consumes(in dp) by default. Every 60dp means 1 cell in android home-screen. The widget takes min 1&#215;1 cell(s).</li>
<li class="li1"><span style="color: #0000ff;"><strong> previewImage</strong></span><i>: </i> specifies a preview of what the app widget will look like after it&#8217;s configured, which the user sees when selecting the app widget.</li>
<li class="li1"><span style="color: #0000ff;"><strong> resizeMode</strong></span><i>:</i> specifies the rules by which a widget can be resized. You use this attribute to make homescreen widgets resizeable—horizontally, vertically, or on both axes.</li>
<li class="li1"><strong><span style="color: #0000ff;"> updatePeriodMillis</span></strong><i>:</i> The widget’s update method is called when the specified time is reached in a millisecond.</li>
<li class="li1"><strong><span style="color: #0000ff;"> widgetCategory</span></strong><i>:</i> declares whether your App Widget can be displayed on the home screen (<span class="s1">home_screen</span>), the lock screen (<span class="s1">keyguard</span>), or both.</li>
</ul>
<h3 class="p2"><span style="color: #000080;"><b>Widget-Java File</b></span></h3>
<p>It creates a java file which defines the basic methods that allow you to programmatically interface with the App Widget, based on broadcast events.</p>
<p><strong><span style="color: #0000ff;">MyAppWidget.Java</span></strong></p>
<pre>package com.example.androidwidgetexample;

import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.Context;
import android.widget.RemoteViews;

<strong><span style="color: #008000;">/**</span></strong>
<strong><span style="color: #008000;"> * Implementation of App Widget functionality.</span></strong>
<strong><span style="color: #008000;"> */</span></strong>
public class MyAppWidget extends AppWidgetProvider {

 static void updateAppWidget(Context context, AppWidgetManager appWidgetManager,
 int appWidgetId) {

 CharSequence widgetText = context.getString(R.string.appwidget_text);
 <strong><span style="color: #008000;">// Construct the RemoteViews object</span></strong>
 RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.my_app_widget);
 views.setTextViewText(R.id.appwidget_text, widgetText);

 <strong><span style="color: #008000;">// Instruct the widget manager to update the widget</span></strong>
 appWidgetManager.updateAppWidget(appWidgetId, views);
 }

 @Override
 public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
 <strong><span style="color: #008000;">// There may be multiple widgets active, so update all of them</span></strong>
 for (int appWidgetId : appWidgetIds) {
 updateAppWidget(context, appWidgetManager, appWidgetId);
 }
 }

 @Override
 public void onEnabled(Context context) {
<span style="color: #008000;"><strong> // Enter relevant functionality for when the first widget is created</strong></span>
 }

 @Override
 public void onDisabled(Context context) {
 <strong><span style="color: #008000;">// Enter relevant functionality for when the last widget is disabled</span></strong>
 }
}

</pre>
<p>The <span style="color: #008000;"><strong>AppWidgetProvider</strong></span> class extends BroadcastReceiver as a convenience class to handle the App Widget broadcasts. The AppWidgetProvider receives only the event broadcasts that are relevant to the App Widget, such as when the App Widget is updated, deleted, enabled, and disabled.</p>
<p class="p1"><span style="color: #000000;">AppWidgetProvider</span> extends <span style="color: #0000ff;"><strong>BroadcastReceiver</strong></span>. <strong><span style="color: #008000;">MyAppWidget</span></strong> is indirectly a child of BroadcastReceiver. So our widget class is a receiver class.</p>
<h3 class="p2"><span style="color: #000080;"><b>Adding Created files in AndroidManifest.xml</b></span></h3>
<p>In <span style="color: #008000;"><strong>AndroidManifest</strong></span> file you can see the below changes:</p>
<p><strong><span style="color: #0000ff;">AndroidManifest.xml</span></strong></p>
<pre><;?xml version="1.0" encoding="utf-8"?>;
<;manifest xmlns:android="http://schemas.android.com/apk/res/android"
 package="com.example.androidwidgetexample">;

 <;application
 android:allowBackup="true"
 android:icon="@mipmap/ic_launcher"
 android:label="@string/app_name"
 android:roundIcon="@mipmap/ic_launcher_round"
 android:supportsRtl="true"
 android:theme="@style/AppTheme">;
 <;activity android:name=".MainActivity">;
 <;intent-filter>;
 <;action android:name="android.intent.action.MAIN" />;

 <;category android:name="android.intent.category.LAUNCHER" />;
 <;/intent-filter>;
 <;/activity>;

 <strong><span style="color: #008000;"><;receiver android:name=".MyAppWidget">;</span></strong>
<strong><span style="color: #008000;"> <;intent-filter>;</span></strong>
<strong><span style="color: #008000;"> <;action android:name="android.appwidget.action.APPWIDGET_UPDATE" />;</span></strong>
<strong><span style="color: #008000;"> <;/intent-filter>;</span></strong>

<strong><span style="color: #008000;"> <;meta-data</span></strong>
<strong><span style="color: #008000;"> android:name="android.appwidget.provider"</span></strong>
<strong><span style="color: #008000;"> android:resource="@xml/my_app_widget_info" />;</span></strong>
<strong><span style="color: #008000;"> <;/receiver>;</span></strong>
 <;/application>;

<;/manifest>;</pre>
<p class="p1">The <span style="color: #0000ff;"><strong><span class="s1"><;receiver>;</span></strong></span> element requires the <strong><span class="s1" style="color: #008000;">android:name</span></strong> attribute, which specifies the <span style="color: #000000;"><span class="s2">AppWidgetProvider</span></span> used by the App Widget.</p>
<p class="p1">The <strong><span style="color: #0000ff;"><span class="s1"><;intent-filter>;</span> </span></strong>element must include an <span style="color: #0000ff;"><strong><span class="s1"><;action>;</span></strong></span> element with the <strong><span class="s1" style="color: #008000;">android:name</span></strong> attribute specifies that the <span style="color: #000000;"><span class="s2">AppWidgetProvider</span></span> accepts the <strong><span style="color: #008000;"><a style="color: #008000;" href="https://developer.android.com/reference/android/appwidget/AppWidgetManager.html#ACTION_APPWIDGET_UPDATE"><span class="s2">ACTION_APPWIDGET_UPDATE</span></a></span></strong> broadcast.</p>
<p class="p1">The <strong><span class="s1" style="color: #0000ff;"><;meta-data>;</span></strong> element specifies the <strong><span style="color: #008000;"><span class="s2">AppWidgetProviderInfo</span></span></strong> resource and requires the following attributes:</p>
<ul class="ul1">
<li class="li1"><span style="color: #0000ff;"><strong><span class="s1">android:name</span></strong></span> &#8211; Specifies the metadata name. Use <span style="color: #008000;"><strong><span class="s1">android.appwidget.provider</span></strong></span> to identify the data as the <span style="color: #000000;"><span class="s2">AppWidgetProviderInfo</span></span> descriptor.</li>
<li class="li1"><strong><span class="s1" style="color: #0000ff;">android:resource</span></strong> &#8211; Specifies the <span style="color: #000000;"><span class="s2">AppWidgetProviderInfo</span></span> resource location.</li>
</ul>
<h3 class="p1"><span style="color: #000080;"><b>RemoteView</b></span></h3>
<p><strong><span style="color: #0000ff;"><a style="color: #0000ff;" href="https://developer.android.com/reference/android/widget/RemoteViews.html">RemoteView</a></span></strong> is a class that describes a view hierarchy that can be displayed in another process. The hierarchy is inflated from a layout resource file, and this class provides some basic operations for modifying the content of the inflated hierarchy.</p>
<!-- WP QUADS Content Ad Plugin v. 2.0.98.1 -->
<div class="quads-location quads-ad2" id="quads-ad2" style="float:none;margin:0px;">

</div>

<p class="p1">RemoteView is limited to support for the following layouts:</p>
<ul class="ul1">
<li class="li2"><strong><span class="s2" style="color: #0000ff;"> <a style="color: #0000ff;" href="https://developer.android.com/reference/android/widget/FrameLayout.html"><span class="s3">FrameLayout</span></a>,</span></strong></li>
<li class="li2"><strong><span class="s2" style="color: #0000ff;"> <a style="color: #0000ff;" href="https://developer.android.com/reference/android/widget/LinearLayout.html"><span class="s3">LinearLayout</span></a>,</span></strong></li>
<li class="li2"><strong><span class="s2" style="color: #0000ff;"> <a style="color: #0000ff;" href="https://developer.android.com/reference/android/widget/RelativeLayout.html"><span class="s3">RelativeLayout</span></a>,</span></strong></li>
<li class="li2"><strong><span class="s2" style="color: #0000ff;"> <a style="color: #0000ff;" href="https://developer.android.com/reference/android/widget/GridLayout.html"><span class="s3">GridLayout</span></a>.</span></strong></li>
<li class="li2"><strong><span class="s2" style="color: #0000ff;"><a style="color: #0000ff;" href="https://developer.android.com/reference/android/widget/ViewFlipper.html"><span class="s3">ViewFlipper</span></a></span></strong></li>
<li class="li2"><strong><span class="s2" style="color: #0000ff;"> <a style="color: #0000ff;" href="https://developer.android.com/reference/android/widget/ListView.html"><span class="s3">ListView</span></a></span></strong></li>
<li class="li2"><strong><span class="s2" style="color: #0000ff;"> <a style="color: #0000ff;" href="https://developer.android.com/reference/android/widget/GridView.html"><span class="s3">GridView</span></a></span></strong></li>
<li class="li2"><strong><span class="s2" style="color: #0000ff;"> <a style="color: #0000ff;" href="https://developer.android.com/reference/android/widget/StackView.html"><span class="s3">StackView</span></a></span></strong></li>
<li class="li2"><strong><span class="s2" style="color: #0000ff;"> <a style="color: #0000ff;" href="https://developer.android.com/reference/android/widget/AdapterViewFlipper.html"><span class="s3">AdapterViewFlipper</span></a></span></strong></li>
</ul>
<p class="p1">RemoteView supports only the following views::</p>
<ul class="ul1">
<li class="li2"><span style="color: #0000ff;"><strong><span class="s2"> <a style="color: #0000ff;" href="https://developer.android.com/reference/android/widget/AnalogClock.html"><span class="s3">AnalogClock</span></a>,</span></strong></span></li>
<li class="li1"><span style="color: #0000ff;"><strong><a style="color: #0000ff;" href="https://developer.android.com/reference/android/widget/Button.html"><span class="s3">Button</span></a>,</strong></span></li>
<li class="li2"><span style="color: #0000ff;"><strong><span class="s2"> <a style="color: #0000ff;" href="https://developer.android.com/reference/android/widget/Chronometer.html"><span class="s3">Chronometer</span></a>,</span></strong></span></li>
<li class="li2"><span style="color: #0000ff;"><strong><span class="s2"> <a style="color: #0000ff;" href="https://developer.android.com/reference/android/widget/ImageButton.html"><span class="s3">ImageButton</span></a>,</span></strong></span></li>
<li class="li2"><span style="color: #0000ff;"><strong><span class="s2"> <a style="color: #0000ff;" href="https://developer.android.com/reference/android/widget/ImageView.html"><span class="s3">ImageView</span></a>,</span></strong></span></li>
<li class="li2"><span style="color: #0000ff;"><strong><span class="s2"> <a style="color: #0000ff;" href="https://developer.android.com/reference/android/widget/ProgressBar.html"><span class="s3">ProgressBar</span></a>,</span></strong></span></li>
<li class="li2"><span style="color: #0000ff;"><strong><span class="s2"> <a style="color: #0000ff;" href="https://developer.android.com/reference/android/widget/TextView.html"><span class="s3">TextView</span></a></span></strong></span></li>
</ul>
<p class="p1"><strong><span style="color: #000080;">Note:</span></strong> If you use another view, RemoteView has no operation for the view.</p>
<h3 class="p1"><span style="color: #000080;"><b>Receiving App Widget broadcast Intents</b></span></h3>
<p class="p2"><strong><span class="s1" style="color: #008000;">AppWidgetProvider</span></strong> is just a convenience class. If you would like to receive the App Widget broadcasts directly, you can implement your own <span style="color: #008000;"><strong><span class="s1">BroadcastReceiver</span></strong></span> or override the <strong><span class="s1" style="color: #008000;">onReceive(Context, Intent)</span></strong> callback. The Intents you need to care about are as follows:</p>
<ul class="ul1">
<li class="li3"><strong><span class="s2" style="color: #0000ff;"><a style="color: #0000ff;" href="https://developer.android.com/reference/android/appwidget/AppWidgetManager.html#ACTION_APPWIDGET_UPDATE"><span class="s3">ACTION_APPWIDGET_UPDATE</span></a></span></strong></li>
<li class="li3"><strong><span class="s2" style="color: #0000ff;"><a style="color: #0000ff;" href="https://developer.android.com/reference/android/appwidget/AppWidgetManager.html#ACTION_APPWIDGET_DELETED"><span class="s3">ACTION_APPWIDGET_DELETED</span></a></span></strong></li>
<li class="li3"><strong><span class="s2" style="color: #0000ff;"><a style="color: #0000ff;" href="https://developer.android.com/reference/android/appwidget/AppWidgetManager.html#ACTION_APPWIDGET_ENABLED"><span class="s3">ACTION_APPWIDGET_ENABLED</span></a></span></strong></li>
<li class="li3"><strong><span class="s2" style="color: #0000ff;"><a style="color: #0000ff;" href="https://developer.android.com/reference/android/appwidget/AppWidgetManager.html#ACTION_APPWIDGET_DISABLED"><span class="s3">ACTION_APPWIDGET_DISABLED</span></a></span></strong></li>
<li class="li3"><strong><span class="s2" style="color: #0000ff;"><a style="color: #0000ff;" href="https://developer.android.com/reference/android/appwidget/AppWidgetManager.html#ACTION_APPWIDGET_OPTIONS_CHANGED"><span class="s3">ACTION_APPWIDGET_OPTIONS_CHANGED</span></a></span></strong></li>
</ul>
<h3 class="p1"><span style="color: #000080;"><b>Understanding Override Methods</b></span></h3>
<p class="p2">The AppWidgetProvider receives only the event broadcasts that are relevant to the App Widget, such as when the App Widget is updated, deleted, enabled, and disabled. When these broadcast events occur, the AppWidgetProvider receives the following method calls:</p>
<p><span style="color: #0000ff;"><strong><span class="s2">onUpdate()</span>:</strong></span></p>
<p><strong><span style="color: #008000;">onUpdate()</span></strong> is called to update the App Widget at intervals defined by the updatePeriodMillis attribute in the <strong><span style="color: #008000;">AppWidgetProviderInfo</span></strong>.</p>
<p class="p1">This method is also called when each App Widget is added to a host (unless you use a configuration Activity), so it should perform the essential setup, such as define event handlers for Views (For example, if you want an App Widget with a button that launches an Activity when clicked) and start a temporary <span class="s1">Service</span>, if necessary.</p>
<p class="p1">If you have declared a configuration Activity, <span style="color: #008000;"><b>this method is not called</b></span> when the user adds the App Widget, but is called for the subsequent updates. It is the responsibility of the configuration Activity to perform the first update when the configuration is done.</p>
<p><strong><span style="color: #0000ff;"><span class="s2">onAppWidgetOptionsChanged()</span> :</span></strong></p>
<p><span style="color: #008000;"><strong><span class="s2">onAppWidgetOptionsChanged()</span></strong></span> is called when the widget is first placed and also whenever the widget is resized. You can use this callback to show or hide content based on the widget&#8217;s size ranges.</p>
<p><img class=" size-full wp-image-1434 aligncenter" src="https://c1ctech.com/wp-content/uploads/2019/12/widgets_resizing1.png" alt="widgets_resizing1" width="1017" height="101" /></p>
<p><strong><span style="color: #0000ff;"><span class="s2">onDeleted(Context, int[])</span>:</span></strong></p>
<p><span style="color: #008000;"><strong><span class="s2">onDeleted()</span></strong></span> is called every time an App Widget is deleted from the App Widget host.</p>
<p><span class="s2"><span style="color: #0000ff;"><strong>onEnabled(Context)</strong></span></span><span style="color: #0000ff;"><strong>:</strong></span></p>
<p><span class="s2" style="color: #008000;"><strong>onEnabled()</strong></span> is called when an instance the App Widget is created for the first time.</p>
<p class="p1">For example, if the user adds two instances of your App Widget, this is only called the first time. If you need to open a new database or perform other setup that only needs to occur once for all App Widget instances, then this is a good place to do it.</p>
<p><strong><span style="color: #0000ff;"><span class="s2">onDisabled(Context)</span>:</span> </strong></p>
<p><span style="color: #008000;"><strong><span class="s2">onDisabled()</span></strong></span> is called when the last instance of your App Widget is deleted from the App Widget host.</p>
<p class="p1">This is where you should clean up any work done in <strong><span class="s1" style="color: #008000;">onEnabled()</span></strong>, such as delete a temporary database.</p>
<p><span style="color: #0000ff;"><strong><span class="s2">onReceive(Context, Intent)</span>:</strong></span></p>
<p><span style="color: #008000;"><strong><span class="s2">onReceive()</span></strong></span> is called for every broadcast and before each of the above callback methods.</p>
<p class="p1">You normally don&#8217;t need to implement this method because the default <span style="color: #008000;"><strong>AppWidgetProvider</strong></span> implementation filters all App Widget broadcasts and calls the above methods as appropriate.</p>
<p class="p2">Confused? No problem. The below GIF shows you when these methods will call.</p>
<p><img class=" size-full wp-image-1430 aligncenter" src="https://c1ctech.com/wp-content/uploads/2019/12/appwidget_lifecycle_methods.gif" alt="appwidget_lifecycle_methods" width="542" height="494" /></p>
<p>Whenever we add/remove the app widget, the two methods <strong><span style="color: #008000;">onUpdate()</span></strong> and <span style="color: #008000;"><strong>onDeleted()</strong> </span>will call for each app widget instance. But<span style="color: #008000;"><strong> onEnabled()</strong></span> will call only when an instance the App Widget is created for the first time and <span style="color: #008000;"><strong>onDisabled()</strong></span> will only be called when the last instance of your App Widget is deleted.</p>
<p><img class=" size-full wp-image-1431 aligncenter" src="https://c1ctech.com/wp-content/uploads/2019/12/appwidget_lifecycle_demo.gif" alt="appwidget_lifecycle_demo" width="522" height="514" /></p>
<p>In the above GIF you can see <strong><span style="color: #008000;">onEnabled()</span></strong> is called only for the first app widget instance(Creation) and <span style="color: #008000;"><strong>onDisabled()</strong></span> is called only for the last instance of the App Widget(deletion).</p>
<h3 class="p1"><span style="color: #000080;"><b>Widget updates</b></span></h3>
<p class="p2">A widget gets its data on periodic updates. There are two methods to update a widget, one is based on an <span style="color: #008000;"><strong>XML configuration file</strong></span> and the other is based on the Android <span style="color: #008000;"><strong>AlarmManager</strong></span> service.</p>
<p class="p2">In the widget configuration file, you can specify a fixed update interval. If the device is asleep when it is time for an update (as defined by updatePeriodMillis), then the device will wake up in order to perform the update and call your broadcast receiver to update the widget. The minimum update interval is 1800000 milliseconds (30 minutes). If you don&#8217;t update more than once per hour, this probably won&#8217;t cause significant problems for the battery life.</p>
<p>If, however, you need to update more frequently and/or you do not need to update while the device is asleep, then you can instead perform updates based on an alarm that will not wake the device. To do so, set an alarm with an Intent that your <strong><span style="color: #008000;">AppWidgetProvider</span></strong> receives, using the <strong><span style="color: #0000ff;"><a style="color: #0000ff;" href="https://developer.android.com/reference/android/app/AlarmManager.html"><span class="s1">AlarmManager</span></a></span>.</strong></p>
<p>Set the alarm type to either <strong><span style="color: #008000;"><a style="color: #008000;" href="https://developer.android.com/reference/android/app/AlarmManager.html#ELAPSED_REALTIME"><span class="s1">ELAPSED_REALTIME</span></a></span></strong> or <strong><span style="color: #008000;"><a style="color: #008000;" href="https://developer.android.com/reference/android/app/AlarmManager.html#RTC"><span class="s1">RTC</span></a>,</span></strong> which will only deliver the alarm when the device is awake. Then set updatePeriodMillis to zero (&#8220;0&#8221;).</p>
<h3><strong><span style="color: #000080;">Adding Code in Widget&#8217;s Java Class</span></strong></h3>
<p>Let&#8217;s create a simple example, demonstrating the use of application Widget. It creates a basic widget application that will be opening a website on widget clicks.</p>
<p>Let’s add some code to our widget class.</p>
<p><span style="color: #0000ff;"><strong>MyAppWidget.java</strong></span></p>
<pre>public class MyAppWidget extends AppWidgetProvider {

 static void updateAppWidget(Context context, AppWidgetManager appWidgetManager,
 int appWidgetId) {

 <span style="color: #008000;"><strong>// Construct an Intent object includes web adresss.</strong></span>
 Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://c1ctech.com"));

 <span style="color: #008000;"><strong>// In widget we are not allowing to use intents as usually. </strong></span>
<span style="color: #008000;"><strong> //We have to use PendingIntent instead of 'startActivity'</strong></span>
 PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);

 <strong><span style="color: #008000;">// Construct the RemoteViews object</span></strong>
 RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.my_app_widget);
 views.setOnClickPendingIntent(R.id.appwidget_text, pendingIntent);

 <strong><span style="color: #008000;">// Instruct the widget manager to update the widget</span></strong>
 appWidgetManager.updateAppWidget(appWidgetId, views);

 }

 @Override
 public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {

 <span style="color: #008000;"><strong>// There may be multiple widgets active, so update all of them</strong></span>
 for (int appWidgetId : appWidgetIds) {
 updateAppWidget(context, appWidgetManager, appWidgetId);
 }
 }


}</pre>
<p>As we can see there is an override method <strong><span style="color: #0000ff;">onUpdate</span></strong> which is called when the specified time defined in <strong><span style="color: #008000;">updatePeriodMillis</span></strong> attribute has reached<em class="iq">.</em></p>
<p>After updating your AppWidgetProvider class with the above code run your app.</p>
<p><img class="alignnone wp-image-1517" src="https://c1ctech.com/wp-content/uploads/2020/02/Screenshot_1580809560.png" alt="Screenshot_1580809560" width="313" height="556" /> <img class="alignnone wp-image-1518" src="https://c1ctech.com/wp-content/uploads/2020/02/Screenshot_1580809494.png" alt="Screenshot_1580809494" width="314" height="558" /></p>
<p> ;</p>
<p>I hope this article will help you in understanding what is App Widget, its lifecycle and also how to add widgets into our application without doing more efforts.

