Passing values to view with the adapter - android

I've implemented (thanks to a tutorial) a way to show a custom listView, with images, names and description.
I used the simpleAdapter to pass the strings name and description to the textViews, and the int of an image's Id (the image is stored in R.drawable) to the imageView.
The problem is: now i should set the ImageViews to display images downloaded from the web, so I have drawables but i can't get IDs from them.
How can i customize the simpleAdapter to pass a Drawable to the imageView, and set the image with the setImageDrawable method?
Please be clear! Thanks

We need to use the viewBinder, as show here:
adapter.setViewBinder(new ViewBinder() {
public boolean setViewValue(View view, Object data,
String textRepresentation) {
if(data instanceof BitmapDrawable ){
((ImageView)view).setImageDrawable((Drawable)data);
return true;
}else{
return false;
}
}
});

Try This:
http://www.androidpeople.com/android-load-image-from-url-example/
Or to load from External media files you have already downloaded:
Bitmap myBitmap = BitmapFactory.decodeFile(“/sdcard/myImage.jpg”);
ImageView myImage = (ImageView) findViewById(R.id.imageToShow);
myImage.setImageBitmap(myBitmap);

This is quite difficult... so I would advise to use CommonsWare's ThumbnailAdapter.

Related

Let ImageView point to new ImageView object Android

I'm setting up an image cache and I have the following
ImageView nimage = (ImageView)v.findViewById(R.id.imView1);
ImageView value = imageCache.get(ei.imageURL);
if (value != null)
nimage = value; // this is the code I want to change
else {
new ImageLoadTask(ei.imageURL, nimage).execute();
imageCache.put(ei.imageURL ,nimage);
}
What do I replace the commented value with. I'm not sure why I can't let one imageView equal another imageView.
Your cache isn't caching the correct things.
You don't want to cache the ImageView itself- an instance of a View is tied to the Context in which it was created, and thus is typically not a great object to cache.
What it sounds like you actually want to cache is the image (the Bitmap object) that you download from the URL that you are providing to your ImageLoadTask. If you cache this, then you can simply call nimage.setImageBitmap() to use your cached bitmap instead of downloading it.

ImageSwitcher load images from url

I'm trying to show images from a URL in a ImageSwitcher. I've tried with:
myImageSwitcher.setImageURI(Uri.parse("http://miurl.com/images/foto01.jpg"));
but it said me this "error":
I/System.out﹕ resolveUri failed on bad bitmap uri: http://miurl.com/images/foto01.jpg
Thanks
I know it is late but because I found a better solution for this, would love to share it with all :)
I had to look at the source code of the default ImageSwitcher and know how it works. Whenever we use ImageSwitcher we set a ViewFactory to it. This ViewFactory creates Views which are then used in the ImageSwitcher. Usually we create an ImageView in the ViewFactory. This ImageView is then used in the ImageSwitcher class to draw the view with the image source specified. Below is the setImageResource() method of the ImageSwitcher class.
public void setImageResource(#DrawableRes int resid){
ImageView image = (ImageView)this.getNextView();
image.setImageResource(resid);
showNext();
}
Let's focus on the first and second line of the method.
ImageView image = (ImageView)this.getNextView();
image.setImageResource(resid);
this.getNextView() gets the view from the ViewFactory and casts the view into an ImageView. And then, the Drawable resource is set to it.
Solution
Now, with these information, let's get to the solution! I'm using Volley's NetworkImageView , but you can use Picasso or Glide too! You just have to change the code a bit.
We need to create a custom ImageSwitcher. Here's my code
MyImageSwitcher.java
public class MyImageSwitcher extends ImageSwitcher {
public MyImageSwitcher(Context context) {
super(context);
}
public MyImageSwitcher(Context context, AttributeSet attrs) {
super(context, attrs);
}
public void setImageUrl(String url) {
NetworkImageView image = (NetworkImageView) this.getNextView();
image.setImageUrl(url, AppController.getInstance().getImageLoader());
showNext();
}
}
Notice the setImageUrl() method. We cast the view we get from getNextView() to a NetworkImageView and set the Url to it. If you want to use Picasso, then just cast it into an ImageView and then use Picasso as it needs to be used.
Activity
MyImageSwitcher imageSwitcher = (MyImageSwitcher) findViewById(R.id.imageSwitcher);
imageSwitcher.setFactory(new ViewSwitcher.ViewFactory() {
public View makeView() {
NetworkImageView myView = new NetworkImageView(getApplicationContext());
myView.setScaleType(ImageView.ScaleType.CENTER_CROP);
myView.setLayoutParams(new ImageSwitcher.LayoutParams(imageSwitcher.getLayoutParams()));
return myView;
}
});
If you want to use Picasso, then just use an ImageView.
Layout
<xyz.farhanfarooqui.loadingimages.MyImageSwitcher
android:id="#+id/imageSwitcher"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
AndroidManifest.xml
Don't forget to include android.permission.INTERNET to your AndroidManifest.xml
Done!
Whenever you want to change the image, do it this way,
imageSwitcher.setImageUrl("http://miurl.com/images/foto01.jpg");
I've solucionated this problem using SliderLayot (https://github.com/daimajia/AndroidImageSlider), but I'll try to do with ImageSwitcher too.
I was trying to achieve the same thing when I landed here. I know it's and old question, but since I don't see an answer, I'll tell you the solution that worked for me.
First of all, I downloaded the image/s that I wanted to show in the imageswitcher and return them as a Bitmap using this code:
InputStream in = new java.net.URL(url).openStream();
Bitmap bitmap = BitmapFactory.decodeStream(in);
Then I set the bitmap in the imageswitcher using imageSwitcher.setImageDrawable(new BitmapDrawable(getResources(), bitmap)).
To use java.net.URL(url).openStream(), you'll need to add <uses-permission android:name="android.permission.INTERNET"> to your AndroidManifest.xmlfile.
Have in mind that you could change the download code to make it better (checking for timeouts for example), but this is just a basic solutions to start from.
Another thing about what you were using, I think you can't use setImageURI() to set an internet resource in your ImageSwitcher. Acording to this post, this method is only for content URIs particular to the Android platform (Note that they are talking about an ImageView in that post, but I think the same goes to this case).

