I have a main layout in which I’d like to sequentially display / hide various custom layouts according to timers and user input.
In my example, I’d like the following timeline:
-
show an initial layout, created by the class MainStart. The user will click on the button when they’re ready to start.
-
after the user clicks, we’ll run a countdown timer, displaying the countdown to the screen. This is done by MainCountdown.
-
once the countdown is complete, MainLast is displayed. It has a button allowing the user to click “Again?” when they want to start over.
-
if the user clicks “Again?” then go to step 1.
According to this question, I may have to use removeView() and addView() to achieve what I want.
I pushed to GitHub a trimmed down version of what I want to do: Hide and Show Layouts so you can see everything, but the problem seems to boil down to myView still being visible after this:
myView = inflate(mContext, R.layout.main_start, _view_group);
myView.setVisibility(View.GONE);
Can I make a small change to my code/layouts to make the views hide/appear the way I want?
Am I completely Doing It Wrong?
From your 1-4 steps, it sounds like a custom View might be overkill for this, but if for whatever reason, you require that it be a custom View, it can still be done pretty easily… And it can be done with a ViewFlipper (or other ViewAnimator), or you can simply have 3 sibling children, and set visibility of each as needed. A bird’s eye view would be something like…
startCountdown(): setDisplayedChild(1) — this is the TextView. Also start the countdown: this can be with a Timer object, or using View#postDelayed().
onCountdownComplete(): setDisplayedChild(2) — this shows the end “Again?” button.
onClick of button2: setDisplayedChild(0)
The countdown can be done with postDelayed() and a Runnable object to decrement the counter, or possibly more performant: use sendEmptyMessageDelayed(). When that handler message is received, decrement the internal counter, and update the TextView.
Edit
Another option for the countdown is CountDownTimer — similar concept, but has more of a “callback” feel to it. Your tick interval would be 1000, and
millisInFuturewould be1000 * <numberOfSeconds>. In theonTick()method, update the TextView withmillisUntilFinished / 1000. In theonFinish()method, call setDisplayedChild(2).