Search code examples
androidlistviewnotificationsremoteview

How to create Listview with help of remoteview?


I am trying to make dynamic listview in widget with help of remoteview, as I wanted this listview of application icons in a widget. I want to show incoming notifications from all apps which are separated application wise. I want to create standing notification list and when user clicks on application icon from listview, that particular notification will be displayed. I am using API 19 for getting all notification and also succeeded but I don't know how I can create Listview in widget with Remoteview and also with drawables(Icons).


Solution

  • Have you search or try with other examples having ListView in widget? Please check the Weather Widget demo available on github.

    Code:

        public class MyWidgetProvider extends AppWidgetProvider {
    
        private static HandlerThread sWorkerThread;
        private static Handler sWorkerQueue;
    
        public MyWidgetProvider() {
            // Start the worker thread
            sWorkerThread = new HandlerThread("MyWidgetProvider-worker");
            sWorkerThread.start();
            sWorkerQueue = new Handler(sWorkerThread.getLooper());
        }
    
        public void onUpdate(Context context, final AppWidgetManager appWidgetManager, int[] appWidgetIds) {
            for (int i = 0; i < appWidgetIds.length; ++i) {
                ...
                final RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget_layout);
                views.setRemoteAdapter(R.id.lvWidget, svcIntent);
    
                sWorkerQueue.postDelayed(new Runnable() {
    
                    @Override
                    public void run() {
                        // TODO Auto-generated method stub
                        views.setScrollPosition(R.id.list, 3);
                        appWidgetManager.partiallyUpdateAppWidget(appWidgetIds[i], views);
                    }
    
                }, 1000);
    
                appWidgetManager.updateAppWidget(appWidgetIds[i], views);
                ...
            }
        }
    }
    

    Which will behave like below:

    enter image description here

    Please check the github project and above sample code.