4

How to set alternate row color in a gridview? I have searched a lot of tutorial about how to set grid view row colors,but nothing about gridview row color. I got list view with alternate row color only. I need alternate row should be white and black. Here I Include my codes. Please help me!!!!!!!!!!

Here is Java Class:

public class MainActivity extends Activity { GridView gridView; static final String[] numbers = new String[] { "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z" }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); gridView = (GridView) findViewById(R.id.gridView1); ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, numbers); gridView.setAdapter(adapter); } } 

Here is xml:

<?xml version="1.0" encoding="utf-8"?> <GridView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/gridView1" android:numColumns="10" android:gravity="center" android:columnWidth="50dp" android:stretchMode="columnWidth" android:layout_width="fill_parent" android:layout_height="fill_parent"> </GridView> 

Please provide good tutorial or sample Example. Thanks in Advance!!!!!!!!!!!

2 Answers 2

7

Create a custom adapter and override its getView method:

@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); gridView = (GridView) findViewById(R.id.gridview1); MyAdapter adapter = new MyAdapter(this, R.layout.item, numbers); gridView.setAdapter(adapter); } public class MyAdapter extends ArrayAdapter<String> { String[] objects; Context context; public MyAdapter(Context context, int textViewResourceId, String[] objects) { super(context, textViewResourceId, objects); this.context = context; this.objects = objects; } @Override public View getView(int position, android.view.View convertView, android.view.ViewGroup parent) { TextView tv; if (convertView == null) { LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); tv = (TextView)inflater.inflate(R.layout.item,parent,false); } else { tv = (TextView) convertView; } tv.setText(objects[position]); if (position % 2 == 0) tv.setBackgroundColor(Color.BLACK); else tv.setBackgroundColor(Color.WHITE); return tv; } } 

item.xml

<?xml version="1.0" encoding="utf-8"?> <TextView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> </TextView> 
Sign up to request clarification or add additional context in comments.

3 Comments

Mohsen Afshin@ It works fine Sir. Thanks for your Answer. One more thing is how to change the alternate Column color?
It's the same as rows, but you should manually set the Row and Column of the cells (inside getView) and then based on the column number change the back color. It's on you to have a logic to determine row and column number of each cell (character, here)
Mohsen Afshin@ Thank you sir.
0

Use custom Adaptor and in Custom adaptor
you will be having method getView

Use this code in your getView Method

if(position % ( 2*columnCount ) == 0) { // set first color } else { // set alternate row's color } 

3 Comments

It would be more like position % columnCount.
@kcoppock ya but not exactly like that it should be like this
Hi-Tech KitKat Android@ Thanks for your Answer Sir.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.