So I have a View which i want to replace with another View. So far i've done this.
View child = fragmentView.findViewById(R.id.view1);
ViewGroup parent = (ViewGroup) child.getParent();
int index = parent.indexOfChild(child);
parent.removeView(child);
child = new MyCustomView(this.getContext());
parent.addView(child, index);
While this works. It also removes the constraints so the new View ends up in the top left corner. I can't seem to find a way to store the constraints placed on the original view and allow the new view to use the same.
Update:
I tried with the following
View child = fragmentView.findViewById(R.id.view1);
ViewGroup.LayoutParams childParams = child.getLayoutParams();
ViewGroup parent = (ViewGroup) child.getParent();
int index = parent.indexOfChild(child);
parent.removeView(child);
child = new MyCustomView(this.getContext());
parent.addView(child, index,childParams);
It seems to be on the right track but not quite there yet.
XML:
i include my custom view in my fragments xml by the following:
<include
android:id="@+id/view1"
layout="@layout/my_custom_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toTopOf="@+id/button5"
app:layout_constraintEnd_toStartOf="@+id/button2"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
the my_custom_view XML looks like this
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_height="wrap_content" android:layout_width="match_parent">
<Button
android:id="@+id/button"
android:layout_width="100dp"
android:layout_height="100dp"
android:text="B1"
android:textSize="30sp"
app:layout_constraintBottom_toTopOf="@+id/timeLeftText"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/peopleText" />
<TextView
android:id="@+id/peopleText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="0/5"
android:textSize="18sp"
app:layout_constraintBottom_toTopOf="@+id/button"
app:layout_constraintEnd_toEndOf="@+id/button"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/timeLeftText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="19:30"
android:textSize="18sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="@+id/button"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/button" />
<ImageView
android:id="@+id/imageView3"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_marginStart="16dp"
android:src="@mipmap/ic_launcher_round"
app:layout_constraintStart_toEndOf="@+id/button"
app:layout_constraintTop_toTopOf="@+id/button" />
</androidx.constraintlayout.widget.ConstraintLayout>
This view is supposed to be inflated in MyCustomView.java However the code never runs MyCustomView.Java only the XML.
The MyCustomView.java looks like this.
public class MyCustomView extends ConstraintLayout {
public MyCustomView(@NonNull Context context) {
super(context);
LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
layoutInflater.inflate(R.layout.my_custom_view, this, true);
}
}