Question

I am developing a small app for android. I use a ListView with a TextView and a CheckBox.
I can already handle the CkeckBox tick. Furthermore I want to make the whole row of the ListView clickable, but I cannot find any ways to realise this. I already tried to make the ListView clickable with:

android:clickable="true"

And I also tried it with this code for the ListView:

listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Toast.makeText(null,"Clicked",Toast.LENGTH_LONG
    }
});

Here is my code for the ListView:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:context="de.matthias.lolchampionowner.app.ChampionActivity">

    <ListView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/championListView"
        android:layout_alignParentTop="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_alignParentBottom="true" />

</RelativeLayout>

Like whatsapp did it. With Android Studio I could find out that this is also a ListView but the code it too obfuscated to get anything out of it.
Here is a picture: WhatsApp ListView

I have no idea how to realise it. I appeciate any help.


Here is the answer from nKn:

U have to Override the getView method. Here is my code and I can handle both clicks from the ListView and the CheckBox diveded:

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    if(convertView == null) {
        convertView = inflater.inflate(R.layout.row_name_check, null);
        holder = new ViewHolder();
        holder.textView = (TextView) convertView.findViewById(R.id.textViewRow);
        holder.checkBox = (CheckBox) convertView.findViewById(R.id.checkBoxRow);
        convertView.setTag(holder);
    }
    holder = (ViewHolder) convertView.getTag();
    final ChampionActivity.EachRow row = this.getItem(position);
    row.checked = ItemStorage.getChampSkinOwned(row.text);
    holder.textView.setText(row.text);
    holder.checkBox.setChecked(row.checked);
    holder.checkBox.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if(((CheckBox)v).isChecked()) {
                ItemStorage.setChampSkinOwned(row.text, true);
            } else {
                ItemStorage.setChampSkinOwned(row.text, false);
            }
        }
    });
    convertView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Log.d("onClick", row.text + " clicked");
            //Toast.makeText(null,"Clicked",Toast.LENGTH_LONG);
        }
    });
    return convertView;
}
Was it helpful?

Solution 2

Probably the best idea is extending an ArrayAdapter and override the getView() method. The second parameter of this method is a View (usually called convertView, but not necessarily), vaguely speaking, is the representation of a row, so this method will be called for each row. Simply declare your onClickListener() over it.

@Override
public View getView(final int position, View convertView, final ViewGroup parent) {
  ...

  convertView.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(final View v) {
      ...
    }
  });

  ...
}

OTHER TIPS

I had the same issue as well, I had to change this to get the desired result. In code for ListView (not the list items), change this as

android:layout_width="match_parent"

Hope this will work for you guys as well

Change ListView layout_width to "fill_parent" like below

<ListView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        ... />

only use

android:layout_width="match_parent"
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top