I want to provide a background drawable, similar to the one shown in the figure, to a layout. How can i do it using a drawable xml? Please suggest a suitable approach to go about this.
Asked
Active
Viewed 928 times
0
-
check this link http://stackoverflow.com/questions/5841128/how-to-display-border-to-imageview – Randroid Sep 22 '11 at 06:29
2 Answers
1
It is not possible to do this with single xml drawable but probably you can club two to create this effect. I would do it this way
- Create a drawable of square type with black borders
- Create a clip drawable and clip the bottom of sqaure drawable.
Reference here
http://developer.android.com/guide/topics/resources/drawable-resource.html#Clip
You can use the android:gravity="top" and then programmatically set the level to reveal 90% (or more) of the image
ImageView imageview = (ImageView) findViewById(R.id.image);
ClipDrawable drawable = (ClipDrawable) imageview.getDrawable();
drawable.setLevel(drawable.getLevel() + 9500);

Rahul Choudhary
- 3,789
- 2
- 30
- 30
-
Thanks rahul for a quick reply. I am using the approach you suggested. I tried using android:gravity=["top" | "right" | "left" ] as given in the clip drawable docs. But this syntax is giving me error in my xml file. However, when i use a single attribute android:gravity="top" it works fine, and only the top line is visible. Please help me with this? – Shafi Sep 22 '11 at 07:14
-
I have updated my answer, you would need to set the level programmatically. I wonder why this couldn't be another property in the xml file :-? – Rahul Choudhary Sep 22 '11 at 08:04
-
0
I know this is 4+ years after the fact but for anyone else wanting to achieve the same result, there's a very simple way to achieve this by creating a layer-list
xml drawable
.
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape>
<padding
android:top="5dp"
android:left="5dp"
android:right="5dp" />
<solid
android:color="@android:color/black" />
</shape>
</item>
<item>
<shape>
<solid
android:color="@android:color/white" />
</shape>
</item>
</layer-list>
Save this to your project's drawable
folder and reference it like any other drawable (android:src
/ android:background
, or programatically)

mjp66
- 4,214
- 6
- 26
- 31