Here i am presenting an idea of a widget on the main screen of the device.
download the code from http://www.mediafire.com/download.php?h2s0uw7wju7vd02
Creat a project and have main activity with name
1)JiteshActivity.java
package com.jitesh.uselesswidget;
import com.jitesh.uselesswidget.R;
import android.app.Activity;
import android.os.Bundle;
public class JiteshActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
}
2)UselessWidgetProvider .java
package com.jitesh.uselesswidget.widgetprovider;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONObject;
import com.jitesh.uselesswidget.R;
import com.jitesh.uselesswidget.settings.Preferences;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.preference.PreferenceManager;
import android.util.Log;
import android.widget.RemoteViews;
import android.widget.RemoteViews.RemoteView;
public class UselessWidgetProvider extends AppWidgetProvider {
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
int[] appWidgetIds) {
DefaultHttpClient client = new DefaultHttpClient();
HttpGet get = new HttpGet("http://search.twitter.com/search.json?q=twitter");
BasicResponseHandler handler = new BasicResponseHandler();
String response = null;
String text=null;
Date d=null;
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
try{
response = client.execute(get,handler);
if(response!=null){
JSONObject object = new JSONObject(response);
JSONArray array = object.getJSONArray("results");
if(array.length()>0){
JSONObject tweet = array.getJSONObject(0);
text = tweet.getString("text");
String dateStr = tweet.getString("created_at");
SimpleDateFormat sdf = new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss Z",Locale.US);
d = sdf.parse(dateStr);
Log.v(getClass().getName(),"Date: "+d+" Text: "+text);
Editor e = preferences.edit();
e.putString(Preferences.PREFERENCE_TWEET, text);
e.putLong(Preferences.PREFERENCE_TIME, d.getTime());
e.commit();
}
}
}
catch(Exception e){
e.printStackTrace();
}
SimpleDateFormat sdf2 = new SimpleDateFormat("d/MM/yy HH:mm");
if(d==null && text==null){
text = preferences.getString(Preferences.PREFERENCE_TWEET, "Indisponible en ce moment!");
Long dateLong = preferences.getLong(Preferences.PREFERENCE_TIME, -1);
if(dateLong!=-1)d=new Date(dateLong);
}
if(d!=null && text!=null){
for(int i = 0; i<appWidgetIds.length;i++){
int appWidgetId = appWidgetIds[i];
RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget);
remoteViews.setTextViewText(R.id.textViewText, text);
remoteViews.setTextViewText(R.id.textViewTime, sdf2.format(d));
appWidgetManager.updateAppWidget(appWidgetId, remoteViews);
}
}
}
}
3)Preferences .java
package com.jitesh.uselesswidget.settings;
public class Preferences {
public static final String PREFERENCE_TWEET = "com.jitesh.uselesswidget.preference.tweet";
public static final String PREFERENCE_TIME = "com.jitesh.uselesswidget.preference.time";
}
4)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:layout_height="fill_parent"
android:orientation="vertical"
android:gravity="center_horizontal|center_vertical"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
android:layout_margin="20dp"
style="@android:style/TextAppearance.Medium"
/>
</LinearLayout>
5) res/layout/widget.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/appwidget_bg" >
<ImageView
android:id="@+id/imageViewWidget"
android:layout_width="68dp"
android:layout_height="68dp"
android:src="@drawable/reynders"
android:layout_margin="8dp"
/>
<TextView
android:id="@+id/textViewText"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/imageViewWidget"
android:textColor="#FFFFFF"
android:layout_marginTop="4dp"/>
<TextView
android:id="@+id/textViewTime"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"
android:textColor="#BBBBBB"
android:layout_margin="4dp"
/>
</RelativeLayout>
7) res/xml/uselessappwidgetprovider.xml
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
android:minWidth="294dp"
android:minHeight="72dp"
android:updatePeriodMillis="1800000"
android:initialLayout="@layout/widget"
>
</appwidget-provider>
8)res/values/strings.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">Go to main screen and please make long press on the screen and select widget " twitter widget "</string>
<string name="app_name">Twitter Widget</string>
</resources>
9)manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.jitesh.uselesswidget"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="4" />
<uses-permission
android:name="android.permission.INTERNET"
/>
<application
android:icon="@drawable/icon"
android:label="@string/app_name" >
<activity
android:label="@string/app_name"
android:name=".JiteshActivity"
android:theme="@android:style/Theme.Light">
<intent-filter >
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name=".widgetprovider.UselessWidgetProvider">
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE"/>
</intent-filter>
<meta-data android:name="android.appwidget.provider"
android:resource="@xml/uselessappwidgetprovider"/>
</receiver>
</application>
</manifest>
copy the resources to drawable