Android - AppWidgetManager (HelloWidget)Tutorial

Basic Tutorials concerning: GUI, Views, Activites, XML, Layouts, Intents, ...

Android - AppWidgetManager (HelloWidget)Tutorial

Postby desuvinodkumar » Thu Jul 30, 2009 10:49 am

HI

A Small tutorial for android widget provider example......

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import java.util.Timer;
import java.util.TimerTask;

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

public class HelloWidget extends AppWidgetProvider {

private Date currentTime;

@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
int[] appWidgetIds) {
Toast.makeText(context, "onUpdate", Toast.LENGTH_SHORT).show();

Timer timer = new Timer();
timer.scheduleAtFixedRate(new MyTime(context, appWidgetManager), 1, 1000);

super.onUpdate(context, appWidgetManager, appWidgetIds);
}

private class MyTime extends TimerTask {

RemoteViews remoteViews;
AppWidgetManager appWidgetManager;
ComponentName thisWidget;
DateFormat format = SimpleDateFormat.getTimeInstance(SimpleDateFormat.MEDIUM, Locale.getDefault());

public MyTime(Context context, AppWidgetManager appWidgetManager) {
this.appWidgetManager = appWidgetManager;
remoteViews = new RemoteViews(context.getPackageName(), R.layout.main);
thisWidget = new ComponentName(context, HelloWidget.class);
}

@Override
public void run() {
currentTime = new Date();

remoteViews.setTextViewText(R.id.widget_textview, "Time = " + format.format(currentTime));
appWidgetManager.updateAppWidget(thisWidget, remoteViews);
}
}

@Override
public void onReceive(Context context, Intent intent) {

// v1.5 fix that doesn't call onDelete Action
final String action = intent.getAction();
if (AppWidgetManager.ACTION_APPWIDGET_DELETED.equals(action)) {
final int appWidgetId = intent.getExtras().getInt(
AppWidgetManager.EXTRA_APPWIDGET_ID,
AppWidgetManager.INVALID_APPWIDGET_ID);
if (appWidgetId != AppWidgetManager.INVALID_APPWIDGET_ID) {
this.onDeleted(context, new int[] { appWidgetId });
}
} else {
super.onReceive(context, intent);
}
}

@Override
public void onDeleted(Context context, int[] appWidgetIds) {
Toast.makeText(context, "onDelete", Toast.LENGTH_SHORT).show();
super.onDeleted(context, appWidgetIds);
}

}



Layout/main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:orientation="vertical"
android:background="@drawable/widget_bg_normal"
android:layout_gravity="center"
android:layout_height="wrap_content">

<TextView android:id="@+id/widget_textview"
android:text="@string/widget_text"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_gravity="center_horizontal|center"
android:layout_marginTop="5dip"
android:padding="10dip"
android:textColor="@android:color/black" android:clickable="true"/>
</LinearLayout>



refference : http://developer.android.com/guide/topi ... index.html

It many be helpful.....
Attachments
screen_Shot [].png
screen_Shot [].png (56.05 KiB) Viewed 9173 times
Last edited by desuvinodkumar on Thu Mar 11, 2010 5:00 am, edited 1 time in total.
Vinod...
User avatar
desuvinodkumar
Senior Developer
Senior Developer
 
Posts: 102
Joined: Mon Mar 23, 2009 6:51 am
Location: India

Top

Postby ksp » Wed Nov 11, 2009 10:41 am

Hi,

Can you please share me with the manifest file content. I am starting with app widgets. but i am not able to get the starting point of it. I mean, from where is the appwidget called? when is the onUpdate() method called?

Or is there a separate way to deploy the appwidget onto the device.

Any help would be appreciated.

Thanks in advance.
ksp
Junior Developer
Junior Developer
 
Posts: 22
Joined: Thu Feb 28, 2008 12:50 pm

Postby mehool » Tue Feb 09, 2010 5:02 pm

Hey,
Nice code... did u write this code yourself? it is also nice to point to the actual source from where you got the code from....
mehool
Freshman
Freshman
 
Posts: 3
Joined: Mon Feb 01, 2010 7:43 pm

Re: Android - AppWidgetManager (HelloWidget)Tutorial

Postby kerrhome » Thu Mar 11, 2010 4:52 am

desuvinodkumar wrote:Layout/mail.xml


Shouldn't that be:

Code: Select all
Layout/main.xml


?

s/mail/main/
kerrhome
Freshman
Freshman
 
Posts: 3
Joined: Wed Mar 10, 2010 5:36 pm
Location: Illinois

Re: Android - AppWidgetManager (HelloWidget)Tutorial

Postby shubh » Thu Jun 02, 2011 9:20 am

Hello , I want to make Custome Home screen for Android Mob. I seen example in Android API Demo.

But I want to behave it like "App Widget Host", so I can add Widget also.

Pls suggest me way to do.
Thank in Advance
User avatar
shubh
Experienced Developer
Experienced Developer
 
Posts: 83
Joined: Tue May 18, 2010 7:17 am
Location: India

Re: Android - AppWidgetManager (HelloWidget)Tutorial

Postby grifw1984 » Mon Jan 09, 2012 4:01 am

grifw1984
Freshman
Freshman
 
Posts: 7
Joined: Mon Jan 09, 2012 3:54 am

Top

Re: Android - AppWidgetManager (HelloWidget)Tutorial

Postby forrest5678 » Thu Jan 12, 2012 5:04 pm

I have seriously read it, very great! Thanks for your sharing!




........................................................................................
Workshop Cooling System

Pig Farm Ventilation

Pigsty Cooling

Workshop Ventilation Equipment

Greenhouse Cooling System

Greenhouse Ventilation System
forrest5678
Experienced Developer
Experienced Developer
 
Posts: 57
Joined: Wed Jan 04, 2012 6:07 pm

Top

Return to Novice Tutorials

Who is online

Users browsing this forum: No registered users and 6 guests