Android listview item background change - android

i have a android listview. i want to change listview item background when i click one listview item.
and then previous selected item must go back to default background. this means only one item has to be selected.
i have searched it for a long time. i can change background of selected item using onItemClick()
but i can't change previous selected item. for example, if i select second item, it was changed. and then i select third item. oh my god! it is changed too! what can i do for this. how can i get the previous position?
here is my android code.
private class ListViewItemClickListener implements
AdapterView.OnItemClickListener {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
TextView title = (TextView) view.findViewById(R.id.title);
title.setBackgroundResource(R.drawable.list_shape);
}
}

You should use the built in methods of selecting items in a listview. Manually changing the background is prone to error as you have found.
Add this attribute to the root view in your listview item xml
android:background="?android:attr/activatedBackgroundIndicator"
then call setItemChecked(x, true) on your ListView where x is the position of the item you want to be selected.
Ensure your listview has a ChoiceMode set that allows selection (such as "SingleChoice")

When I have this in a similar example I have a global field named:
selectedListItem;
This would be updated in your onitemClickListener and the previous item would then have it's background returned to the default.
So to update your code:
private class ListViewItemClickListener implements
AdapterView.OnItemClickListener {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
//First update the previously selected item if one has been set
if(selectedListItem!=null){
TextView previousTitle = (TextView) selectedListItem.findViewById(R.id.title);
previousTitle.setBackgroundResource(R.drawable.list_default_background);
}
//Then update the new one
TextView title = (TextView) view.findViewById(R.id.title);
title.setBackgroundResource(R.drawable.list_shape);
selectedListItem = view;
}
}
So simply initalise selectedListItem as a field in your adapter with the onClickListener as an inner class and have your default background drawable instead of list_default_background .
Alternatively you can track the position numbers instead of the actual view.
EDIT:
To use this method for your list you will also have to keep track of an ID or object instance for your specific list item. In my own solution, in my ListAdapter's getView method I make sure only the list item that matches the ID/instance of the correct item is updated. With your code the way it is you will also find that when you scroll down the view at the same position in this list of visible items is also updated. This is because list view's refer to the list in sets of items, where each set corresponds to the items visible on the screen at any one time.
To update a singular, specific item you would be better suited to using a selector background or indicator as mentioned in the other answers.
HTH

You can change the ListView item colour on clicking it like below. Follow these steps.
(Remember, This is for Custom List View)
Create an XML file in Drawable Folder as Below:
<item android:drawable="#color/orange" android:state_focused="true"/>
<item android:drawable="#color/orange" android:state_pressed="true"/>
<item android:drawable="#drawable/listview"></item>
Choose your own resources.
While implementing Custom ListVIew, you'll have additional layout for Custom List Item design. Below is such an Example.
<ImageView
android:id="#+id/imageView1"
android:layout_width="60dp"
android:layout_height="60dp" />
<TextView
android:id="#+id/textView1"
android:layout_width="fill_parent"
android:layout_height="60dp"
android:layout_toRightOf="#+id/imageView1"
android:background="#drawable/listselect_picture"
android:gravity="center"
android:text="TextView"
android:textColor="#drawable/select_txtcolor"
android:textSize="16sp" />
In Above code I have put the set the XML file from Step 1 as "background" attribute. This will work as you want to.
Additionally if you want to change the text colour on ListItem selection as well, use below XML code and set that XML file as "TextColor" attribute.
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_selected="true" android:color="#android:color/white"/>
<item android:state_focused="true" android:color="#android:color/white"/>
<item android:state_pressed="true" android:color="#android:color/white"/>
<item android:color="#android:color/black"/>
</selector>
The code above will change the text color to while on selection and revert to original when unclicked.

Related

Android : Change the background color of a listview/gridview

