Widget Creating in Android Studio

App Widget are miniature application views that can be embedded in other applications (such as the Home screen) and receive periodic updates. These views are referred to as Widgets in the user interface, and you can publish one with an AppWidget provider. An application component that is able to hold other App Widgets is called an AppWidget host.
The Basics
To create an AppWidget, you need the following:
AppWidgetProviderInfo
object- Describes the metadata for an AppWidget, such as the App Widget’s layout, update frequency, and the AppWidgetProvider class. This should be defined in XML.
AppWidgetProvider
class implementation- Defines the basic methods that allow you to programmatically interface with the AppWidget, based on broadcast events. Through it, you will receive broadcasts when the App Widget is updated, enabled, disabled and deleted.
Adding the AppWidgetProviderInfo Metadata
The AppWidgetProviderInfo
defines the essential qualities of an App Widget, such as its minimum layout dimensions, its initial layout resource, how often to update the App Widget, and (optionally) a configuration Activity to launch at create-time. Define the AppWidgetProviderInfo object in an XML resource using a single <appwidget-provider>
element and save it in the project’s res/xml/
folder.
<?xml version="1.0" encoding="utf-8"?> <appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:initialKeyguardLayout="@layout/widget" android:initialLayout="@layout/widget" android:minHeight="40dp" android:minWidth="250dp" android:previewImage="@drawable/ic_launcher" android:updatePeriodMillis="1000" android:widgetCategory="keyguard|home_screen" tools:ignore="UnusedAttribute" > </appwidget-provider>
Declaring an App Widget in the Manifest
First, declare the AppWidgetProvider
class in your application’s AndroidManifest.xml
file. For example:
<receiver android:name=".AppWidgetObject" android:label="@string/widget_name"> <intent-filter> <action android:name="android.appwidget.action.APPWIDGET_UPDATE" /> </intent-filter> <meta-data android:name="android.appwidget.provider" android:resource="@xml/widget_info" /> </receiver> <service android:name=".AppWidgetObject$UpdateService" />
Creating the App Widget Layout
You must define an initial layout for your AppWidget in XML and save it in the project’s res/layout/
directory. You can design your AppWidget using the View objects listed below, but before you begin designing your AppWidget, please read and understand the AppWidget Design Guidelines.
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="vertical"> <TextView android:textSize="30dp" android:id="@+id/idForTimeChange" android:layout_gravity="center" android:layout_width="wrap_content" android:layout_height="50dp" android:text="00 : 00 : 00"/> <TextView android:textColor="@color/colorAccentLight" android:layout_gravity="center" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="logicchip.com"/> </LinearLayout>
Using the AppWidgetProvider Class
The AppWidgetProvider
class extends BroadcastReceiver as a convenience class to handle the AppWidget broadcasts. The AppWidgetProvider receives only the event broadcasts that are relevant to the AppWidget, such as when the AppWidget is updated, deleted, enabled, and disabled. It also require a service to make constant update to the widget.
public class AppWidgetObject extends AppWidgetProvider { private Handler handler; private Runnable runnable; SimpleDateFormat simpleDateFormat; String time; Calendar calander; Context context; @Override public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) { context.startService(new Intent(context, UpdateService.class)); } public static class UpdateService extends Service { private Handler handler; private Runnable runnable; SimpleDateFormat simpleDateFormat; String time; Calendar calander; @Override public int onStartCommand(Intent intent, int flags, int startId) { TimeForUpdate(); return START_REDELIVER_INTENT; } @Nullable @Override public IBinder onBind(Intent intent) { return null; } public void TimeForUpdate(){ handler = new Handler(); runnable = new Runnable() { @Override public void run() { handler.postDelayed(this, 1000); try { calander = Calendar.getInstance(); simpleDateFormat = new SimpleDateFormat("hh : mm : ss"); time = simpleDateFormat.format(calander.getTime()); RemoteViews widgetUi = new RemoteViews(getPackageName(), R.layout.widget); widgetUi.setTextViewText(R.id.idForTimeChange,time); try { ComponentName widgetComponent = new ComponentName(getBaseContext(),AppWidgetObject.class); AppWidgetManager widgetManager = AppWidgetManager.getInstance(getBaseContext()); widgetManager.updateAppWidget(widgetComponent, widgetUi); Log.e("Widget", "Ok"); } catch (Exception e) { Log.e("widget", "Failed to update widget", e); } } catch (Exception e) { e.printStackTrace(); } } }; handler.postDelayed(runnable, 0); } } }