How to get current time of action in Android? - android

How can I get the time an action was performed in the users preset format (24/12Hr)? I'm trying to set a textview with the time an onClick was performed.
EDIT: I've tried
Time currentTime = new Time();
currentTime.setToNow();
However, instead of XX:XX/X:XX I get a bunch of unneeded text.

The question have been asked already, e.g. here.
So, to put current date-time in TextView use the following code:
time.setText(DateFormat.getDateTimeInstance().format(Calendar.getInstance().getTime()));
If You need only time (without date), then it should look the following:
time.setText(DateFormat.getTimeInstance().format(Calendar.getInstance().getTime()))
Also, checkout getTimeInstance(int style) which allows to use different style of the format (e.g. long, short, medium).

Related

How do I set multiple notifications using one date selection?

I need to set multiple notifications at the same time based on a list of mutiple choice questions. I will figure the radio buttons and if statements out later, for now I'm just setting the dates randomly for testing purposes.
If the user selects jan 1st and clicks a button. It will set a notification in the future with a reminder to do an activity. It also saves this information so the user can edit the details if he desires or change a few options.
This part I already have coded and it works just fine. however, It only sets a single notification currently. It is a pretty involved code consisting database, receivers, fragments, etc. that all talk to each other.
As I said, this part I have working just fine but I am not including all the code because it is seriously involved and no one would try to break it down if i drown you in a sea of code. I can certainly post specific code if someone request it.
My issue is that I need it to set a good amount of notifications at various future dates upon the button click, not just one. I need to change the notification message to a preset variable string for each additional notification event but some things like title will remain the same.
my current working code executes like this....
User Selects Jan 1 > User Clicks Button > Notification set for Feb 14 with a unique title and message set by the user and saved for future editing...
At the same time the notifications are set they are saved so the user can change the date and a few options if needed. I want only one title and a single date saved. I have the save feature already working but I need to know how to link the additional reminders to the existing saved item. Im trying to make all the data linked so that if the user deletes the saved item, all the set notifications for that file are deleted and not just the one that falls on the user selected date.
===== This is what I am trying to pull off
User Picks Some Options and Clicks A Button [I have this working already]
Upon Click of said button, the following notifications are all set in the future :
[currently it sets this notification only]
Jan 1 notification :
(title)Day Master App
(message)Happy New Year
[Im trying to figure out how to add the following notifications upon the above button click and also save this information to the existing db under the same item]
Feb 14 notification : (title)Day Master App - (message)Its V Day
March 1 notification : (title)Day Master App - (message)Spring Is Near
March 14 notification : (title)Day Master App - (message)Its probably raining
(((and we'll just pretend i finished the list...)))
I cant make the future notifications a static number because when the future notifications are set is determined by a bunch of radio button choices before the user clicks the execute button. This is going to be a pretty complex and hacky if/else novel the way I think I have to do it. Am I correct?
I have a display/edit listview that shows your saved notifications and the unique name and date the user set. This works fine currently but only sets a single notification based on user input.
I need to add some more notifications but i dont need to save them under a different item or name. I want them all under the same save item so all the notifications that were set when the button was clicked can be added and deleted as a group. There will be no option to delete certain notifications that were set. It will be all or none.
I would imagine I could just add some more variables into the existing "save notification" code? As in piggyback some more items (like all the dates and messages) for the additional notifications? do i need to write a new function for each future notification I set in order to be able to delete it?
do I need to create a new db for each additional notification that is set? A separate Adapter? Im so confused...
===
Im not looking for a code example exactly, I want to know how this would be implemented into an existing code. I realize there is probably 100 ways to code what I have described. I just need the process explained.
Please explain this to me slowly. I know im way overthinking this.
I tried to explain this as best I could, if you need clarification on something please ask. Thank You.
I have solved my problem although I could not accomplish exactly what I was trying to originally.
I managed to set the multiple notifications on a single click with different dates by simply copying the same code I used to display the single notification and simply giving each additional notification a unique ID and creating a new variables to give them individual text, future dates, separate times, etc. These variables can be easily set programmaticly or by the user when you set them up.
// On clicking the set notifications button
public void SetNotificationsButton(View v){
ReminderDatabase rb = new ReminderDatabase(this);
// Creating Original Reminder
int ID = rb.addReminder(new Reminder(mTitle,
mDate, mTime, mRepeat, mRepeatNo, mRepeatType, mActive));
// Create Feeding Notification
int FeedingID = rb.addReminder(new Reminder("Have you fed the cat today?",
mFeedingDate, mFeedingTime, mRepeat, mRepeatNo, mRepeatType, mActive));
}
//
// and just continued copy and pasting the rest of the notification events
// changing the ID for each individual notification. Without a unique ID
// the current notification will override the previous notification and
// appear to only set the last reminder.
I was not able to remove all notifications by selecting a single list item, I probably could have figured this out by grouping the notifications into a separate variable but once I realized that you can only have a maximum of 50 notifications set at any given time it seemed like overkill.
Since I have a repeat option for certain notifications I can keep my notifications set; numbers low and stay away from the 50 at a time limit while still firing a reminder to the user every day.
I found a great example for setting single notifications I used as reference.
https://github.com/blanyal/Remindly
Thank those of you who took the time to read my question.

Change textView according to date/time (Java, Eclipse)

I want to create a textView that will change according to date and time. For example, I want to create a textView that'll say "Today is >weekday<". The app will check the date/time on the phone and check which day it is. For example on 6th June 2013 it'll say "Today is thursday".
I'd appreciate any help, thank you!
Yes of course, check out http://docs.oracle.com/javase/6/docs/api/java/util/Calendar.html
All you need to do is create a textview, then get the current day of the week from the Calendar library and set the text of the textview using that information.
To make it dynamic, you'll need to put this in onCreate() and possibly onResume() that way each time that application is created or restarted the content of the textview can be reset.

solution for SQLite function for time difference without java code

Hey please pay attention to solve this problem I know this could be possible but I am not getting the way ..
I am using sqlite in android to store and manipulate the field as shown below in the figure
Figure http://s16.postimage.org/idc13pxdh/image.png
first_start_time,last_start_time,last_end_time are datetime field in SQLite database
I need to get the difference of last_start_time and last_end_time as mean of function so that I don't need to do it programatically
last_end_time = strftime('%Y-%m-%d %H:%M:%S','now','localtime')
last_start_time= strftime('%Y-%m-%d %H:%M:%S','2012-01-27 02:34:56')
,i,e if my firsttime is =2012-06-15 14:54:33
and Endtime is =2012-06-15 14:54:40
then time diff would be like this =00:00:14;
i want to have such difference time like last_end_time -last_start_time
i also want to use trigger to do use in sqlite data base in android
at the last I need that all my total_expended_time should get summed up like ...
select sum(total_expended_time) from study_result
.. one thing keep in mind the result should be in datetime stamp so that i can reflect it to my UI
Any suggestion will be appreciated

AndroidPlot, setting different DomainValueFormats

I am trying to do the following for the x labels in my graph.
The first label should have format:
plot.getGraphWidget()
.setDomainValueFormat(new SimpleDateFormat("dd/MM/yyyy hh:mm:ss"));
All the other labels should only show minutes and seconds:
plot.getGraphWidget()
.setDomainValueFormat(new SimpleDateFormat("mm:ss"));
I can set the Format for all labels, but I can't figure out how to change it for only the first label. My graph is dynamic and changes every second (incoming data gets updated each second with a new value).
Is there maybe a function that could help me? (It's all a bit difficult to find with website being down...) Or any other tips?
Any help is greatly appreciated.
Assuming you have no repeating values in your domain model you could keep track of your first value and test for equality inside a custom Formatter implementation. It's not pretty but because there is no way to pass in the index of the item being drawn to the Formatter that's about the only way I can see doing this.

Can we added changing image/text on android app

Is there a way in android app that we can add some text or image and we can change that dynamically without adding new version. Like we have this text: Today's deal is: 1
then in a week if i want that text to show as: Today's deal is: 2.
Is there a way to do this, for an app which does not have a server and is stand alone app.
A text or image can be altered during run time; so how you choose to change that is up to you. You would have a simply switch statement depending on the day of the week for your specific example.
TextView tv = (TextView) findViewById(R.yourxml.name);
tv.setText("Monday!");
...
One way I can think of to achieve this is by writing a long value in the shared preferences for a Date object. Each time you start the application we will check if it's been 1 day since the value was saved. Also maintain the different string you want to display as string array in the resources.
I hope it helps...

Categories

Resources