I have been trying to implement widget for my application with GridView . I am new to Android Development. I have searched a lot on internet but could not find anything helpful. I am kind of stuck here , as this is not showing anything on my GridView . Any help would be appreciated.
Thanks in advance !
This is my Code
public class MainActivity extends AppWidgetProvider{
public static final String TOAST_ACTION = "com.example.widgetdevelopment.TOAST_ACTION";
public static final String EXTRA_ITEM = "com.example.widgetdevelopment.EXTRA_ITEM";
@Override
public void onReceive(Context context, Intent intent) {
AppWidgetManager mgr = AppWidgetManager.getInstance(context);
if (intent.getAction().equals(TOAST_ACTION)) {
int appWidgetId = intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID,
AppWidgetManager.INVALID_APPWIDGET_ID);
int viewIndex = intent.getIntExtra(EXTRA_ITEM, 0);
Toast.makeText(context, "Touched view " + viewIndex, Toast.LENGTH_SHORT).show();
}
super.onReceive(context, intent);
}
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
int[] appWidgetIds) {
for (int i = 0; i < appWidgetIds.length; ++i) {
Intent intent = new Intent(context, GridWidgetService.class);
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetIds[i]);
intent.setData(Uri.parse(intent.toUri(Intent.URI_INTENT_SCHEME)));
RemoteViews rv = new RemoteViews(context.getPackageName(), R.layout.activity_main);
rv.setRemoteAdapter(appWidgetIds[i], R.id.gridView1, intent);
rv.setEmptyView(R.id.gridView1, R.id.empty_view);
Intent toastIntent = new Intent(context, MainActivity.class);
toastIntent.setAction(MainActivity.TOAST_ACTION);
toastIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetIds[i]);
intent.setData(Uri.parse(intent.toUri(Intent.URI_INTENT_SCHEME)));
PendingIntent toastPendingIntent;
toastPendingIntent=PendingIntent.getBroadcast(context,0,toastIntent,PendingIntent.FLAG_UPDATE_CURRENT);
rv.setPendingIntentTemplate(R.id.gridView1, toastPendingIntent);
appWidgetManager.updateAppWidget(appWidgetIds[i], rv);
}
super.onUpdate(context, appWidgetManager, appWidgetIds);
}
}
This is my RemoteViewServices Class
public class GridWidgetService extends RemoteViewsService{
@Override
public RemoteViewsFactory onGetViewFactory(Intent intent) {
// TODO Auto-generated method stub
return new GridRemoteViewsFactory(this.getApplicationContext(), intent);
}
}
public class GridRemoteViewsFactory implements RemoteViewsFactory {
private static final int mCount = 10;
private List<WidgetItem> mWidgetItems = new ArrayList<WidgetItem>();
private Context mContext;
private int mAppWidgetId;
public GridRemoteViewsFactory(Context context, Intent intent) {
mContext = context;
mAppWidgetId = intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID,
AppWidgetManager.INVALID_APPWIDGET_ID);
}
@Override
public void onCreate() {
for (int i = 0; i < mCount; i++) {
mWidgetItems.add(new WidgetItem(i + "!"));
}
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
@Override
public void onDataSetChanged() {
// TODO Auto-generated method stub
}
@Override
public void onDestroy() {
// mWidgetItems.clear();
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return mCount;
}
@Override
public RemoteViews getViewAt(int position) {
RemoteViews rv = new RemoteViews(mContext.getPackageName(), R.layout.widget_item);
rv.setTextViewText(R.id.widget_item, mWidgetItems.get(position).getSomething());
Bundle extras = new Bundle();
extras.putInt(MainActivity.EXTRA_ITEM, position);
Intent fillInIntent = new Intent();
fillInIntent.putExtras(extras);
rv.setOnClickFillInIntent(R.id.widget_item, fillInIntent);
return rv;
}
@Override
public RemoteViews getLoadingView() {
// TODO Auto-generated method stub
return null;
}
@Override
public int getViewTypeCount() {
// TODO Auto-generated method stub
return 2;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
@Override
public boolean hasStableIds() {
// TODO Auto-generated method stub
return true;
}
}
This is my Object class .
public class WidgetItem {
String something;
public WidgetItem(String something) {
super();
this.something = something;
}
public String getSomething() {
return something;
}
public void setSomething(String something) {
this.something = something;
}
}
Widget_item XML
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.widgetdevelopment.MainActivity" >
<TextView
android:id="@+id/widget_item"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
</RelativeLayout>
Main_Activity Layout (this layout is the layout of the Widget containing GridView )
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="top"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="@string/website"
android:textAppearance="?android:attr/textAppearanceMedium" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView1"
android:layout_below="@+id/textView1"
android:text="@string/app_name" />
<GridView
android:id="@+id/gridView1"
android:layout_width="150dp"
android:layout_height="100dp"
android:layout_alignLeft="@+id/button1"
android:layout_alignParentRight="true"
android:layout_below="@+id/button1"
android:numColumns="3" >
</GridView>
<TextView
android:id="@+id/empty_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:textColor="#ffffff"
android:textStyle="bold"
android:text="empty_view_text"
android:textSize="20sp" />
</RelativeLayout>
onGetViewFactory
returns null and that you have no layout with your 'MainActivity'? Do you complete this tutorial? vogella.com/tutorials/AndroidWidgets/article.html – Jovitta