Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Changing background of a single row of gridview in android

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

like image 491
akh Avatar asked Mar 04 '13 08:03

akh


2 Answers

you can look this example it will help to you.

like image 161
Talha Avatar answered Nov 14 '22 13:11

Talha


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.

like image 44
Rahul Patil Avatar answered Nov 14 '22 12:11

Rahul Patil