How to use Android Widgets in Android Studio

Create a new project in Android Studio

File – New Android Project.
How to use Android Widgets in Android Studio
How to use Android Widgets in Android Studio

What a is Widget

A widget is a small gadget or control of your android appliaction palced on the home screen. Widget can be veru handy as the allow to put your favourite application on the screen in order to quickly access them.

Create a Android Widgets

Right click on src/ package-New-Widget-app widget

Creating AppWidgetProvider in XML

This is used to describe the metadata for an app Widget, such as the app Widget’s layout, update frequency, and the AppWidget provider class. This is always defined as XML file.

<?xml version="1.0" encoding="utf-8"?>
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
    android:initialKeyguardLayout="@layout/new_app_widget"
    android:initialLayout="@layout/new_app_widget"
    android:minHeight="40dp"
    android:minWidth="40dp"
    android:previewImage="@drawable/example_appwidget_preview"
    android:resizeMode="horizontal|vertical"
    android:updatePeriodMillis="86400000"
    android:widgetCategory="home_screen">
    </appwidget-provider>

Create the App widget Layout

<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" />

Use AppWidget Provider class

public class NewAppWidget extends AppWidgetProvider {
    @Override
    public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
        // There may be multiple widgets active, so update all of them
        final int N = appWidgetIds.length;
        for (int i = 0; i < N; i++) {
            updateAppWidget(context, appWidgetManager, appWidgetIds[i]);
        }
    }


    @Override
    public void onEnabled(Context context) {
        // Enter relevant functionality for when the first widget is created
    }

    @Override
    public void onDisabled(Context context) {
        // Enter relevant functionality for when the last widget is disabled
    }

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

        CharSequence widgetText = context.getString(R.string.appwidget_text);
        // Construct the RemoteViews object
        RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.new_app_widget);
        views.setTextViewText(R.id.appwidget_text, widgetText);
 // Create an Intent to launch MainActivity
        Intent intent = new Intent(context, MainActivity.class);
        PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
        views.setOnClickPendingIntent(R.id.appwidget_text,pendingIntent);

        // Instruct the widget manager to update the widget
        appWidgetManager.updateAppWidget(appWidgetId, views);
    }
}

AndroidManifest.xml

We need to register the app widget in your application manifest file. To do this, you will use the tag. This block of XML should be placed inside the application tag in application manifest.
The receiver name is the name of your app widget provider class implementation.

<receiver android:name=".NewAppWidget" >
            <intent-filter>
                <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
            </intent-filter>
            <meta-data
                android:name="android.appwidget.provider"
                android:resource="@xml/new_app_widget_info" />
        </receiver>

Related Posts

1 Comment

    Hi!

    Thanks in advance for your post about how to create a widget by using Android Studio.

    I also use this software in order to develop any Android applications.

    I’d like to ask you a question:
    Inside the ” updateAppWidget ” method, you mention a “MainActivity” class; so, is this a blank Activity or which does this activity contain in order to run the application?

    Regards.

Leave a comment

Hey, so you decided to leave a comment! That's great. Just fill in the required fields and hit submit. Note that your comment will need to be reviewed before its published.