Android – how to make a click event on a textview of child of a custom listview in android

androidandroid-layoutandroid-listviewandroid-widgettextview

Basically I'm new to Android and don't know much about it. I'm making a quiz program in which I'm using custom ListView with 5 custom TextViews, one for question and other 4 for options. My problem is that I want the TextView as clickable as well as the LisView as choice mode as single. That is if I click one text all other TextViews should become unclickable. My problem is whenever I click on a TextView in the child layout, only the outer layout, that is the item of the ListView get selected.
here is the screenshot of the my listview

https://picasaweb.google.com/108429569548433380582/Android?authkey=Gv1sRgCJ3kxJz7tLvaTg#5783846428648608706

Best Answer

You can do it in two ways:

1. Either by directly using onClickListener like this:

textView.setOnClickListener(new View.OnClickListener(
        public void onClick(View arg0) {

            // Do anything here.

        }
    });

OR

2. In XML file, in declaration of <TextView /> add one more attribute as:

android:onClick="onClickTextView"

and in yout activity, add this function:

public void onClickTextView(View view) {

         // Do anything here.
}

UPDATE:

Use following code to get click event on TextView:

// Click event for single list row
        getListView().setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                    long arg3) {
                TextView tv = (TextView) (findViewById(R.id.title));
                if (tv != null) {
                    tv.setOnClickListener(new View.OnClickListener() {

                        @Override
                        public void onClick(View v) {
                            Toast.makeText(MainActivity.this, "CLICKED",
                                    Toast.LENGTH_LONG).show();
                        }
                    });
                } else {
                    Toast.makeText(MainActivity.this, "TV not found",
                            Toast.LENGTH_LONG).show();
                }
            }
        });