I want to create an activity and this activity will show the current time at the top of the screen. I want to show and I need that it changes as time passes by.
I`ve tried this
hora=(EditText) findViewById(R.id.editTextTime);
String mydate =java.text.DateFormat.getTimeInstance().format(Calendar.getInstance().getTime());
hora.setText(mydate);
Itt shows the current time but it doesn`t change.
Related
I have 2 activities, the first activity has a textView which keeps track of a count (with a max threshold) based on when a user clicks a button. Now, when I go to my second activity to change the max threshold and go back to the first activity, I want the threshold to be the previous one (ie not the new one set, only when the user clicks a 'reset' button will the new threshold take effect).
For example, on the first activity, the threshold is 5. I go to the second activity and to change threshold to 10 then press the up button to go back to the first activity. The threshold should still be 5 until I click the 'reset' btn.
I know I can use SharedPreferences but based on what I read it shouldn't be used. I've also tried onSavedInstanceState but to no avail.
You simply can use intent to pass values between your activies.
When you want to send value from one activity to another
Intent i = new Intent(thisActivity.this, newActivity.class);
i.putStringExtra("your_key", value);
startActivity(i);
In your new activity
String value = getIntent().getStringExtra("your_key");
I want to show the same image on the screen non-stop while the 3 activities start one by one : LoadingActivity, MainActivity, and then DetailActivity, and all this time the user sees only one image.
Thus, the picture should hide starting multiple activities in a row. Typically, when the application starts, starting LoadingActivity, then MainActivity, then DetailActivity. Usually it ends at the MainActivity, and while the LoadingActivity is starting , the user sees the Welcomу layout for a few seconds, and then the MainActivity interface. It's very rarely necessary to go to the DetailActivity, for example, when the browser link to the DetailActivity has already been chosen.
But when I get a push notification, I need to go to the DetailActivity immediately bypassing the LoadingActivity and MainActivity, I do not launch multiple services, do not take updates from the server, do not set the required variables, which means that when the user tries to do something from the DetailActivity, the functionality will be limited.
But what I'm trying to say is that I don't like that all 3 interfaces are shown when user presses the push notification, so I decided to ask if it's possible to have only one picture hanging during all these three activities loading?
To get a picture hanging in a row, all three activities, I duplicated to the MainActivity some key points from the LoadingActivity. Push starts MainActivity, if it is push, then I don't show a main layout, but an additional, with a picture. Next, I run websoket, login and query the server for updating data relating to this particular push. I receive data and load the DetailActivity, displaying the right information.
Additionally I made: - in onStop of MainActivity I switch to main layout, so that when user press the back of the DetailActivity, he sees a normal layout of the MainActivity.
- If within 10 seconds do not get a response from the server, then just display DetailActivity without new information. This is not good, but better than to open the MainActivity user may not immediately find the desired option and opens DetailActivity.
I have a ViewPager with a FragmentStatePagerAdapter and the starting page should be somewhere in the middle. But when the user goes to another Activity and then comes back, the page number should be saved, so that he returns to the same page. There is probably an easy way to get this done, but I can't find the solution.
Usually the current page number will be saved, so this is not the point. The problem is that I don't know where to put the code that sets the starting page at first, because when I put it in onCreate, the starting page will be shown even if I come back from a different Activity.
I also tried saving the number in a variable in the onPause method:
this.currentPageNumber = viewPager.getCurrentItem();
But next time when onStart() is called, the variable currentPageNumber is null, so it seems that variables are not saved after onDestroy().
I could use a static variable, but it feels wrong. Is there a better solution?
EDIT: Sorry, I didn't make clear enough, that I want the saved page only be opened, if I come back to this Activity after I launched it. Every time I start the Activity from the launcher, the starting page should be shown and not the saved page.
EDIT 2: The behaviour I want to have is exactly the same as the Google Calendar app has when you open the day or week perspective. If I open this app, the current day will be shown. If I swipe to another day, then open settings, then press back, this other day is still be shown. But if I restart the app, again today will be shown.
After you have initialised your viewpager, use this method :
viewPager.setCurrentItem(int position)
Or this :
viewPager.setCurrentItem(int position, boolean withAnimation)
You can save the last position by using SharedPreference in the onPageSelect() method where you can get the position of each page. You have to implement the OnPageChangeListner in order to have this method.
Edit
Your question wasn't clear :
Sorry, maybe I didn't express my problem well enough. I want the starting page to appear everytime I start my app. But if I enter the activity by the back-button (for example if I opened my settings activity and then go back) the last viewed page should be shown. The solution provided in the link will lead to the safed page everytime I open the app
I don't know why you want to change this, it's a normal behavior, read this.
But if you insist, you can always use setCurrentItem() method in the onResume of your Activity/Fragment, thus the first page will always be shown after your Activity/Fragment gets resumed.
Edit 2
That can still be done by setCurrentItem. In your adapter, try to detect the index of the page of the current day. Create a method that returns that field from outside the adapter. And then after you have initialised your ViewPager,
viewpager = (ViewPager) findViewById(R.id.viewpager);
viewpager.setAdapter(adapter);
setCurrentItem(adapter.getCurrentDayPosition()) // or something like that ...
The method in the adapter :
public int getCurrentDayPosition() {
return this.currentDayPosition // this a field of the adapter.
}
I have the same requirement to show the default page whenever the onCreate of the ViewPager hosting Activity is called and show the current page for all the other cases including the motivating case: when user navigate back from the Activity started from the current page (onCreate won't be called in this case). My solution is based on the time spend between onCreate and onResume.
If the time between onCreate and onResume is shorter then a threshold (I use 1s assuming onCreate could be finished within 1s which we really wanted for the good programming practice), then we believe this is the first time the onStart/onResume is called right after the onCreate, otherwise we believe it is about all the other cases including our motivating case.
Then we only need to set the current timestamp in onCreate and do the time comparison in onResume to make the decision to set the current item of ViewPager to default or current. Note that we need to save the current page number in onPause in SharedPreference.
I am not claiming this idea is perfect but it works fine for me so far. Let me know if you are interested in the implementation and I will post my code upon your request.
i think this solve your problem , because first time you launch your activity there is no instance saved (savedInstanceState) ...
read comments in the post ...Android :Save View Pager State ...
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.
i have a gridview which is a calendar. once the user selects a date and decides to go to another activity , upon return the day he had previously selected should remain selected.
upon selecting a day the image of the gridview cell that was selected gets changed. now when the user returns , if i can simulate the onclick event , then i would not have to write extra code.
in summary, i need to simulate a onclick event on a gridview cell via code. how do i do it?
thank you in advance.
You do not need to simulate the click. You just have to store somewhere(shared preferences is ok) the day, which is choosen. If there is no such value, then no day is choosen.
So, all you need to do it to save the day and read the saved value in your onResume() method.
If want still reuse onClick, just move the code from onClick() method to the separate one, and call it with your onResume().