Search code examples
androiddynamicwidgetscrollviewappwidgetprovider

Dynamic layout in android Widget


Is it possible to create an dynamic layout in a AppWidgetProvider ? i only found tutorials to create widgets using XML, but it isn't possible to create views that way..

I want to draw images in row like this: https://i.sstatic.net/rgtil.jpg And add a horizontal scroll. Is it possible ?

Regards !

EDIT: (this is what i have so far.. but i still don't know how to add those images)

@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
        int[] appWidgetIds) {
    // TODO Auto-generated method stub
    super.onUpdate(context, appWidgetManager, appWidgetIds);
    final int N = appWidgetIds.length;
    for(int i = 0; i<N; i++){
        int awID = appWidgetIds[i];
        RemoteViews v = new RemoteViews(context.getPackageName(), R.layout.about);
        try
        {
            j = 1;
            Set<User> users = LFMRestConnection.getFriendsNowListening(user, 1440);;
            for (User u : users) {
                album_pic = loadBitmap(u.getImageURL().getImageURL(ImageURL.Size.LARGE));
                resized_a_p = android.graphics.Bitmap.createScaledBitmap(album_pic, (50), (50), true);
            //URLS
                url_to_enter[j]=u.getLastTrack().getArtistName();
                track_to_enter[j]=u.getLastTrack().getTrackName();
            //profile picture
                user_pic = loadBitmap(u.getImageURL().getImageURL(ImageURL.Size.LARGE));
                resized_u_p = android.graphics.Bitmap.createScaledBitmap(user_pic, (25), (25), true);
                j++; 
            }
        }
        catch(Exception e)
        {
            //startActivity(i);
            System.out.println("widget error");     
        }
        appWidgetManager.updateAppWidget(awID, v);
    }
}

Solution

  • Create a HorizontalScrollView in your xml layout, put a horizontal LinearLayout in it, and give the layout an ID by setting the id attribute

    android:id="@+id/imageScrollLayout"
    

    Then, in the onCreate for your activity, find the layout object

    LinearLayout imageScrollLayout = (LinearLayout)findViewById(R.id.imageScrollLayout); 
    

    To add new ImageViews to the layout dynamically, use

    ImageView newImageView=new ImageView();
    
    //set the ImageView image with one of the following:
    newImageView.setImageBitmap(bm);
    newImageView.setImageDrawable(drawable);
    newImageView.setImageResource(resId);
    
    //add the new ImageView to your layout
    imageScrollLayout.addView(newImageView);
    

    All the xml settings for the ImageView can be accessed in code, just set them after you set your image. The Google Android documentation is pretty good for finding all the relevant methods. Also, if you're adding images anywhere but the onCreate method for your activity, just remember to make imageScrollLayout a field in the activity, rather local in the onCreate method, so you can access it everywhere.

    EDIT: To implement this in an AppWidgetProvider, you need to override the onUpdate method. This method is called whenever the system is requesting an update for the widget. If the RemoteViews of the widget do need to be changed, you need to create a new set of views, and attach them to the widget using

    appWidgetManager.updateAppWidget(componentName, remoteViews)
    

    where appWidgetManager is a supplied argument to the onUpdate method. A similar process needs to be implemented in the onAppWidgetOptionsChanged() method. To supply the remoteViews argument, inflate the layout file in code using something to the effect of

    LayoutInflater myInflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);  
    final View myRootLayout = myInflater .inflate(R.layout.myLayout, null);
    

    after which you can use the normal

    myRootLayout.findViewById(R.id.myView);
    

    to find your layout and add ImageViews to it.

    After all that, I suspect that HorizontalScrollView is not supported as a RemoteView, making it hard to implement the horizontal scrolling you want. This has likely been a design decision by Google, as horizontal scrolling in an AppWidget would not work well with the horizontal scrolling between homescreens. Maybe use left and right edge buttons instead, to show a new page of images?