Android: disabling highlight on listView click
Add this to your xml:
android:listSelector="@android:color/transparent"
And for the problem this may work (I'm not sure and I don't know if there are better solutions):
You could apply a ColorStateList to your TextView.
Android - Disable ListView Selection Highlight but Keep OnClick enabled
Just create a drawable that has a transparent color in it, something like this:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_window_focused="false" android:drawable="@android:color/transparent"/>
<!-- Even though these two point to the same resource, have two states so the drawable will invalidate itself when coming out of pressed state. -->
<item android:state_focused="true" android:state_enabled="false" android:state_pressed="true" android:drawable="@drawable/list_selector_disabled_holo_light" />
<item android:state_focused="true" android:state_enabled="false" android:drawable="@drawable/list_selector_disabled_holo_light" />
<item android:state_focused="true" android:state_pressed="true" android:drawable="@color/transparent" />
<item android:state_focused="false" android:state_pressed="true" android:drawable="@color/transparent" />
<item android:state_focused="true" android:drawable="@drawable/list_focused_holo" />
</selector>
And then set by code or by XML:
listView.setSelector(R.drawable.my_transparent_selector);
The javadoc for this method says:
Set a Drawable that should be used to highlight the currently selected item.
and the XML attribute is:
android:listSelector
You can play with all the states, remember that you also have the focus state.
Removing the onClick highlight on all items in a ListView
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
How to cancel Android ListView item from being activated/highlighted in ListView's setOnItemClickListener?
I actually encountered the same issue a few days ago. The solution is not to set view.setItemChecked(false), but instead in the .onItemClick()
-method, call your listview and set listview.setItemChecked(position, false)
. This solved it for me.
Example:
listView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view,
int position, long arg3) {
// load data if no other call to web service is in progress
if (_isNotLoadingData) {
loadList2AndList3DataUsingWebService();
} else {
ListView lv = (ListView) adapterView;
lv.setItemChecked(position, false);
}
}
});
Android - Keep ListView's item highlighted once one has been clicked
Put a position variable for selected item. Change the position in onItemClicked()
method. Check the selected position in List Adapter inside getView()
and set the background for the selected item.
public class TestAdapter extends BaseAdapter
{
private Context context;
private ArrayList<TestList> testList;
private int selectedIndex;
private int selectedColor = Color.parseColor("#1b1b1b");
public TestAdapter(Context ctx, ArrayList<TestList> testList)
{
this.context = ctx;
this.testList = testList;
selectedIndex = -1;
}
public void setSelectedIndex(int ind)
{
selectedIndex = ind;
notifyDataSetChanged();
}
@Override
public int getCount()
{
return testList.size();
}
@Override
public Object getItem(int position)
{
return testList.get(position);
}
@Override
public long getItemId(int position)
{
return position;
}
private class ViewHolder
{
TextView tv;
}
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
View vi = convertView;
ViewHolder holder;
if(convertView == null)
{
vi = LayoutInflater.from(context).inflate(R.layout.test_list_item, null);
holder = new ViewHolder();
holder.tv = (TextView) vi;
vi.setTag(holder);
}
else
{
holder = (ViewHolder) vi.getTag();
}
if(selectedIndex!= -1 && position == selectedIndex)
{
holder.tv.setBackgroundColor(Color.BLACK);
}
else
{
holder.tv.setBackgroundColor(selectedColor);
}
holder.tv.setText("" + (position + 1) + " " + testList.get(position).getTestText());
return vi;
}
}
Now set the selectedIndex variable when a list item clicked.
public class TestActivity extends Activity implements OnItemClickListener
{
// Implemented onItemClickListener
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id)
{
adapter.setSelectedIndex(position);
}
}
Disable highlight in ExpandableListView
Try adding
android:listSelector="@android:color/transparent"
in ExpandableListView xml
Or override the ExpandableListView style.
Related Topics
Android Boot_Completed Not Received When Application Is Closed
How to Set Relativelayout Layout Params in Code Not in Xml
Use Uri Builder in Android or Create Url with Variables
Android Saving Created Bitmap to Directory on Sd Card
How to Make an Application Ignore Screen Orientation Change
Fusedlocationapi.Getlastlocation Always Null
Intent Action for Network Events in Android Sdk
Was Preferencefragment Intentionally Excluded from the Compatibility Package
Call Method When Home Button Pressed
Removing Log Call Using Proguard
Comparing Two Drawables in Android
Android - Nullpointerexception on Searchview in Action Bar
Some Androids Apps Won't Connect Through Fiddler
Understanding Command Through Adb Shell and Through Code - Android
Listing All Extras of an Intent
Vectordrawable - Is It Available Somehow for Pre-Lollipop Versions of Android
Install_Failed_Update_Incompatible When I Try to Install Compiled .Apk on Device