I'm trying to change the background of a selected item in a gridview, so I used the following code to do that
gridview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
// Basically changes the boolean value of an object's variable to "true", this means it has been clicked on
Themes.themes.get((int)id).clicked= true;
// Reload the list
adapter.notifyDataSetChanged();
}
});
Then in the getview() method of my adapter class I use
if(theme.clicked)
convertView.setBackgroundColor(context.getResources().getColor(android.R.color.holo_green_dark));
Once I click on a item of my gridview its background color changes, but when I scroll down the background color of other cells in the gridview also changes. I tired using different methods to apply the onclick event (like using a selector) but nothing seems to be working.
Does someone know of a way to solve this issue ?
Thanks in advance !
This issue is because of recycling of views. You need to set the background color for unselected items also.
if(theme.clicked)
convertView.setBackgroundColor(context.getResources().getColor(android.R.color.holo_green_dark));
else {
//Set the default color here for unselected items
}
let's say you have a background in the drawable folder name selector_bg.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:drawable="#color/colorPrimaryPress"/>
<item android:drawable="#color/colorPrimary" />
</selector>
i supose you have a layout for the cellule of your gridView ?
if so set android:background="#drawable/selector_bg"

onItemClick listview sets color of 2 items

Following scripts sets the color of (for example) ListItem position 1, but it also gives number 12 (11+1) a nice grey color. Is this some kind of bug in Android?
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
ListView.setSelection(arg2);
arg1.setBackgroundColor(Color.LTGRAY);
adapter.notifyDataSetChanged();
}
ListView recycles (reuses) the views. So you need to associate the background color with the data, not the view! Then, in getView() you have the chance to correctly set the background color based on the data.
#David Wasser is correct ... cell re-use is causing multiple listview rows to draw with gray background.
However, if you're trying to set your background based on SELECTION STATE, consider this technique:
// set single or multi-select on your list (CHOICE_MODE_SINGLE = single row selectable)
// do this in onCreate
listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
.
.
.
// in your onItemClick, set the checked state of the item
// you DO NOT need to call notifyDataSetChanged
listView.setItemChecked(position, true);
And, set the background on your listview cell layout to the built-in or a custom selector
BUILT-IN:
android:background="?android:attr/activatedBackgroundIndicator"
CUSTOM:
android:background="#drawable/myListBackground"
drawable/myListBackground.xml:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_activated="true" android:drawable="#color/lightgray" />
<item android:drawable="#color/transparent" />
</selector>
the key is the state_activated entry, which will be used when the item is selected/checked. You can also specify colors for other states, the above example references colors from a colors.xml table.
For more details on this, please check out How does "?android:attr/activatedBackgroundIndicator" work?

android custom spinner. selected item in blue color

I am using a custom spinner. It is almost similar to default, except that I need to set padding at left of each item ( in the drop down padding in each row before text). I am able to do that. But I also want to show the selected item in a different color when list of values are displayed to user as dropdown.
I have used text view as drop down item.
Can someone please suggest if it can be done. I have tired to achieve this with xml, but I couldn't find any option.
Thanks in advance.
EDIT :
Text of selected item in Blue color.
You could specify android:dropDownSelector="#color/spinner_selector" for the spinner element and define the spinner_selector color to #800000FF, for example.
See
http://developer.android.com/reference/android/widget/Spinner.html#attr_android:dropDownSelector
You Can Do it by following code.
Just apply style to your textview as background.
// TextStyle is xml file which contain following code.
<item android:state_selected="false"
android:drawable="#android:color/white" />
<item android:state_selected="true"
android:drawable="android:drawable="#android:color/blue"" />
You have to change color of textview at runtime by using spinner's method setOnItemSelectedListener.
For Example,
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
TextView tv = (TextView) view.findViewById(R.id.tv );
tv.setTextColor(R.color.green);
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});

Android GridView Background color on Selection

