Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

onItemClick fires only on text not on the entire row of ListView

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"
/>
like image 424
Javide Avatar asked Jan 13 '23 11:01

Javide


1 Answers

Set the layout_width of the TextView in activity_list_item to match_parent.

like image 148
alex Avatar answered Jan 17 '23 06:01

alex