Border for gridview elements in android

I displayed the image and text as a grid. Each element consists of one image and one text. It is working fine. But I would like to know how to set the border of each gridview element separately in android.

+3
source share
2 answers

1) create the attrs.xml file in the res> value folder.

2) Add a resource:

<declare-styleable name="Gallery1">
        <attr name="android:galleryItemBackground" />
 </declare-styleable>

3) Add the following code to your activity:

TypedArray a = obtainStyledAttributes(R.styleable.Gallery1);
int mGalleryItemBackground = a.getResourceId(
                    R.styleable.Gallery1_android_galleryItemBackground, 0);
a.recycle();

4), then set the mGalleryItemBackgroundbackground of your view. You will get a border outside your view. For instance:

imageView.setBackgroundResource(mGalleryItemBackground);
+7
source

You can also create your own background by creating your own form file ...

    <shape xmlns:android="http://schemas.android.com/apk/res/android"
      android:shape="rectangle" >
    <solid android:color="#ffffff" />
    <stroke android:width="1dip" android:color="#4fa5d5"/>
    </shape>

and then just add the background to the resource ...

    imageView.setBackgroundResource(R.drawable.shape);
0
source

All Articles