I am in desperate need of help. I have been trying to solve my issues out for days and I am losing any desire to carry on with my project. I just can't seem to understand, so please can you help me do so.
I am trying to create an activity_all_friends.xml that holds a list of friends. My idea was that I would have a linear layout that holds custom views ("friendView").
I have created the friendView:
<?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_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_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" />
</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>
Anyway, when I try and draw this new view on my activity_all_friends layout, it appears blank:
I also want to add these new friends with the click off the "Create New" button on the top right. I have assigned setOnClickListener to the button. The button press is working and the new elements seem to be ceated, I just cannot see them.
My FriendView class:
public class FriendView extends LinearLayout {
private Context context;
private String friend;
public FriendView(Context context) {
super(context);
init(null,0);
}
public FriendView(Context context, AttributeSet attrs) {
super(context, attrs);
init(attrs,0);
}
public FriendView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs,defStyle);
init(attrs,0);
}
private void init(AttributeSet attrs, int defStyle){
}
public String getFriend() {
return friend;
}
public void setFriend(String friend) {
this.friend = friend;
}
public View getView(View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.friend_view, null);
TextView friendName = (TextView) view.findViewById(R.id.friendView);
TextView interactionInPersonText = (TextView) view.findViewById(R.id.interactionInPersonText);
TextView interactionVideoText = (TextView) view.findViewById(R.id.interactionVideoText);
TextView interactionTextText = (TextView) view.findViewById(R.id.interactionTextText);
TextView interactionPhoneText = (TextView) view.findViewById(R.id.interactionPhoneText);
// TODO
friendName.setText("Temp random");
interactionInPersonText.setText(Long.toString(Math.round(Math.random() * 300)));
interactionVideoText.setText(Long.toString(Math.round(Math.random() * 300)));
interactionTextText.setText(Long.toString(Math.round(Math.random() * 300)));
interactionPhoneText.setText(Long.toString(Math.round(Math.random() * 300)));
return view;
}
@Override
protected void onDraw(Canvas canvas){
canvas.drawColor(Color.BLUE);
}
}
Anyone got any ideas as to why this is happening? My understanding is that I need to write the xml element that I want to generate (the friend_view.xml in my case), I want to write the class which dynamically handles what happens to the element (the friendView class in my case), and then I want to link that to the layout where I want it to be generated (the activity_all_friends.xml in my case). Is this correct?
2 other small questions: I have my friends_view.xml in the layout folder, is this correct? Where do the attributes come in (attr.xml)? I want to hit my API and dynamically change the numbers and friends name.
Thanks for all your help!!
You should inflate your layout into your custom view using inflater.inflate(R.layout.friend_view,this,true) without assigning to any variable and you should call this in your init function.