3

My gridView displays images (imageItem) from local storage with Picasso. Image's state is changed when it is selected by clicking the select icon in the top right conner, and on the selected state, the image has blue border like this: blue border

I tried something like this:

public void onClick(View v) { if(!v.isSelected()){ imageItem.setBackgroundResource(R.drawable.photoborder); }else{ imageItem.setBackgroundDrawable(null); } } }); 

And I did see the border when the image has not been loaded. When Picasso finishes loading image, the border has been covered:

border has been covered

How to set border over image in ImageView? Thank you very much for any suggestion!!

Here is the layout for each image item:

 <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <custome.SquaredImageView android:id="@+id/image_item_in_gridview" android:layout_width="fill_parent" android:layout_height="fill_parent" android:src="@drawable/ic_launcher" android:background="@drawable/attachment_gallery_images_items_background" /> <ImageView android:id="@+id/imageCheckStatus" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_alignParentTop="true" android:layout_marginRight="2dp" android:layout_marginTop="2dp" android:padding="2dp" android:src="@drawable/abc_ic_cab_done_holo_dark" /> </RelativeLayout> 
1
  • 1
    I'm not really sure since I don't use Picasso, but it seems that there are more than 1 layers used: ImageView as back layer and check mark indicator as front layer. What you are doing is setting the ImageView's background, not the front layer. Perhaps it will be clearer if you could post the XML layout used in the adapter. Commented Jul 14, 2014 at 3:36

3 Answers 3

3

Your layout has 2 views; SquaredImageView to display the image, and ImageView as selection indicator. When you set the background for SquaredImageView, it will be placed behind SquaredImageView (as it is a background).

What you need is to add a View in front of SquaredImageView, then to modify its background in the code.

Add a View in your XML layout:

<?xml version="1.0" encoding="utf-8"?> <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_gravity="right"> <custome.SquaredImageView android:id="@+id/image_item_in_gridview" android:layout_width="match_parent" android:layout_height="match_parent" android:src="@drawable/ic_launcher" android:background="@drawable/attachment_gallery_images_items_background" /> <View android:id="@+id/imageCheckBorder" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@drawable/photoborder" /> <ImageView android:id="@+id/imageCheckStatus" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginRight="2dp" android:layout_gravity="right" android:layout_marginTop="2dp" android:padding="2dp" android:src="@drawable/abc_ic_cab_done_holo_dark" /> </FrameLayout > 

Then modify the code to change the background of that View, instead of SquaredImageView. (in this example, it's named as imageCheckBorder)

public void onClick(View v) { if(!v.isSelected()){ imageCheckBorder.setBackgroundResource(R.drawable.photoborder); }else{ imageCheckBorder.setBackgroundDrawable(null); } } 
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you, I've just also figured it out
Glad you figured it out by yourself :)
0

you can use border drawable as layer for your imageview

<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" > <stroke android:width="@dimen/drawable_selector_border_thickness" android:color="#0099CC" /> <solid android:color="#500099CC" /> </shape> 

Comments

0

according to your code, you are using 2 different instances at the same time i.e.imageItem.setBackgroundResource and

imageItem.setBackgroundDrawable 

What you actually need to do is just simply stick with one of the background instances, and you would get your desired result. The code should look like this

public void onClick(View v) { if(!v.isSelected()){ imageItem.setBackgroundResource(R.drawable.photoborder); }else{ imageItem.setBackgroundResource(null); } } }); 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.