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!
You can find the
friendNameview by usingfindViewByIdon the new instance of custom view created by you(the one you would be adding to view hierarchy)No..they are not randomly generated at run time. They will be what you have already assigned to the view(e.g.
friendName).