Summary:
A view starts with visibility = 'GONE'. When the view is set to visible, I want everything below it to move down by the height of that view. It doesn't work the first time, but works every time after that.
Explanation:
I'm using a radio group that has three options for delivery.
If the user clicks the third option, there's a second radio group that shows up with more options.
The second radio group is only visible when the third option of the first Radio Group is selected. When it becomes visible I want everything below it to slide down to make room. I've added view.animate().translationY() calls for each view below the two radio groups. This works to move everything out of the way when the second radio group shows up, and slide them back when its gone. The problem is that this only works the second time around. The first time the radio group appears, the views all just stay in place.
Attempts:
SystemClock -- I've tried adding a SystemClock.sleep(1500) call. This just adds a delay to the animation, but it still doesn't work the first time.
Handler -- I've tried adding a Handler to handle the animation views, same problem.
Start Delay -- I've tried adding a Start Delay as mentioned in this stack-overflow answer and here this one
Hard Coding the height -- I've tried hard-coding the distance to move down. I thought that the problem might be that I'm getting the height toMove by calling getHeight() on the radio group after its set to visible. If this was the problem, I though hard coding the height might work, but the same issue persists.
Vertical Chain -- I tried attaching all the views in a vertical chain, and adding the radio group to the chain when it becomes visible. The radio group couldn't be included in the chain when visibility was 'GONE', adding it to the chain did nothing. If I added it to the Constraint Layout after the visibility is set to 'VISIBLE', the app crashes.
Screenshots:
This is what happens the first time I click on the radio button: screenshot: radio group shows up, views don't slide down
This is what happens when I click away from the last radio button: screenshot: radio group 2 goes away
This is what happens when I choose "Standard Delivery" after that, and what I want to happen the first time too: screenshot: radio group 2 is set to visible, the other views slide down
Code:
GitHub: This is the whole app GitHub: The specific java file -- radio group switch starts on 109
Snipit:
--- Switch Statement ---
...
...
case R.id.standard_delivery_radio:
delivery = STD_DELIVERY;
deliveryType = "Standard Delivery";
stdDeliveryGroup.setVisibility(View.VISIBLE);
break;
}
// Move the views down
if (stdDeliveryGroup.getVisibility() == View.VISIBLE) {
toMove = stdDeliveryGroup.getHeight(); }
// Move the views back to starting position
if (stdDeliveryGroup.getVisibility() == View.GONE) {
toMove = 0;}
// Animate the Views
shippingNumber.animate().translationY(toMove);
shippingText.animate().translationY(toMove);
orderButton.animate().translationY(toMove);
totalText.animate().translationY(toMove);
totalNumber.animate().translationY(toMove);
Notes:
- This is happening inside a fragment.
- The code is inside a switch statement inside the onCreateView method.
- The fragment is inside the main activity, which has a view pager with tabs.
If you want to find the code in the file, its under java/com/example/OrderScreenFragment.java
I really have looked around and can't find why this is happening, please don't mark as duplicate or vague. If you need anything clarified, I will be happy to clarify.