Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add drawables to a view programmatically from a URL but not in xml layout

Tags:

android

I'm currently trying to create an Android View object dynamically and then add this view to a widget based on some user selection from a gallery. The problem is that it does not have setters for drawable resources. The only way to set the images is if I predefine an xml layout. I cannot then set the user selected images.

Even though I can create a view it will not have the right properties set. I tried implementing my own and exposing the drawables but then ran in to the issue "Class Not Found" and I've read its impossible to get around.

Is it possible to provide something in the AttributeSet on the constructor?

Really struggling, I don't even need a custom view if I could find a way to set the options.

 android:drawable1="@drawable/predefined_image"
 android:drawable2="@drawable/predefined_image" 

And pass this via an AttributeSet since I know it just takes these and creates drawables from them.


Okay lets clarify the question a little,

I have an xml layout file

<AndroidSystemWidget
android:drawable1="@Drawable/drawable1"
android:drawable2="@Drawable/drawable2"
/>

Now I have 2 png files on the SD card /sdcard/pictures/image1.png /sdcard/pictures/image2.png

Now in the code there are no exposed setters for drawable1 or 2 there is only a single constructor

AndroidSystemWidget widget = new AndroidSystemWidget(Context, AttributeSet)

I want to create this AndroidSystemWidget in my widget configuration with the two png files above set to drawable1 and drawable2 and then set it via Views on the app widget provider.

like image 652
DeliveryNinja Avatar asked Mar 24 '11 23:03

DeliveryNinja


1 Answers

Your question is a little vague, so forgive me if I misunderstand, but you could simply use an ImageView and then set your image via setImageFoo calls:

imageView.setImageBitmap(bitmap);
imageView.setImageResource(R.drawable.my_image);
imageView.setImageDrawable(drawable);

If you're using a custom view and drawing using canvas calls, then you can use the various drawBitmap routines as well.

If you're looking to load images over the network as your title indicates, then you may check out this answer, which has a link to the DroidFu library as well.

like image 196
Matthew Willis Avatar answered Oct 06 '22 20:10

Matthew Willis