Get custom view embedded element

219 Views Asked by At

I am creating a custom view on the click of a button. The custom view is:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="100dp"
    android:gravity="center"
    android:orientation="horizontal">

    <ImageView
        android:id="@+id/friendImage"
        android:layout_width="100dp"
        android:layout_height="match_parent"
        android:contentDescription="Person Image"
        app:srcCompat="@drawable/person2" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:orientation="vertical">

        <TextView
            android:id="@+id/friendName"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:gravity="center"
            android:text="Name of friend"
            android:textAlignment="center"
            android:textColor="#000000"
            android:textSize="20sp" />

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:orientation="horizontal">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:gravity="center"
                android:orientation="horizontal">

                <ImageView
                    android:id="@+id/interactionInPersonIcon"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_margin="3dp"
                    android:layout_weight="1"
                    app:srcCompat="@drawable/in_person" />

                <TextView
                    android:id="@+id/interactionInPersonText"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:text="299"
                    android:textAlignment="center" />
            </LinearLayout>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:gravity="center"
                android:orientation="horizontal">

                <ImageView
                    android:id="@+id/interactionVideoIcon"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_margin="3dp"
                    android:layout_weight="1"
                    android:tint="#292828"
                    app:srcCompat="@android:drawable/presence_video_online" />

                <TextView
                    android:id="@+id/interactionVideoText"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:text="299"
                    android:textAlignment="center" />
            </LinearLayout>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:gravity="center"
                android:orientation="horizontal">

                <ImageView
                    android:id="@+id/interactionTextIcon"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_weight="1"
                    app:srcCompat="@drawable/text_icon" />

                <TextView
                    android:id="@+id/interactionTextText"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:text="299"
                    android:textAlignment="center" />
            </LinearLayout>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:gravity="center"
                android:orientation="horizontal">

                <ImageView
                    android:id="@+id/interactionPhoneIcon"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_weight="1"
                    android:tint="#353535"
                    app:srcCompat="@drawable/phone_icon" />

                <TextView
                    android:id="@+id/interactionPhoneText"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:text="299"
                    android:textAlignment="center" />
            </LinearLayout>
        </LinearLayout>
    </LinearLayout>
</LinearLayout>

The user enters some text (a name) into a text field, and presses the button to create the new custom view (a friend in their friend list).

What I am trying to do is access the friend name text view that is embedded in the view. It has the ID "@id+/friendName" but I can't seem to work out how to access this.

So I guess the question is - if I know the ID of the FriendView element, how do I then get the friendName TextView element within it.

Also is there an issue that once another custom view is created (a new friend), these IDs of the embedded views within each custom view will be the same, the only differing factor is that the custom view IDs will differ. Are these randomly generated at run time?

Maybe getting this embedded view by ID isn't actually the best way anyway?

Thanks so much for your help in advance!

2

There are 2 best solutions below

2
upenpat On

The user enters some text (a name) into a text field, and presses the button to create ? > the new custom view (a friend in their friend list).

What I am trying to do is access the friend name text view that is embedded in the view. > It has the ID "@id+/friendName" but I can't seem to work out how to access this. So I guess the question is - if I know the ID of the FriendView element, how do I then > get the friendName TextView element within it.

You can find the friendName view by using findViewById on the new instance of custom view created by you(the one you would be adding to view hierarchy)

Also is there an issue that once another custom view is created (a new friend), these IDs of the embedded views within each custom view will be the same, the only differing factor is that the custom view IDs will differ. Are these randomly generated at run time?

No..they are not randomly generated at run time. They will be what you have already assigned to the view(e.g. friendName).

0
Mee On

Okay so I hadn't twigged something when I was trying to do this and I was confusing myself.

My custom view XML file creates the IDs e.g. "@id+/friendName" for the TextView I was interested in. This means that R.id.friendName is populated. However, I didn't realise that what was confusing me was more about the context of where this friendName ID appears.

The findViewById method acts directly on a View and so my issue was that I wasn't findViewById on the custom view class (FriendView). I solved it with:

this.findViewById(R.id.friendName);

from within the FriendView class.

To answer my follow up question - I debugged the code when my customView is created and it sets the ID as -1.