Changing background of a single row of gridview in android
Asked Answered
G

3

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)....

Gwenngwenneth answered 4/3, 2013 at 8:23 Comment(3)
What does you mean single row !! GridView provide click on one ItemAugustinaaugustine
Are you sure you don't want a list view with "3" items on each row?Quake
@Sameer - I know that gridview provide click on one item...but i want to provide a shelf image at the background so that it will look like a bookshelf...Gwenngwenneth
F
2

you can look this example it will help to you.

Fronde answered 4/3, 2013 at 11:7 Comment(0)
L
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.

Lylelyles answered 4/3, 2013 at 9:17 Comment(2)
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..Gwenngwenneth
yes , it depends on the how you have designed the UI / the row . If you paste your code with question . it'll be helpful.Lylelyles
T
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);
Terrilynterrine answered 15/11, 2014 at 11:4 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.