On an Android app targeting APIs 11 to 18, I have dynamically populated a ListView using an array of strings and added a listener to each row. The text usually covers approximately 50% of a row only. The problem is that if I click on the actual text, the onItemClick action is fired, but if I click on the blank portion of the row it is not.
My question is: how can I add the listener to the entire area/view of each row?
public class MainActivity extends ActionBarActivity {
protected void onCreate(Bundle savedInstanceState) {
...
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.activity_list_item, my_array);
ListView listview = (ListView) findViewById(R.id.listView1);
listview.setAdapter(adapter);
// add listener
listview.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
Log.d(TAG, "position=" + position);
}
});
activity_main.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:android1="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<ListView
android:id="@+id/listView1"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight=".80"
>
</ListView>
activity_list_item.xml:
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/my_textview"
android:textSize="35sp"
/>
Set the layout_width
of the TextView
in activity_list_item to match_parent
.
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