It is from a tutorial I did a while ago and is very very simple.
Main.xml
You may have to play around with the text shadows and colors to make the countdown itself stand out.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:id="@+id/RelativeLayout1"
android:layout_gravity="center_horizontal"
android:layout_height="fill_parent"
android:background="@drawable/skyrim2">
<TextView android:paddingTop="35dip"
android:layout_alignParentBottom="true"
android:id="@+id/dayText"
android:layout_height="wrap_content"
android:text="@string/hello"
android:textColor="#ffffff"
android:layout_width="fill_parent"
android:shadowColor="#000000"
android:shadowRadius="3.5"
android:shadowDx="2" android:textStyle="bold"
android:shadowDy="2" android:gravity="center">
</TextView>
</RelativeLayout>
DaysToSkyrim.java
So obviously the date you want to count down to is saved in the calendar
public class DaysToSkyrim extends AppWidgetProvider {
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
int[] appWidgetIds) {
Timer timer = new Timer();
timer.scheduleAtFixedRate(new MyTime(context, appWidgetManager), 1, 60000);
}
private class MyTime extends TimerTask {
RemoteViews remoteViews;
AppWidgetManager appWidgetManager;
ComponentName thisWidget;
public MyTime(Context context, AppWidgetManager appWidgetManager) {
this.appWidgetManager = appWidgetManager;
remoteViews = new RemoteViews(context.getPackageName(), R.layout.main);
thisWidget = new ComponentName(context, DaysToSkyrim.class);
}
@Overridepublic void run() {
Date date1 = new Date();
Calendar calendar = new GregorianCalendar(2011,10,11);
long days = (((calendar.getTimeInMillis()- date1.getTime())/1000))/86400;
remoteViews.setTextViewText(R.id.dayText,""+ days+" Days");
appWidgetManager.updateAppWidget(thisWidget, remoteViews);
}
}
}
Calendar calendar = new GregorianCalendar(2011,10,11);
The way the calendar is set up with January being 0. means that the month you want to count down to, in this case November which would be the 11th month, is actually counted as the 10th month.
Hence 2011,10,11
This should get you something looking like this
Full source can be downloaded Here.
No comments:
Post a Comment