When I try to add two chronometers in the same activity, they always show the same time.
I am trying to implement a board game like chess and keep the time of players. So initially one chronometer runs and the other one waits, when one chronometer stops the other one starts.
But for example, when chrono1 shows 00:15 and chrono2 shows 00:00 if I stop chrono1 and start chrono2, it jumps to 00:15 and resumes from there while it is supposed to start from 00:00.
I use the following code:
Chronometer player1Chrono = (Chronometer)findViewById(R.id.player1Chrono);
Chronometer player2Chrono = (Chronometer)findViewById(R.id.player2Chrono);
player1Chrono.start();
...
//when player 1 makes a move
player2Chrono.start();
player1Chrono.stop();
Any idea how to solve this?
Use this before starting player2Chrono,
player2Chrono.setBase(SystemClock.elapsedRealtime());
player2Chrono.start();
It will start from 0.
Related
I'm trying to make a CountDownTimer App with some influence from the Android documentation. The App has a button and textview. When the button is clicked the countdown starts and when the same button is clicked the countdown timer stops. The timer is getting stopped when I press the back button or navigate to another activity in the App.
I want the timer to continue counting down even when the above operations are performed and the activity should be destroyed only when the timer isn't running.
How can I do this? Can anyone give me an in detailed answer?
Whenever you navigate to next activity your current one goes to onPause()(if not finished()), and the UI thread goes to sleep, so in order to update your timer when you leave and again come back to the activity is updating the UI whenever your Activity resumes itself(which is what onResume() is for). Trying using a boolean to detect pause event this is because when the activity starts it calls onResume also.
So summary is whenever leaving current activity store a boolean and while returning check it in onResume() and update Ui accordingly. Hope this helps.
I have an Android app that does about 30 seconds of heavy duty number crunching at the start of the app. How do I display a screen that disappears at the end of the initial processing?
What I currently do is:
My activity_main.xml's topmost view is a ViewAnimator which has (for now) two child views - the main view, and a FrameLayout which currently is a white background that fills the screen and a child text view with "Processing..." on it.
My MainActivity.java's onCreate() method calls a user-defined method doInitialProcessing(); doInitialProcessing calls the ViewAnimator's setDisplayedChild method to show the "Processing" view, then does the number crunching, then calls setDisplayedChild again to show the main view.
In the Eclipse Emulator, what usually happens is, the screen turns white at first, then goes black, then the main view appears. (Every now and then, the "Processing..." text shows up.)
Is this the right way to go about this? Show the initial call to setDisplayedChild be in a separate thread? Does the fact that it is called from onCreate have anything to do with it?
There are two ways to do it:
Create a normal activity with a layout for your splash and start the other activity using a timer. (this way, you cannot load data in the second activity)
The other way is to create a splash fragment, that is displayed for a few seconds while the content is loaded in the background.
I use the following timer to start my activity from SPlash activity. This can also be used in a fragment:
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
Intent in = new Intent(SplashActivity.this, HomeActivity.class);
startActivity(in);
finish();
}
}, SPLASH_TIME_OUT);
SPLASH_TIME_OUT is the delay in integer value. (1000 for sec)
You can use a Splash Screen that shows something like the logo of your app for instance for some seconds while the app munches on the numbers.Here is a good tutorial on creating Splash Screens.You can also read up on this StackOverflow question.
Is it possible to start an activity in the background? That is, instead of showing it in the front, I want it to start in the background and want it running "under" another Activity.
Is this possible?
Are you looking for Service ?
Activity is supposed to be run in foreground while Service is supposed to be run in background
There can be only one top Actvity!
You can't start two activities simultaneously but what you can do is start the activity that you want in background 1st and inside the onCreate method of that activity call the foreground activity in such a way you can have desired activity at top and another one in background ready to be brought to foreground when required.
Your mainActivity---->open Activity1(supposed to be in background)-->Activity2(Activity2 comes FG while Activity1 goes in BG).
I want to create an ending splash screen that should come up at the end of the application as i have exit button in the last page so i want that when someone clicks on exit button a good bye page should appear for 3 seconds having a image and soft music tone and then application should automatically end, i am confused how should i fix this end page in my application, i am thinking to code at click event of exit button and via intent start the good bye page and then how to code?, so that it should exit out of application after 3 seconds of soft music, also what changes i have to make in the androidMainfest file for that.
start a CountDownTimer in your ending splash activity's oncreate method, set it to finish after 3000 milisenconds and write activity.finish in its onfinish event handler. also, you need to call activity.finish() in your last page activity after calling startactivity of the good bye page.
Just start the ExitActivity with the button and then run an CountDownTimer for 3 seconds which then calls the intent for the HomeScreen like here
I am a noob learning Android and making my own little demo project at the same time.
Basically, when the user starts my app it shows levels.java with 8 buttons, when the user clicks the 8th button this fires:
public void button_clicked8(View v) {
text1.setText("clicked 8");
startActivity(new Intent(this, GameScreen.class));
}
which starts my main gamescreen class where the simple game is played.
If the user gets the math problem wrong 3 times, the game is over and I fire this code:
r_settings.setGameStarted(false);
r_settings.setGameOver(0);
r_settings.setInternalLevel(0);
r_settings.setDisplayLevel(0);
this.finish();
which basically resets some static variables and sends the user back to the levels.java screen.
So far all of the above works like a charm, in the levels screen if the user presses the 8 button again the game starts again, the problem is this works for around 4 times, on the 4th or 5th time it goes to the gamescreen but nothing works... and then in logcat I get this error:
http://imageshack.us/photo/my-images/546/68081548.png/
http://imageshack.us/photo/my-images/59/93717315.png/
(you need to click the image to see it full size)
Where am I going wrong?
Thanks!
R
Well, you get a NullPointerException at GameScreen.java:64. Does it work after you fix that?