get the index of a child view in android

In onclicklistener, Get the parent of TextView

TableRow r = (TableRow) ((ViewGroup) textView.getParent());
int position = r.indexOfChild(textView)

Hope this helps


First you need to get a reference to the parent view with .getParent(). Having the parent you can use indexOfChild(myView) to get the index of your view within the parent. In one row it looks like this:

int indexOfMyView = ((TableRow ) myView.getParent()).indexOfChild(myView);

This should work with any type of parent view:

int indexOfMyView = ((ViewGroup) myView.getParent()).indexOfChild(myView);