Hiding multiple Views before saving - Android

I am overlapping couple of view's dynamically in my app over a bitmap. I would like to delete those view's before saving the bitmap to gallery. Below in the function which adds view's on my bitmap
public void add()
{
relLayout.addView(newRect);
relLayout.addView(newSpeech);
relLayout.addView(editImgv);
relLayout.addView(resizeImgv);
}
There is a button on pressing it the above add() function gets called and all those views get added over my bitmap again.
Before saving the bitmap I would like to delete all editImgv and resizeImgv which were added
over my bitmap.
Any ideas on how to do it?
Thanks in advance :)
I have resolved it by using Vector arrays - I placed all the 'resizeImgv' and 'editImgv' image views which I am adding dynamically in a vector array of type 'ImageView' and when I am about to save I am just setting their visibility to 'GONE' one by one. It was a simple solution in the end :)
//Global
Vector<ImageView> imgv = new Vector<ImageView>();
....
//Adding views to my vector array
public void setImageViewArray(ImageView imgview){
imgv.add(imgview);
}
.....
//when I am about to save
for(int i = 0; i < imgv.size(); i++ ){
if(imgv.get(i).getVisibility() == View.VISIBLE){
imgv.get(i).setVisibility(View.GONE);
}
}

ANDROID::GET ID(R.drawable.image) value of the image in a imageview

I would like to store image ID's of the images in ImageView into database....then use these IDs as to change the images in ImageViews dynamically...
Pretty much simpler than you think it is.
The trick is to use Tag, so after you set image to the ImageView, set a tag with value of the Drawable id, afterward you can retrieve the id by getting tag.
public void setImageTag(ImageView imageView, int resId) {
imageView.setImage.... <--- set your image
imageView.setTag(int);
}
public int getImageTag(ImageView imageView) {
return (int)imageView.getTag();
}

Android - Gallery of ImageViews with background images?

I am new to Android development, but familiar with the concept of views, controls, objects, XML layouts, C#, etc.
I'm trying to create a horizontally scrolling "list" of images using as much native functionality as possible. (I'm not adverse to using custom components, but I'm trying to learn and optimize as much as possible before hacking something together.)
I currently have a Gallery with an adapter tied to it. The adapter is creating ImageViews as seen in many basic tutorials. On each pass of the adapter, I'm setting the background image for the ImageView. My hope was that I'd be able to position the foreground image to lay on top of the background image at a specific X/Y position. Unfortunately, I haven't made it past the point of getting the background image to behave the way I'd like it to.
Is this even possible with a simple Gallery and an ImageView? Or, do I need to build a custom control of some sort (possibly using nested layouts?) and use that control on each iteration of the adapter?
Any help will be greatly appreciated.
Here's what I'm seeing...
http://philaphan.com/public/stackoverflow/gallery1.png
...and what I'd like to see...
http://philaphan.com/public/stackoverflow/gallery2.png
Here's my code:
public class MyAdapter extends BaseAdapter
{
private Context mContext;
public MyAdapter(Context c)
{
mContext = c;
}
public int getCount()
{
return App.myList.size();
}
public Object getItem(int position)
{
return position;
}
public long getItemId(int position)
{
return position;
}
public View getView(int position, View convertView, ViewGroup parent)
{
String imagePath = App.myList.get(position).thumbnail;
ImageView i = new ImageView(mContext);
i.setLayoutParams(new Gallery.LayoutParams(150, 150));
i.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
i.setBackgroundResource(R.drawable.image_bk5);
//i.setBackgroundColor(Color.BLACK);
File f = new File(imagePath);
if (!f.exists())
{
i.setImageResource(R.drawable.image_missing);
}
else
{
Bitmap bmp = BitmapFactory.decodeFile(imagePath);
i.setImageBitmap(bmp);
}
return i;
}
}
I think you can use:
i.setPadding(50, 50, 50, 50); //setpadding(left,top,right,bottom)
Try to use this..
replace i.setScaleType(ImageView.ScaleType.CENTER_INSIDE); with i.setScaleType(ImageView.ScaleType.FIT_XY);
or check your background image . I think your background image width creating problem.
I was able to work around this by using a HorizontalScrollView with a LinearLayout nested within it. I then programmatically add two ImageViews -- one for the background and one for the foreground -- and I position the foreground at whatever X/Y that I choose.
I'd still be interested in knowing if this can be done within a Gallery control, if anyone sees this at a later date.
As a general rule, if you want to add a background image to any kind of inflated gallery or ArrayAdapter or BaseAdapter, then - Make sure that the background image is defined in the parent layout, or parent view, for example the main layout, using the attribute "android:background="#drawable/backupimage".
Do not set the background in the XML representing the custom-row/object-view of the repeating instances.
This way the background image will be displayed only once, appearing static behind all inflated objects, and not duplicated again and again on every single row/object.

Categories

Resources