Search code examples
androidtextviewandroid-textattributes

Get TextView in the parent of prent of Button


I have a layout for a row of LIstView items like this:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="match_parent"
    android:layout_gravity="right"
    android:descendantFocusability="afterDescendants"
    android:gravity="right"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/label"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="right"
        android:gravity="right"
        android:textSize="40sp" 
        android:focusableInTouchMode="false"
        android:clickable="false"
        android:focusable="false"  />

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:background="@drawable/btn_background" >

        <Button
            android:id="@+id/share_btn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_alignParentTop="true"
            android:layout_marginTop="2dp"
            android:background="@drawable/delete_btn"
            android:drawableRight="@drawable/delete"
            android:paddingBottom="3dp"
            android:paddingTop="2dp"
            android:text="share"
            android:onClick="myClickHandler" />

    </RelativeLayout>

</LinearLayout>

And i want to get the text of TextView when user click on delete button(in myClickHandler() method),

with this code:

RelativeLayout vwParentRow = (RelativeLayout)v.getParent();

I get the parent of Button(RelativeLayout), but don't know how to get the text of TextView that in parent of parent of Button

How i can to do this?

Thanks


Solution

  • I found a solution for you:

    In your myClickHandler-method you can use the following code to determine what the content of your textview was:

    public void myClickHandler(View v) {
        ViewParent relativeLayout = v.getParent();
        ViewParent linearLayout = relativeLayout.getParent();
    
        TextView textView = (TextView) ((View) linearLayout).findViewById(R.id.label);
        String text = textView.getText().toString();
        Log.i("textview content", text);
    }