Android Widget Example
Android Widget are present on android home screen and remotely it can show some part of your activity while activiy is close or pause.
To create Android Widget we required to extends AppWidgetProvider in our activity, and override onUpdate() method.step1: Code for Activity java file:create an java file with in src/MyWidgetProvider.java.
package in.androidshivendra.androidwidgetapplication;
import java.util.Random;
import android.os.Bundle;
import android.app.Activity;
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.util.Log;
import android.view.Menu;
import android.widget.RemoteViews;
public class MyWidgetProvider extends AppWidgetProvider {
private static final String ACTION_CLICK = "ACTION_CLICK";
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
int[] appWidgetIds) {
// Get all ids
ComponentName thisWidget = new ComponentName(context,
MyWidgetProvider.class);
int[] allWidgetIds = appWidgetManager.getAppWidgetIds(thisWidget);
for (int widgetId : allWidgetIds) {
// Create some random data
int number = (new Random().nextInt(100));
RemoteViews remoteViews = new RemoteViews(context.getPackageName(),
R.layout.widget_layout);
Log.w("WidgetExample", String.valueOf(number));
// Set the text
remoteViews.setTextViewText(R.id.update, String.valueOf(number));
// Register an onClickListener
Intent intent = new Intent(context, MyWidgetProvider.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.update, pendingIntent);
appWidgetManager.updateAppWidget(widgetId, remoteViews);
}
}
}
create an style xml file in res/drawable/myshpae.xml
folder in application.
android:shape="rectangle" >
android:width="2dp"
android:color="#00fffa" />
android:angle="225"
android:endColor="#DDff00"
android:centerColor="#cccccc"
android:startColor="#DD000000" />
android:height="20dp"/>
create a layout xml file within res/layaout/widget_layout.xml and put a textview.
android:id="@+id/layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="8dip"
android:background="@drawable/myshpae" >
android:id="@+id/update"
style="@android:style/TextAppearance.Medium"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="4dip"
android:gravity="center_horizontal|center_vertical"
android:text="Static Text" />
create a xml file for widget information with in res/xml/widget_info.xml.
android:initialLayout="@layout/widget_layout"
android:minHeight="72dp"
android:minWidth="146dp"
android:updatePeriodMillis="2000"
>
update manifest file with these code
package="in.androidshivendra.androidwidgetapplication"
android:versionCode="1"
android:versionName="1.0" >
android:minSdkVersion="8"
android:targetSdkVersion="17" />
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
android:icon="@drawable/ic_launcher"
android:label="Example Widget"
android:name="in.androidshivendra.androidwidgetapplication.MyWidgetProvider" >
android:name="android.appwidget.provider"
android:resource="@xml/widget_info" />
No comments:
Post a Comment