I have a linear-layout "draggableBallsContainer" that includes 10 vertically-aligned balls which can be dragged and dropped. After dragging and dropping some of the balls into another layout, I want to re-initialize "draggableBallsContainer" by clicking on a button.
After clicking on the button, the balls are not correctly aligned, and some of them are even not visible inside "draggableBallsContainer" anymore.
here is the xml:
<LinearLayout
android:id="@+id/draggableBallsContainer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:layout_margin="5dp"
android:layout_weight="2"
android:background="@drawable/background_right_layout"
android:gravity="center"
android:orientation="vertical">
<ImageView
android:id="@+id/ball_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="1"
android:contentDescription=""
android:src="@mipmap/ball" />
.
.
/* balls removed for readability purposes */
.
.
<ImageView
android:id="@+id/ball_10"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="1"
android:contentDescription=""
android:src="@mipmap/ball" />
</LinearLayout>
OnClick will run this code:
draggableBallsContainer.removeAllViews();
//int i = 0;
LinearLayout.LayoutParams ballLayoutParams = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT, 1f);
ballLayoutParams.gravity = Gravity.CENTER;
for (ImageView ball : balls) {
draggableBallsContainer.addView(ball, ballLayoutParams);
}
By running this code, my expectation is to get the exact same initial state ( 10 Balls vertically aligned). However, only some of the balls will be displayed, and "randomly" aligned.
I tried to call invalidate as answers on some similar topics suggested, but in vain.
I would appreciate your help and thanks in advance.