Tuesday, May 29, 2012

Android Widget

Android Widget

android widget it's like small application place to your home screen.
You can display any live updates, or messages etc..

XML CODE

<?xml version="1.0" encoding="utf-8"?>
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
    android:minWidth="294dp"
    android:minHeight="72dp"
    android:updatePeriodMillis="1000"
    android:initialLayout="@layout/main">
</appwidget-provider>

Screen Shot



WidgetProvider.Class

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.widget.RemoteViews;
public class WidgetProvider extends AppWidgetProvider {
  DateFormat df = new SimpleDateFormat("hh:mm:ss");
  public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
    final int N = appWidgetIds.length;
    Log.i("ExampleWidget",  "Updating widgets " + Arrays.asList(appWidgetIds));
    // Perform this loop procedure for each App Widget that belongs to this
    // provider
    for (int i = 0; i < N; i++) {
      int appWidgetId = appWidgetIds[i];
      // Create an Intent to launch ExampleActivity
      Intent intent = new Intent(context, AndroidMyFirstWidgetActivity.class);
      PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
      // Get the layout for the App Widget and attach an on-click listener
      // to the button
      RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.main);
     
     // views.setOnClickPendingIntent(R.id.widget1label, pendingIntent);
      // To update a label
     
      views.setTextViewText(R.id.widget1label, "Welcome to Mad Blog : " +df.format(new Date()));
      // Tell the AppWidgetManager to perform an update on the current app
      // widget
      appWidgetManager.updateAppWidget(appWidgetId, views);
    }
  }
}

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.widget"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="8" />
    <uses-permission android:name="android.permission.INTERNET"/>

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:name=".AndroidMyFirstWidgetActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <!-- <action android:name="android.intent.action.MAIN" />
 -->
               <!--  <category android:name="android.intent.category.LAUNCHER" /> -->
            </intent-filter>
        </activity>
        <receiver android:name=".WidgetProvider" android:label="Mad BLOG Widget">
  <intent-filter>
        <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
  </intent-filter>
  <meta-data android:name="android.appwidget.provider" android:resource="@layout/widgetprovider" />
</receiver>
       
    </application>

</manifest>


You Can download Source code HERE

2 comments:

  1. A widget displays an application's most important or timely information at a glance, on a user's Home screen.

    ReplyDelete
    Replies
    1. upload link for download code

      Delete

Check out this may be help you

Related Posts Plugin for WordPress, Blogger...