I have created a GridView that displays various Bitmaps . I want that when a user selects selects a cell its back ground color changes. This is what i am doing. My ImageAdapter extends BaseAdapter has the following lines of code in it the getView function
if(videoObjects.get(position).isSelected)
{
gridView.setBackgroundColor(Color.BLUE);
}
else
{
gridView.setBackgroundColor(Color.TRANSPARENT);
}
My GridView has a OnItemClickListener that has the following code in onItemClick.
userVideos.get(postion).isSelected=!userVideos.get(postion).isSelected;
videoBitmapsAdapter.notifyDataSetChanged();
This should toggle the boolean variable and then i redraw the adapter. But the selection does not work.
Kind Regards.
It might be better to use a selector as background with 2 states:
<selector>
<item state_selected="true" drawable="yourbg" />
<item state_selected="false" drawable="otherbg" />
</selector>
Then in your
onItemClick(View v, ViewGroup parent, int position){
if (this.previousSelectedView != null) this.previousSelectedView.setSelected(false);
v.setSelected(true);
}
That way, there is no need to invalidate the whole gridview. You'll have to keep a reference to the currently selected griditem, so you can deselect it when a different item is clicked.
(This code is not tested)

onItemClick in ListView randomly changes the background color

I am using multiple choice ListView items. The user can select multiple items. So I wanted to highlight the items selected by the user by changing either the text color or background color of the list row. I have gone through stack overflows links but didn't get the proper solution. When I am trying to do it, it is randomly changing any unselected item background color. Help!!!
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
videocursor.moveToPosition(position);
v.setBackgroundColor(Color.parseColor("#80B7DBE8"));
SaveSelections();
}
That randomly changing background color is due to the reason that ListView recycles views in its list to avoid unnecessary memory consumptions. You need to provide your own custom adapter in which you need to override getView method and perform some checks to highlight only those list items who have some kind of flag set to true for background color
Far better than the above suggestions is to use a selector, also known as a state-list drawable. That way, the OS takes care of all of the business of color highlighting, etc.
more explicitly, take the following xml and save it under res/drawable as something like li_background.xml (I'm using images, but feel free to swap out colors as is appropriate to your situation)
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_focused="true"
android:drawable="#drawable/list_item_pressed" />
<item android:state_pressed="true"
android:drawable="#drawable/list_item_pressed" />
<item android:state_selected="true"
android:state_activated="true"
android:drawable="#drawable/list_item_selected" />
<item android:state_activated="true"
android:drawable="#drawable/list_item_selected" />
<item android:state_selected="true"
android:drawable="#android:color/black" />
<item android:drawable="#android:color/transparent" />
</selector>
and set the background of All ListItems in the ListView to R.drawable.li_background. if your listView is properly configured for multiple selections (android:choiceMode="multipleChoice" or listView.setChoiceMode(AbsListView.CHOICE_MODE_MULTIPLE); if you prefer) then android will take care of the rest. This stuff isn't well documented, I had a hard time finding the solution and I see similar questions all the time, but the relevant dos are here and here
Wagas is correct. In your adapter's getView, you are passed a View (called convertView by Eclipse's autocomplete). This is because ListViews recycle the views for each row. If you set a property for a given View, it will retain that property when it is recycled.
For this reason, you should never set the properties of anything in a given row's View outside the getView method of the Adapter backing the ListView.
There are a number of ways you could handle this, but the basic idea is that you want to set some piece of data that uses the position passed in to onItemClick to set a flag. Then check the flag in the Adapter's getView to decide how to set the background for the row's View.
For example, if only one item can be selected, you might just set a member variable, say mPosition on the Adapter itself to the position passed in to onItemClick, and then check in getView if position == mSelectedPosition to decide how to set the View. If multiple items can be selected, perhaps you set a member variable on each object contained in the Adapter.
You probably should override getView() in the adapter, and change the row background color in there.
An example:
#Override
public View getView(int position, View convertView, ViewGroup parent) {
if(convertView != null) {
if (mSelectedPositions.contains(position)) {
convertView.setBackgroundColor(Color.parseColor("#3fa9f5"));
} else {
convertView.setBackgroundColor(Color.parseColor("#ffffff"));
}
}
return super.getView(position, convertView, parent);
}
in onItemClick() just use adapter.notifyDataChange(); also set a flag to true;
in the getView() of your CustomAdapter use boolean flag to know whether the row was selected or not.
Then just check that flag and proceed as you want
i.e.,
if(flag){
v.setBackgroundColor(Color.parseColor("#80B7DBE8"));
}else{
v.setBackgroundColor(Color.parseColor("#FF0000"));
}

Categories

Resources