Get different cells of GridView - android

I'm trying to change the background color of my GridView. This works perfectly with this code:
GridView gv = (GridView) findViewById(R.id.gvSpeelveld);
gv.setBackgroundColor(Color.RED);
But now I want to change the color of different cells. For example: Row 2 cell 2 should be blue.
What method should I use to get the GridView item on a specific position to change the color?
I tried with these methods, but it didn't work out well
//Attempt 1
gv.getChildAt(1).setBackgroundColor(Color.BLUE);
//Attempt 2 (returns data, not the whole object)
gv.getItemAtPosition(5);
So how can I the content of different cells?

It is not a good practice to set the background of a particular list/grid item after execution of the adapter. The best way is to set it in the adapter itself by identifying position.e.g.
class YourAdapter extends BaseAdapter<T>{ ....
getView(int position, View convertView, ViewGroup parent){
View v = convertView;
......
if(position == your_postion){
v.setbackground(Color.parseColor("#FF0000");
}
}
}

Related

List items overlaps each other

i like to implement a layout for a listview like this:
Any idea how i can implement this?
Thanks
You can do it in a couple of ways, but I think the most convenient way is to create two 9-patch background images so that you can define which sides should scale in what way. (Check here for information about 9-patch)
Make sure that the background image has a transparent part, where you want the one list item to overlap with the other, and set the layout_marginTop of the second item negative on an amount that gives you the desired effect.
This Approach must Help,Using two Different View and Displaying One at one Time based on Position
To implement listView elements as the screenshot, you need to have a CustomListViewAdapter (extends ArrayAdapter ) and in your CustomListViewAdapter class you need to specify in the method getView() the background to use in each element as below:
#Override
public View getView(int position, View convertView, ViewGroup parent)
{
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(R.layout.rowlayout, parent, false);
if ((position % 2) == 0) {
rowView.setBackgroundResource(R.drawable.trapeze_down);
} else {
rowView.setBackgroundResource(R.drawable.trapeze_up);
}
rowView.refreshDrawableState();
return rowView;
}
Note:
* rowLayout represent the layout of an element in the listView
* trapeze_down and trapeze_up are two png images representing the trapeze form you want to have in background
* when you create your list view, don't forget to specify :
android:divider="#null"
android:dividerHeight="0dp"
*And finally you just set the adapter to your listView

How can I change the Background Resource from the first ListView visible Item?

I'm Noob at Java Android.
I searched more about my question and I found that I can use
listView.getFirstVisiblePosition();
To get my first visible position. How can I implement a new Background Resource only to my first visible position?
I tried to do that by far.
View get = lv.getFirstVisiblePosition()
get.setBackgroundResource(R.drawable.newconversation);
Thanks.
There is a method in your adapter which controls your list items views
#Override
public View getView(int position, View convertView, ViewGroup parent) {
You can just use this condition :
if(position == 0)
//set your background
After convertView has been inflated
getFirstVisiblePosition() returns an Integer position. You want to get the view at that position.
Try
View view = list.getChildAt(list.getFirstVisiblePosition());
Then set your background on the view.

Set color of Listview items in a specific repeating pattern

I wanted to know how I may go about creating a loop for setting a specific background colour to each of my listview items in a reoccurring pattern. Say I have 10 items and 4 colours and I want the 10 items to be coloured in order of the pattern and for it to repeat, until the items are all coloured. Considering I have the following colours in an Integer Array:
int[] colours = {Color.RED, Color.BLUE, Color.GREEN, Color.MAGENTA};
Well, I would implement a custom Adapter or a ViewBinder for this.
In case of an Adapter, for example ArrayAdapter you have to override the getView method
...
public View getView(int position, View convertView, ViewGroup parent) {
View view = convertView;
if (view == null) {
view = getActivity().getLayoutInflater().inflate(R.layout.item, null);
}
view.setBackgroundColor(colors[position % colors.length]);
return view;
}
....
First off, you have to be clear about two things:
This should go in the process of getView()
getView() renders your rows, but it doesn't sequentially. Performance depends very much on your layout implementation, but you cannot guarantee your rows will be rendered in order.
That said, I think there could be a way to do this:
Prior to passing your data to your ArrayAdapter, define an extra variable in your ArrayList, you should use a class if you're not yet using it.
Assign it the number of the background you want to set it. Ideally, this should be an int with the Color.YOURCOLOR you want to set it.
Once processing within your getView() method, get that data for the row and simply call:
YourClass item = (YourClass) getItem[position];
convertView.setBackgroundColor(item.getMyColor());

How do I set a background color for each item in a list view based on content

So I've been modifying the notepad tutorial code: http://developer.android.com/resources/tutorials/notepad/notepad-ex1.html .
Basically what I want to do is to create specific layout styles (i.e. background color) for different rows within the ListView based on the content. So for example, the text for the title is "1" so the background of that row will be red. Or, if the text for the title is "2" then the background of that row (or list item) will be green.
What I'm trying to accomplish is to have a summary of the the rows in the database and color-code each row based upon what category (numerical field) the item is stored as.
They use SimpleCursorAdapter, and you need to write your own in order to change the output. In your adapter, which handles the presentation of your data, you can override the method getView(). Then populate each row with the data from the db. And depending on the data, set a different background color.
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if (v == null) {
LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = inflater.inflate(R.layout.my_layout, null);
}
Cursor.moveToPosition(position);
ImageView img = (ImageView)v.findViewById(R.id.bg);
if (Cursor.getString(Cursor.getColumnIndex("column")).equals("black")) {
img.setImageBackground(Color.Black);
}
return v;
Hwa Rang's answer is correct. You might also want to use the getItem method to get the item at a particular position on the list to determine what type of data it is. Then change the background color of that row in getView method

Background Color Recycled in ListView

I have a strange problem. I am setting the background color of the items of a listview like so:
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
...
if (status == true) {
row.setBackgroundColor(Color.argb(255,0,85,187));
}
else {
if (morestuff) {
row.setBackgroundColor(Color.argb(128,255,0,0));
}
}
...
}
This seems to work. However, when i scroll on the listview, and then back, some of the rows have acquired a color from another row without being set by this code. I suspect the listview is recycling the views as an optimization.
How can I fix this?
The getView will be called all the time when the listview is drawn. Simply taking it will be called when we do a small change example do a small scroll
If you want to set color to a specific row, Just do it by checking the position (First argument of getview).
The list view is definately recycling views as an optimization. You should look at the efficient list view example for ideas.

Categories

Resources