7

I'm currently developing an android application which needs to show a book shelf where it contains maximum of 3 images in each row. I'm using gridview for that. But i'm stuck in one point where i'm not able to change the background of a whole row in gridview. can anyone tell me how to do this?

The adapter used for gridview

private class SampleGridAdapter extends BaseAdapter{

    private Context context;
    private int[] images = {"R.drawable.img1","R.drawable.img2","R.drawable.img3","R.drawable.img4","R.drawable.img5","R.drawable.img6","R.drawable.img7"};
    public SampleGridAdapter(Context context) {
        super();
        this.context = context;

    }
    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return images.length;
    }
    @Override
    public Object getItem(int arg0) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public long getItemId(int arg0) {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public View getView(int arg0, View arg1, ViewGroup arg2) {
        // TODO Auto-generated method stub

        View v = null;
        ImageView coverImageView;

        LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

            v = inflater.inflate(R.layout.bookgrid, null);
            coverImageView = (ImageView) v.findViewById(R.id.coverImageView);   
            coverImageView.setImageResource(images[position]);


            coverImageView.setScaleType(ImageView.ScaleType.FIT_XY);

        return v;
    }
}

bookgrid.xml

<?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:background="@drawable/shelfimage" >

<ImageView
    android:id="@+id/coverImageView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:paddingBottom="0dp"
    android:paddingLeft="25dp"
    android:paddingTop="6dp"
    android:src="@drawable/ic_launcher" />
 </RelativeLayout>

I have set the number of columns as 3 in my gridview declaration. Here i'm setting the background in xml file as android:background="@drawable/shelfimage". but it is setting that background for each item seperately...... I want to set a single image as background for each row (having 3 items)....

akh
  • 2,478
  • 3
  • 23
  • 23

3 Answers3

2

you can look this example it will help to you.

Talha
  • 12,673
  • 5
  • 49
  • 68
1

The answer is for what I have understood for you question .

IF in the case of , you are using some adapter for gridview , in the adapter class and getView method check the position and set the color to the layout which you are inflating . In adapter class getview method the bg of inflating view can be changed for specific positions .

If you have implemented the view with using adapter the logic works.

Rahul Patil
  • 2,707
  • 2
  • 21
  • 32
  • 1
    Thanks for your response....with your logic i can change background of the individual grid item....but i need to know whether there is any way to put a background for one complete row in gridview.. – akh Mar 04 '13 at 09:23
  • yes , it depends on the how you have designed the UI / the row . If you paste your code with question . it'll be helpful. – Rahul Patil Mar 04 '13 at 10:06
0

I think the only way to achieve this is by setting background image for gridview that repeats in vertical direction (or maybe either), a sample code:

Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.bg_image);
BitmapDrawable bitmapDrawable = new BitmapDrawable(bmp);
bitmapDrawable.setTileModeXY(TileMode.REPEAT, TileMode.REPEAT);
gridView.setBackgroundDrawable(bitmapDrawable);
duckduckgo
  • 1,280
  • 1
  • 18
  • 32