Thursday, April 2, 2015

Android App Widgets| App Widgets in Android | Create App Widgets in Android

Hello Friends,

Today I am here with new learning "how to create widgets for your application"
like you see calender, calculator, Facebook, whether widgets on your phone in widget section.
It is very simple to create your own app widget, follow below step to make it done-




1)Create a demo project i.e.- AppWidgetDemo

2)Create a Widget Provider for  your application i.e.- MyProvider.java


package com.androidhub4you.app.widget;

import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.widget.RemoteViews;

public class MyProvider extends AppWidgetProvider {

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

        // Get all component
        ComponentName thisWidget = new ComponentName(context, MyProvider.class);
        int[] allWidgetIds = appWidgetManager.getAppWidgetIds(thisWidget);
        for (int widgetId : allWidgetIds) {

            //set the remote view to widget
            RemoteViews remoteViews = new RemoteViews(context.getPackageName(),
                    R.layout.activity_my_widget);
            // Set the text to textview you change it in runtime
            remoteViews.setTextViewText(R.id.my_text, "Manish");

            // Intent to set dynamic vaalues to views
            Intent intent = new Intent(context, MyProvider.class);

            intent.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
            intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, appWidgetIds);

            PendingIntent pendingIntent = PendingIntent.getBroadcast(context,
                    0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
            remoteViews.setOnClickPendingIntent(R.id.my_text, pendingIntent);
            appWidgetManager.updateAppWidget(widgetId, remoteViews);
        }
    }
}

3)Create a widget layout for your app inside res/layout/activity_my_widget.xml


<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/my_shape"
    tools:context="com.androidhub4you.app.widget.MainActivity" >

    <ImageView
        android:id="@+id/my_image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:src="@drawable/widget_logo" />

    <TextView
        android:id="@+id/my_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/my_image"
        android:layout_margin="2dp" />

</RelativeLayout>

4)Create an appwidget-provider inside res/xml/my_widget.xml

<?xml version="1.0" encoding="utf-8"?>
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
    android:initialLayout="@layout/activity_my_widget"
    android:minHeight="60dp"
    android:minWidth="400dp"
    android:updatePeriodMillis="200000" >

</appwidget-provider>

5)For the good look for you widgets create a shape inside res/drawable/my_shape.xml

<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle" >

    <stroke
        android:width="2dp"
        android:color="@android:color/white" />

    <gradient
        android:angle="225"
        android:endColor="@android:color/background_dark"
        android:startColor="@android:color/background_light" />

    <corners
        android:bottomLeftRadius="5dp"
        android:bottomRightRadius="5dp"
        android:topLeftRadius="5dp"
        android:topRightRadius="5dp" />

</shape>

6)and inside your manifest.xml call your reciver-

   <receiver
            android:name="MyProvider"
            android:icon="@drawable/widget_logo"
            android:label="My Widget Demo" >
            <intent-filter>
                <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
            </intent-filter>

            <meta-data
                android:name="android.appwidget.provider"
                android:resource="@xml/my_widget" />
        </receiver>


Reference URL-
https://developer.android.com/guide/topics/appwidgets/index.html

DOWNLOAD FULL CODE


Thanks,
Manish