I want to make a listview unselectable and unclickable. I'm talking about the color change that occurs when I click on a list item. The code is given below. I'm a newbie in android so please be kind.
from : listitem.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="8px">
<TextView
android:id="@+id/label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#FFFFFF"/>
<TextView
android:id="@+id/data"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="16px"/>
</LinearLayout>
from : details.java
TestActionAdapter() {
super(TestDetails.this, R.layout.action_list_item, actions);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
TestAction action = actions.get(position);
LayoutInflater inflater = getLayoutInflater();
View view = inflater.inflate(R.layout.action_list_item, parent, false);
TextView label = (TextView) view.findViewById(R.id.label);
label.setText(action.getLabel());
TextView data = (TextView) view.findViewById(R.id.data);
data.setText(action.getData());
return view;
}
setEnabled(false) in getView() for such items).
There's an even easier way: set ListBox property IsHitTestVisible="False" . This prevents all the items in the list from receiving mouse events. This has the advantage of stopping the highlighting as you mouse-over as well.
If you only want to set width and height of listview. you can set it by right clicking on listview in xml and select set width / set height and set it, for ex. 20dp, 50 dp etc..
ListView supports to disable the selection. You can disable it by setting the SelectionMode property to None.
Check out the answer here:
How to disable list items...
Basically extend ArrayAdapter
, and add these 2 functions:
@Override
public boolean areAllItemsEnabled() {
return false;
}
@Override
public boolean isEnabled(int position) {
return false;
}
If all you want is to prevent all rows highlighting on click just use ListView android:listSelector="@android:color/transparent"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With