Textview onClick is called instead of Listview onItemClick.
OK I have searched and seen similar problems, tried them with no success. I have a listView with some items and I want to click one item and show the detail somewhere else.
This is my listView
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_pedidos);
ListView listView = (ListView) findViewById(R.id.actualStoresList);
Model.initialize();
Vector<String> values = Model.stores;
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapter, View view, int position,
long id) {
Object o = adapter.getItemAtPosition(position);
String str_text = o.toString();
Log.i("", "I have selected this: " + str_text);
}
});
CustomStringAdapter adapter = new CustomStringAdapter(this, R.layout.my_list_layout, R.id.list_content, values);
listView.setAdapter(adapter);
}
This is my_list_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:gravity="center_vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:id="@+id/list_content"
android:textColor="#000000"
android:gravity="center"
android:layout_margin="4dip"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="20dp"/>
</LinearLayout>
The list is displayed correctly. When I click on the list item (TextView) it "steals" the click event, so nothing happens (the onItemClickListener is bound to the listView, not the TextView).
The TextView has a little headroom where if I'm careful I can click behind it, actually touching the listView. In this case, the event fires fine and I see the log.
I tried to set the TextView android: focusable = "false" but still the TextView is "above" the ListView and always gets click events.
How can I make the TextView "transparent" so that it actually clicks on the listView, or add an onclickListener to the TextView so that I can handle its events?
Thank!
Alejandro
source to share
Setting the clickable
property TextView
to false should solve this problem. Try the following:
<TextView
android:id="@+id/list_content"
android:textColor="#000000"
android:gravity="center"
android:layout_margin="4dip"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="20dp"
android:clickable="false" />
source to share