GridView in android widgets
Asked Answered
M

1

7

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>
Milewski answered 7/7, 2014 at 5:55 Comment(9)
You have no question in your post:) Try this: developer.android.com/intl/ru/guide/topics/ui/layout/… androidhive.info/2012/02/android-gridview-layout-tutorial mkyong.com/android/android-gridview-exampleJovitta
My question is that is there any tutorial or steps to start? I have also looked into the Google's documentationMilewski
Thank you but brother i need to implement in widget. :) i have also gone through this tutorials but this is implementing StackView. vogella.com/code/com.example.android.stackwidget/src/com/…Milewski
What problems you still have after google's tutorial?Jovitta
I have done but not getting object value on gridview . i am gona edit my question. please help :)Milewski
Hmmm... Is it normal tha your onGetViewFactory returns null and that you have no layout with your 'MainActivity'? Do you complete this tutorial? vogella.com/tutorials/AndroidWidgets/article.htmlJovitta
Oops my bad ! onGetViewFactory returns new GridRemoteViewsFactory(this.getApplicationContext(), intent).Milewski
but this is still not working and tutorial you have suggested don't have my solution. This is just a simple widget tutorial with services to update widget :)Milewski
Hey can you help me?. I create gridview in appwidget same as like you..but gridview is does not update after count is increase or decrese.Please help me.Stripy
L
8

Have you registered service in the AndroidManifest.xml? Like this :

<service android:name="GridWidgetService"
   android:permission="android.permission.BIND_REMOTEVIEWS" />
Larsen answered 7/7, 2014 at 11:1 Comment(1)
Oops ! i forget , Thank You brother now this is working fine, Thank you So much You just saved my day.Milewski

© 2022 - 2024 — McMap. All rights reserved.