1

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.stack.imgur.com/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);
    }
}
Jason Costa
  • 73
  • 2
  • 12

1 Answers1

1

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?

skyrift
  • 743
  • 4
  • 12
  • Yes i know how to create a dynamic layout in a activity class, but i don't know how in a AppWidgetProvider.. your example is for a normal activity. Is it possible in a widget ? – Jason Costa Aug 28 '12 at 16:03
  • Might want to edit the question - all standard views that can be dropped onto a layout in an activity are called widgets. However, this is similiar when implementing a AppWidgetProvider, see edited answer. – skyrift Aug 28 '12 at 16:55
  • I updated the post with my code so far. I will now try to implement your answer ! brb :) – Jason Costa Aug 28 '12 at 17:39
  • I'm not sure how to make sense of this answer. What `Activity` are you talking about? And how do you access to `root` layout of the layout that you pass to your WidgetProvider (you're talking about Widgets right?) can you please take a look at my question here http://stackoverflow.com/questions/22582260/how-do-i-access-a-viewflipper-in-a-widget-to-populate-its-content-dynamically and see if you can adjust your answer to answer it? I suspect it would clarify the stuff that doesn't make sense to me in *this answer as well. – Yevgeny Simkin Mar 22 '14 at 19:21