How to apply different colors for gridview row items in android? -


i have grid view 2 columns. want set different colors grid view row items. example, want 4 colors applied after every 4th row item, alternately.

android:background="@color/dividercolor"/> <linearlayout     android:layout_width="match_parent"     android:layout_height="0dp"     android:layout_weight="1.6"     android:padding="8dp">     <gridview         android:id="@+id/gridcategory"         android:layout_width="match_parent"         android:layout_height="match_parent"         android:numcolumns="2"         android:verticalspacing="10dp"         android:horizontalspacing="10dp"         android:stretchmode="columnwidth"         android:gravity="center">     </gridview> </linearlayout> 

this may helps ,have try.

class mylistadapter extends baseadapter{      @override     public int getcount() {         return mlist.size();     }      @override     public news getitem(int position) {         return mlist.get(position);     }      @override     public long getitemid(int position) {         return position;     }      @override     public view getview(int position, view convertview, viewgroup parent) {         if(convertview==null){             convertview=view.inflate(mactivity, r.layout.gridview, null);             textview tvcontent = (textview) convertview                     .findviewbyid(r.id.iv_pic);         }         textview info = getitem(position);         info.settext(info.title);         //is ensure 2 columns         int type=position/2;         //for each row processing respectively         switch(type){             case 0:                 convertview.setbackgroundcolor(color.parsecolor("#ff0000"));                  break;             case 1:                 convertview.setbackgroundcolor(color.parsecolor("#00ff00"));                 break;             case 2:                 convertview.setbackgroundcolor(color.parsecolor("#0000ff"));                 break;             case 3:                 convertview.setbackgroundcolor(color.parsecolor("#000000"));                 break;         }         return convertview;     }  } 

Comments