Removing the onClick highlight on all items in a ListView - android

I'm sure this is just a matter of changing some xml layout attribute(s) but I couldn't figure out which one(s).
I have a listView containing some items.
I'm looking for a way to remove the blue highlighting background that appears on an item when it is clicked.

Try this : add this line in your listview xml code android:listSelector="#android:color/transparent"
or if you want only change the selected item's backgroung color will change and rest will remain unchanged then add some additional code in your adapter getView method
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) convertView.getContext()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
= inflater.inflate(
R.layout.your_list_item, null);
}
if(postion!=SelectedPosition)
{
convertView.setBackgroundColor(default Color); // change color as your wish or set transparent
}
else
{
convertView.setBackgroundColor(Color.argb(125,75,236,90));// change color as your wish or set transparent
}
return convertView;
}
you can add your own logic for changing color . hope it works

Related

android Color of the cell on Grid View changes on scrolling

So I have a grid of textViews and consists of 228 cells. If a user clicks on any cell on the grid the color of cell changes.But as soon as I scrolled up or down the color moves to another cells.I used customgridview Adapter . Here's the code of getView() .
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View gridView;
if(convertView == null) {
convertView = inflater.inflate(R.layout.fingerprintlayout, null);
}
TextView tv = (TextView)convertView.findViewById(R.id.textView);
tv.setText(gridV[position]); //Setting the text
if(position == getClickedCellPosition()){
convertView.setBackgroundColor(Color.BLUE); //Setting the background color
}
return convertView;
}
Does it actually move to a different cell value or a different cell in the layout? The convertView == null code is there to reuse a view if it is available. So you might set the color for cell 1,1 but when you scroll that view may be reused for 2,1.
One solution to VERIFY this is the problem is to remove the null check and just inflate the layout again. DO NOT RELEASE YOUR CODE LIKE THIS. It is inefficient.
What you can do is keep a cache of which cells are highlighted or not. When getView is called you should reset the background to the default if the cell it represents is not selected or you should highlight it if it is selected.

Colouring a listview custom item element basing on value

I have a simple (?) problem that I can't seem able to fix.
I have to populate a listview with two columns - two string arrays.
I managed to do so, after searching a lot (see here).
Now, what I need to do is to colour the background of the second item red, purple or blue depending on its value.
Is it possible?
I know that many things can be done with custom listviews, even assigning different images based on a particular value.
Thank you in advance.
In your listadapter (for example see http://www.vogella.com/articles/AndroidListView/article.html#adapterown_example) override getView() and when setting the value of the second textview set its background color. e.g.:
#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);
TextView textView = (TextView) rowView.findViewById(R.id.label);
textView.setText(values[position]);
String s = values[position];
if (s.startsWith("red")) {
//BACKGROUND COLOR CHANGE
textView.setBackgroundColor(getResources().getColor(R.color.your_red));**
}
return rowView;
}

Android: ListView does not redraw when I scroll the ListView using scrollTo(x,y)

My ListView is in the background (using a relative layout).
I have a textbox which is used as a view in ListView, which is set to different background color for different lines. This is done using the following code in adapter.
public View getView(int position, View convertView, ViewGroup parent)
{
View rowView = convertView;
if (rowView == null)
{
LayoutInflater inflater = ((Activity)context).getLayoutInflater();
rowView = inflater.inflate(R.layout.listview_background, null);
rowView.setTag(new ViewHolder((TextView) rowView.findViewById(R.id.listBackgroundTV)));
}
((ViewHolder)rowView.getTag()).tv.setHeight((position != 0)?((CompareTextBox)((Activity)context).findViewById(R.id.ctb1)).getLineHeight():((CompareTextBox)((Activity)context).findViewById(R.id.ctb1)).getLineHeight() + (int) (context.getResources().getDisplayMetrics().density + 0.5f));
((ViewHolder)rowView.getTag()).tv.setBackgroundColor((b[position])?0xff000000:0xffffffff);
return rowView;
}
So you see the textbox background color is set using the position.
Now I use ListView.scrollTo(x,y) function to scroll the ListView upwards.
Now, as the background of the textbox is calculated from the position, when the ListView is scrolled upwards, the correct color of the textbox should be displayed for the new rows displayed. But this is not happening. All the items in ListView exposed due to scrolling upwards have same black background.
Any idea, how the ListView view can be refreshed/redrawn, or any issue spotted in the above code?
You can refresh the ListView with a call to the notifyDataSetChanged() method of your adapter. You could try to call it after scrollTo, even though that should usually be called if the underlying dataset changed to refresh the view items.

Android + ListView background sets background while scrolling?

I have a ListView that's populated via an ArrayAdapter. Within the adapter, i set the view background color dependent on a conditional. It works, but while scrolling the remaining rows adopt this color. Here's some code:
class DateAdapter extends ArrayAdapter<DateVO> {
private ArrayList<DateVO> items;
public ViewGroup listViewItem;
//constructor
public DateAdapter(Context context, int textViewResourceId, ArrayList<DateVO> items) {
super(context, textViewResourceId, items);
this.items = items;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
try {
if (view == null) {
LayoutInflater vi = (LayoutInflater)getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = vi.inflate(R.layout.row, null);
}
final DateVO dateItem = items.get(position);
if (dateItem != null) {
//is this my issue here? does position change while scrolling?
if(items.get(position).getField().equals("home")){
view.setBackgroundResource(R.drawable.list_bg_home);
}
...
}
}catch (Exception e) {
Log.i(ArrayAdapter.class.toString(), e.getMessage());
}
return view;
}
}
That is the default behavior of a ListView. It can be overridden by setting the cacheColorHint to transparent.
Just add,
android:cacheColorHint="#00000000"
in your xml file.
For more details you can go through the ListView Backgrounds article.
Here is an excerpt:
To fix this issue, all you have to do is either disable the cache color hint optimization, if you use a non-solid color background, or set the hint to the appropriate solid color value. You can do this from code (see setCacheColorHint(int)) or preferably from XML, by using the android:cacheColorHint attribute. To disable the optimization, simply use the transparent color #00000000. The following screenshot shows a list with android:cacheColorHint="#00000000" set in the XML layout file
EDIT : The view passed as convertView is essentially a view which is a part of the list view, but isn't visible anymore (due to scrolling). So it is actually a view you had created, and might be a view for which you had set the custom background. To solve this just make sure that you reset the background if the condition is not satisfied. Something like this :
if(condition_satisfied) {
//set custom background for view
}
else {
//set default background for view
convertView.setBackgroundResource(android.R.drawable.list_selector_background);
}
Essentially if your condition is not satisfied you will have to undo whatever customisations you are doing when your condition is satisfied, because you might have received an old customised view as convertView.
That should solve your problem.

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

Categories

Resources