Change background color of selected item on a ListView
You can keep track the position of the current selected element:
OnItemClickListener listViewOnItemClick = new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapter, View arg1, int position, long id) {
mSelectedItem = position;
mAdapter.notifyDataSetChanged();
}
};
And override the getView method of your adapter:
@Override
public View getView(int position, View convertView, ViewGroup parent) {
final View view = View.inflate(context, R.layout.item_list, null);
if (position == mSelectedItem) {
// set your color
}
return view;
}
For me it did the trick.
change color of selected listview item
Apply "@drawable/list_item_selector"
to the row of that list(List item) not a List itself..
Something like, your list item (list row)..
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal"
android:background="@drawable/list_item_selector">
<TextView android:id="@+id/textForList"
android:layout_height="fill_parent"
android:layout_width="wrap_content"
android:padding="10sp" />
.
.
.
</LinearLayout>
list_item_selector.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="true">
<shape>
<solid android:color="#66FFFFFF" />
</shape>
</item>
<item>
<shape>
<solid android:color="#FF666666" />
</shape>
</item>
</selector>
In Android how do you use a selector on a ListView to change the background color that will allow the text to appear as well?
Create a sub folder in res
called color
. Use that folder to create new color selectors.
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:color="pressed_color"/>
<item android:color="default_color"/>
</selector>
From there, use the TextView:textColor
attribute and apply the selector.
android:textColor="@color/your_selector"
Edit
First, remove the line android:drawSelectorOnTop="true"
from your ListView
. This is the reason the item in your ListView
is being covered up. Next, create a custom selector for your row background that uses a transparent color when the item is touched. This way, your list selector will be shown in place of the background of the item.
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@android:color/transparent" android:state_pressed="true"/>
<item android:drawable="your_default_color"/>
</selector>
Apply this selector to using the android:background
attribute.
How can I change default background color of a selected ListView item in Android?
I figured it out. Instead of using my theme to try and apply the style, I just created my own xml layout for the list items instead of using the default and set the selector from there.
Here is what the list item xml looks like.
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/text1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceListItemSmall"
android:textColor="@drawable/list_txtcolor"
android:gravity="center_vertical"
android:paddingStart="?android:listPreferredItemPaddingStart"
android:paddingEnd="?android:attr/listPreferredItemPaddingEnd"
android:background="@drawable/activated_background"
android:minHeight="?android:attr/listPreferredItemHeightSmall">
</TextView>
As you can see, I just set the item background to my selector.
android:background="@drawable/activated_background"
And again, here is my selector.
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_activated="true" android:drawable="@drawable/list_selected"/>
<item android:state_selected="true" android:drawable="@drawable/list_selected"/>
<item android:state_pressed="true" android:drawable="@drawable/list_selected"/>
</selector>
Android ListView. How to change background color of manually selected item
I've managed to accomplish that by making several selectors for the different states
first put this in your listview
android:listSelector="@drawable/list_selector"
Then create xml files in drawable to control the diferent states
@drawable/list_selector
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/list_item_bg_normal" android:state_activated="false"/>
<item android:drawable="@drawable/list_item_bg_pressed" android:state_pressed="true"/>
<item android:drawable="@drawable/list_item_bg_pressed" android:state_activated="true"/>
</selector>
@drawable/list_item_bg_normal
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient
android:startColor="@color/list_background"
android:endColor="@color/list_background"
android:angle="90" />
</shape>
@drawable/list_item_bg_pressed
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient
android:startColor="@color/list_background_pressed"
android:endColor="@color/list_background_pressed"
android:angle="90" />
</shape>
In your ListView Selection
listView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,long arg3) {
view.setSelected(true);
...
}
}
Don't forget to add list_background_pressed and list_background to your values/color.xml or just set the color manually in each file.
And I Believe that when you use setSelection(int pos) that will automatically uset the layout you've set as selectected.
Hope it helps.
How to set selection colour of listview programmatically
yourListView.setSelector(R.color.colorname);
colors.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="colorname">#333333</color>
//other colors
</resources>
How to change ListView Selected items color from default to red without using selector
Got Answer by using the below code
In mainActivity adapter class
adapter=new ArrayAdapter<String>(getApplicationContext(), R.layout.text_view,R.id.textView1,players);
lvview.setAdapter(adapter);
main.xml look like below
<ListView
android:id="@+id/list"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="8.5"
android:cacheColorHint="#00000000"
/>
And my custom layout field is like below
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginTop="23dp"
android:text="TextView"
android:textColor="#ffffff" />
<CheckBox
android:id="@+id/checkBox1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/textView1"
android:layout_alignBottom="@+id/textView1"
android:layout_alignParentRight="true"
android:focusable="false"
android:focusableInTouchMode="false"
/>
And in MainActivity onitem click listener for ListView I called the custom layout view and the code is given below
lvview.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> l, View v, int position,
long id) {
// TODO Auto-generated method stub
SparseBooleanArray checked = lvview.getCheckedItemPositions();
checkedText=(CheckBox) v.findViewById(R.id.checkBox1);
checkedList=(TextView) v.findViewById(R.id.textView1);
if(checkedText.isChecked()==false)
{
counter_selected++;
checkedText.setChecked(true);
checkedList.setTextColor(Color.RED);
selectedCounterText.setText("" + counter_selected);
}
else
{
counter_selected--;
checkedText.setChecked(false);
checkedList.setTextColor(Color.WHITE);
selectedCounterText.setText("" + counter_selected);
}
}
});
And it solved my problem..
Related Topics
How to Delete an Sms from the Inbox in Android Programmatically
Android: How to Auto-Restart an Application After It Has Been "Force Closed"
How to Make a Specific Text on Textview Bold
Setbackground VS Setbackgrounddrawable (Android)
Saving Edittext Content in Recyclerview
How to Add Window Android.View.Viewroot$W@44Da9Bc0 -- Permission Denied for This Window Type
How to Remove Application from Recent Application List
Android: Detect When Scrollview Stops Scrolling
Android: Unable to Add Window. Permission Denied for This Window Type
Calling a Fragment Method from a Parent Activity
How to Store(Bitmap Image) and Retrieve Image from SQLite Database in Android
Handling Registration Id Changes in Google Cloud Messaging on Android
Using Both Front and Back Cameras Simultaneously Android
Check If Application Is on Its First Run
Deleting a Gallery Image After Camera Intent Photo Taken