I have a RelativeLayout with many textview like in this main.xml sample.
main.xml
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:background="#000000"
android:id="@+id/RelativeLayout"
>
<TextView
android:id="@+id/tv00"
android:background="#ffffff"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:clickable="true"></TextView>
<TextView
android:id="@+id/tv01"
android:layout_width="wrap_content"
android:layout_below="@+id/tv00"
android:padding="5dp"
android:clickable="true"></TextView> ...
...</RelativeLayout>
I want in my activity.java to get access to the id of a textview I click. How to do that?
With onClickListener I can listen on RelativeLayout, but how to do it for a specific textview in that layout. Something with getid()?
activity.java
RelativeLayout rv =(RelativeLayout)findViewById(R.id.RelativeLayout);
rv.setOnClickListener(new View.OnClickListener(){
public void onClick(View v) {
}
});
As far as I know there is nothing like a “onChildClick” listener for RelativeLayout.
You could iterate over your TextViews (using
rv.getChildCount()andrv.getChildAt(i)) and add click listeners to the individual views…however, if your RelativeLayout just contains a list of TextViews you might want to consider using a ListView instead (and itssetOnItemClickListener()).