user2511393
user2511393

Reputation: 3

Android: Is there any way to find out which view was being click

I have a ListViewwith BaseAdapter which its items are LinearLayout. Inside LinearLayout, there're a TextView and a RadioGroup.

I wrote a OnItemClickListener for listview. However, when I clicked the item inside ListView. Nothing response!

I want to know how could I find out which view was being Clicked. Is there any method like Log.setShowClickingView(true)?

Upvotes: 0

Views: 130

Answers (2)

MDMalik
MDMalik

Reputation: 4001

First you need to have an event in your Xml that is something like this

 android:onClick="myCheckMethod"

Remember you need to declare /write this statement for your button and radio button.

Now In your Java file (Class) declare the method and match it with the view and you will know which view was clicked

public void myCheckMethod(View v) {
    // does something very interesting

    String Caller = "";
    if (R.id.DI == v.getId()) {
        Caller = "DI";
    } else if (R.id.btnExp == v.getId()) {
        Caller = "Exp";
    } 
    Log.i(Tag,"View Clicked was:"+Caller);
}

Edit User requested to know how to know the ID

getResources().getResourceEntryName(int resid);

or

getResources().getResourceName(int resid);

You'll prefer the first one I guess.

Upvotes: 2

Steve Bergamini
Steve Bergamini

Reputation: 14600

When using OnItemClickListener, one of the things returned is the position within the listView of the item that was clicked:

listView1.setOnItemClickListener(new OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position,
                long id) {

            Toast.makeText(getBaseContext(), "Item clicked: " + position, Toast.LENGTH_LONG).show();

        }
    });

In the above example, "int position" represents the item that was clicked. A toast message is displayed with the index of the item that was clicked.

Upvotes: 1

Related Questions