Use a grid-view or recyclerview instead of seperate layouts.
Constraint layout is the way to go.
<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView 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:id="@+id/ribbon_card"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:cardBackgroundColor="@color/colorWhite"
app:cardPreventCornerOverlap="false">
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/constraintLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="4dp"
android:background="@drawable/button_ribbon">
<ImageView
android:id="@+id/imageView"
android:layout_width="102dp"
android:layout_height="59dp"
android:padding="6dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@drawable/ic_antarctica_service_medal_ribbon" />
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="4dp"
android:text="@string/r1"
android:textColor="@color/colorPrimary"
android:textSize="20sp"
android:textStyle="bold"
app:layout_constrainedWidth="true"
app:layout_constraintEnd_toStartOf="@+id/img_addRibbon"
app:layout_constraintHorizontal_bias="0.103"
app:layout_constraintStart_toEndOf="@+id/imageView"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="48dp"
android:layout_marginLeft="48dp"
android:textSize="15sp"
app:layout_constraintEnd_toStartOf="@+id/img_addRibbon"
app:layout_constraintHorizontal_bias="0.09"
app:layout_constraintLeft_toRightOf="@+id/imageView"
app:layout_constraintStart_toEndOf="@+id/imageView"
app:layout_constraintTop_toBottomOf="@+id/textView" />
<ImageView
android:id="@+id/img_addRibbon"
android:layout_width="50dp"
android:layout_height="35dp"
android:layout_marginEnd="16dp"
android:layout_marginRight="16dp"
android:background="@drawable/ic_frame00"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.444" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.cardview.widget.CardView>
And then the animation logic:
itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (!isRibbonOn) {
System.out.println(isRibbonOn);
isRibbonOn = true;
System.out.println(isRibbonOn);
itemView.setBackgroundColor(Color.CYAN);
ImageView checkyImage = (ImageView) v.findViewById(R.id.img_addRibbon);
checkyImage.setBackgroundResource(R.drawable.ribbon_anim);
AnimationDrawable checkyAnimation = (AnimationDrawable) checkyImage.getBackground();
checkyAnimation.start();
} else if (isRibbonOn = true) {
System.out.println(isRibbonOn);
isRibbonOn = false;
System.out.println(isRibbonOn);
itemView.setBackgroundColor(Color.WHITE);
ImageView checkyImage = (ImageView) v.findViewById(R.id.img_addRibbon);
checkyImage.setBackgroundResource(R.drawable.ribbon_anim_return);
AnimationDrawable checkyAnimation = (AnimationDrawable) checkyImage.getBackground();
checkyAnimation.start();
}
{
if ((listener != null)) {
int position = getAdapterPosition();
int ribbon = mAwardList.get(position).getImageResource();
if (position != RecyclerView.NO_POSITION) {
listener.onAddRibbonClick(ribbon, position, isRibbonOn);
}
System.out.println("youclickedme" + ribbon);
}
}
}
});