android, disabling the button even if user exits the app - android

My program saves some data to SQLITE with datetime. There is a button save, which saves the value of 1 spinner along with datetime.
I will provide an example to make sure all understand:
User inputs at 20:03:24, now i want to disable the save button until the HOUR is at least 21:00. So every save can only be on every full hour.
Now my idea is to read the hour from SQLITE and check if the last inputed hour is != current Hour then
btn.setClickable(true);
else
btn.setClickable(false);
This is what i have created and partially works.
public void CHECK(){
sqliteDbHelper_ = new Sqlite_DBHelper(getActivity());
sqLiteDatabase = sqliteDbHelper_.getWritableDatabase();
String[] columns = {"hour_only","JAKOST"};
cursor = sqLiteDatabase.query("podatki", columns,null,null,null,null,null);
Calendar calander = Calendar.getInstance();
Integer currentHour = calander.get(Calendar.HOUR_OF_DAY);
gumb_poslji.setClickable(true);
cursor.moveToLast();
Integer lastInputedHour = cursor.getInt(0);
Log.e(TAG+"zadnja vnesena URA",""+lastInputedHour);
Log.e(TAG+"trenutna URA",""+currentHour);
if (lastInputedHour == currentHour){
gumb_poslji.setBackgroundColor(getResources().getColor(R.color.TextMainColor));
gumb_poslji.setClickable(false);
}
else
gumb_poslji.setClickable(true);
gumb_poslji.setBackgroundColor(getResources().getColor(R.color.colorGreen));
}
How to implement a listener on the app that will constantly looking if there is a difference in the app?
Also be aware the button must also change state from clicable to not clicable even if a user is in the app and the hour changes.
Since i am new to java and Android i am looking for an idea or an example how to do this, not a solution. Am i thinking in a right direction?

Related

ScrollView with too many Textviews (over 400)

I was able to make an app with a scrollable calendar, like the image:
Preview and Component Tree
The calendar itself, for 2016, looks like this:
2016 Calendar
It's a five week based calendar, and it's meant to be built like that, for its purposes.
When you run the app you have a calendar, with a week or so displayed.Then you can scroll the days and see the whole calendar. The months names stand still on the left.
Clicking the arrows updates the calendar, showing past and future years, as expected.
It works.
I still don't understand A LOT of java, but I could manage to make it work.
One search here, another there, one tutorial here, another example of code there...
It works.
The problem is: it's too slow to update the calendar.
It takes about 3 seconds or so to update the days, and you can't scroll the calendar during that time.
After the update the scroll is normal. You go right and left. No problem.
It's the processes to update all those TextViews that's killing me... Yes, I do have A LOT of TextViews, and that's where I think I could start getting some help.
The calendar is designed to display five weeks, monday to sunday, which gives me 35 TextViews per month... that's 420 TextViews...
The following method clears the calendar prior to update:
public void Clears_the_Calendar() {
int iId;
TextView tvDay;
for (iId = 0; iId < 420; iId++) {
tvDay= (TextView)findViewById(iarrTextView_Days_ids[iId]);
tvDay.setText(" ");
}
}
As you can see, I use the array iarrTextView_Days_ids to reference the TextViews. It looks like:
int[] iarrTextView_Days_ids = new int[]
{R.id.M01_D01, R.id.M01_D02, ... , R.id.M01_D34, R.id.M01_D35,
R.id.M02_D01, R.id.M02_D02, ... , R.id.M02_D34, R.id.M02_D35,
.
.
.
R.id.M12_D01, R.id.M12_D02, ... , R.id.M12_D34, R.id.M12_D35}
And this is what I use to build the calendar:
private void mtBuild_Calendar(int iYear, int iColumn) {
int i, iId, iDay, iMonth1, iMonth2, iTotal_Days;
TextView tvDay;
Date dtDate = null;
int iRight_Column = 35;
String sDay, sDate = "01/01/" + iYear;
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
Calendar cMain_Calendar = Calendar.getInstance();
Calendar cAuxiliary_Calendar = Calendar.getInstance();
try {
dtDate = sdf.parse(sDate);
cMain_Calendar.setTime(dtDate);
cAuxiliary_Calendar.setTime(dtDate);
} catch (ParseException e) {
e.printStackTrace();
}
cMain_Calendar.setTime(dtDate);
cAuxiliary_Calendar.setTime(dtDate);
Clears_the_Calendar();
// displays "1" (jan/1), according to iColumn, which is determined in another method:
iId = iColumn - 1;
tvDay = (TextView)findViewById(iarrTextView_Days_ids[iId]);
tvDay.setText("1");
// It's not important to be extremely precise about leap years:
if ((iYear % 4) == 0) {
iTotal_Days = 366;
} else {
iTotal_Days = 365
}
for (i = 1; i < iTotal_Days - 1; i++) {
// Adds a day to the cAuxiliary_Calendar and compares its MONTH with the previous day from cMain_Calendar
cAuxiliary_Calendar.add(Calendar.DATE, 1);
iMonth1 = cMain_Calendar.get(Calendar.MONTH);
iMonth2 = cAuxiliary_Calendar.get(Calendar.MONTH);
// Performs the comparisons needed to change the months:
if (iColumn < iRight_Column) {
if (iMonth2 == iMonth1) {
iColumn = iColumn + 1;
iId = iId + 1;
} else {
iColumn = iColumn + 1;
iId = iId + 36;
}
} else {
if (iMonth2 == iMonth1) {
iColumn = 1;
iId = iId - 34;
} else {
iColumn = 1;
iId = iId + 1;
}
}
// Adds a day to cMain_Calendar and displays it in the proper place according to iId:
cMain_Calendar.add(Calendar.DATE, 1);
iDay = cMain_Calendar.get(Calendar.DAY_OF_MONTH);
sDay = Integer.toString(iDay);
tvDay = (TextView) findViewById(iarrTextView_Days_ids[iId]);
tvDay.setText(sDay);
}
}
At first a method clears all the 420 TextViews, and then just the 365 or 366 needed for the year are fulfilled.
Still, that's a lot of work... this afternoon I started taking a look at canvas.drawText
For a moment it seemed to me that it would be faster, cause for what I understood TextView uses canvas inside its core. But I couldn't even find a way to make a simple canvas.drawText to work inside the ScrollView.
So the questions are:
Should I use canvas.drawText instead of TextViews? How can I do that?
Or, is there any other faster way to display 365 numbers like the app is supposed to do?
(new info, not sure how it looks...)
Trying new thigs:
Perhaps I misunderstood what adapters can do, but for what I've learned it won't help me... cause once an year is fully loaded into the view there's no delay in scrolling the calendar. The problem is how long it takes to refresh all the 365 TextViews when I want to change from one year to another. In fact, I could manage to reduce that delay to less than 1 second at this point, cause I finally follow the Android Studio advice about too many nested weights.. All my TextViews where using that attibute on its style. I removed them all, letting weights just for the LinearLayouts.
Right now I'm stuck in trying to find a way to pass a string to a method which could draw a text into the view. I still can't get a drawText to work inside the ScrollView, cause I can't find a way to call it. Seems to me that it would only works inside an onDraw, which only triggers when I load a view. But I need to get drawText doing its job 420 times (when I clear the calendar) plus 365 times when a new calendar is shown.
Am I supposed to load 800 views into the ScrollView, each time I want to chage the year ???
I also changed the main LinearLayout to RelativeLAyout, but things are getting weird cause I had to set a lot of heights (which was 0dp prior to the change) and now I'm having troubles on changing from portrait to landscape... but that's a minor issue.

Date not storing after iteration

I have an iteration that runs for a set number of times depending on another value which can vary which is why I'm using an iteration that iterates based on that value, inside that iteration I add 30 days to a date once each iteration and then add the results to a table.
PROBLEMS
I simply end up with the first instance of adding 30 days which is outside the iteration itself. This means that my values inside the iteration are not being stored properly but I can't see why.
I've checked the DateTime operations and displayed the value of newdate and it shows the proper date so it's most likely the storing of the date. But I don't know what's going wrong, it works pre-iteration which is what's got me confused. Why isn't it executing inside the iteration? Does anyone have any idea?
Ex.
InitialDate | 3/29/2015
2ndDate | 4/28/2015<-- This is what's stored which is pre-iteration
3rdDate | 5/28/2015<-- This is what it's supposed to be after the iteration
so on and so forth....
Values pre-iteration
//Date stuff
String startdate = (String.valueOf(Acc._date));
DateTimeFormatter formatter = DateTimeFormat.forPattern("MM-dd-yyyy HH:mm:ss");
DateTime dt = formatter.parseDateTime(startdate);
DateTime startpoint = new DateTime(dt);
DateTime whendue = startpoint.plusDays(30);
DateTime foriteration = whendue;
String formattedDate = whendue.toString(formatter);
//Storing initial date
pay.setDateDue(formattedDate);
db.AddPayment(pay);
Actual iteration
while (i < j) {
//Operation for Date Calculation
DateTime ndt = foriteration.plusDays(30);
foriteration = ndt;
String newdate = ndt.toString(formatter);
//Adding values to PayTable
pay.setDateDue(newdate);
db.AddPayment(pay);
i++;
}
Finally found out what was wrong. Nothing. My roommate played a prank on me and just got back from his trip out of town and explained to me how he changed my getDateDue to execute a plusDays(30) to mimic my code so that when I called AddPayment which calls getDateDue it would look like it would work but in actuality would only add 30 days once to the startdate no matter what I did.
Summary
Roommate is an ass, nothing is wrong with my code. Sorry for this pointless post.

Update Only Once Per Day

I have some things in my Android application that need to update once per day.
It's pretty simple I think, but I have no idea in what format I need to format the date and time (if time is needed) and how to check if an update has been done today (where today is between 00:01am and 23:59pm in the user's local time). The update should not be done if it was already done for today.
Here's what I DO know how to do:
Save the last update date in SharedPreferences (but how do I get a
string of it, I do not know)
Get things from SharedPreferences (but I
don't know how to compare dates in string format)
It is irrelevant what format you choose. It is just matter of recalculations.
I'd suggest using milliseconds since epoch, as all system calls use it, so it would be easier for you to use the same.
As 1000 millis is 1 second it's easy to figure out that 1000*60*60*24 equals to one day (24hrs). So, if storedMillis is bigger than NOW - 1000*60*60*24, (and NOW is i.e. System.currentTimeMillis()), then it is too early to do the check. If storedMillis is smaller, then save your NOW timestamp and do the check:
long now = System.currentTimeMillis();
long diffMillis = now - lastCheckedMillis;
if( diffMillis >= (3600000 * 24) ) {
// store now (i.e. in shared prefs)
// do the check
} else {
// too early
}
EDIT
I am interested in doing it when the app is first opened for the
current day, even if the last update was done 10 minutes ago.
It's just the matter how to get the proper millis to compare against. Replace long now = System.currentTimeMillis(); from above code with following code block:
Calendar cal = Calendar.getInstance();
cal.clear(Calendar.HOUR);
cal.clear(Calendar.HOUR_OF_DAY);
cal.clear(Calendar.MINUTE);
cal.clear(Calendar.SECOND);
cal.clear(Calendar.MILLISECOND);
long now = cal.getTimeInMillis();
which shall do the trick.
If you store your date in format 20121122 (YYYYmmdd) then you can compare is like 20121122 > 20121123. But it must be stored as int or cast to int when comparing.
Store the timestamp (System.currentTimeMillis() ) of the Last execution and compair it with the currient one. If the difference is more than 24 hours... You know it or?
Set up an Alarm with AlarmManager that executes every 24 hours, then do stuff
Check this question: Alarm Manager Example
It's a more complicated approach than the rest, but makes sure things are done, while with the other options the app must be executed in order to check if it has to update whatever.
Here is the method
public boolean runOnceADay() {
SharedPreferences shp= c.getSharedPreferences(Constants.GENERAL_SHP, MODE_PRIVATE);
SharedPreferences.Editor editor= shp.edit();
long lastCheckedMillis = shp.getLong(Constants.ONCE_A_DAY, 0); // "KEY" you may change yhe value
long now = System.currentTimeMillis();
long diffMillis = now - lastCheckedMillis;
if (diffMillis >= (3600000 * 24)) { // set up your time circulation
editor.putLong(Constants.ONCE_A_DAY, now);
editor.commit();
Util.showMessage(c, "Once a Day Test");
return false;
} else {
Util.showMessage(c, "Too Early");
return true;
}
}

check current time between time interval android

I want to create an app that will allow the user to check whether or not the current time falls between a specified time interval. More specifically, I created a sql table using sqllite program, with the table specifying an end time and a start time for each record. The problem is that the type of data each field can be is limited to text, number, and other type other than a datetime type. So, how would I be able to check if the current time is between the start and end time since the format of time is in h:mm and not just an integer value that I could just do less than or greater than? Do I have to convert the current time to minutes?
You should be able to do the comparison even if time is not stored in the datetime type, here is a link that explains the conversion from string to time.
If that doesn't work, convert the time to seconds (int) and calculate the difference.
Try this. You can save and retrieve your time as String:
String to long: String.valueOf()
long to String: Long.valueOf()
Then, you use this procedure to check time:
//Time checker
private boolean timeCheck(long startTimeInput, long endTimeInput) {
long currentTime = System.currentTimeMillis();
if ((currentTime > startTimeInput) && (currentTime < endTimeInput)) {
return true;
} else {
return false;
}
}
And in your main program check it like this:
//You kept the times as String
if (timeCheck(Long.valueOf(start), Long.valueOf(end))) {
//it is in the interval
} else {
//not in the interval
}
I hope it helps.

Cannot get Correct month for a call from call log history

I am trying to extract information from the call log of the
android. I am getting the call date that is one month back from the
actual time of call. I mean to say that the information extracted by
my code for the date of call is one mont back than the actual call
date.
I have the following in the Emulator:
I saved a contact. Then I made a call to the contact.
Code:
I have 3 ways of extracting call Date information but getting the same
wrong result. My code is as follows:
/* Make the query to call log content */
Cursor callLogResult = context.getContentResolver().query(
CallLog.Calls.CONTENT_URI, null, null, null, null);
int columnIndex = callLogResult.getColumnIndex(Calls.DATE);
Long timeInResult = callLogResult.getLong(columnIndex);
/* Method 1 to change the milliseconds obtained to the readable date formate */
Time time = new Time();
time.toMillis(true);
time.set(timeInResult);
String callDate= time.monthDay+"-"+time.month+"-"+time.year;
/* Method 2 for extracting the date from tha value read from the column */
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(time);
String Month = calendar.get(Calendar.MONTH) ;
/* Method 3 for extracting date from the result obtained */
Date date = new Date(timeInResult);
String mont = date.getMonth()
While using the Calendar method , I also tried to set the DayLight
SAving Offset but it didnot worked,
calendar.setTimeZone(TimeZone.getTimeZone("Europe/Paris"));
int DST_OFFSET = calendar.get( Calendar.DST_OFFSET ); // DST_OFFSET
Boolean isSet = calendar.getTimeZone().useDaylightTime();
if(isSet)
calendar.set(Calendar.DST_OFFSET , 0);
int reCheck = calendar.get(Calendar.DST_OFFSET );
But the value is not set to 0 in recheck. I am getting the wrong
month value by using this also.
Please some one help me where I am wrong? or is this the error in
emulator ??
Thanks,
Nishant Kumar
Engineering Student
Calandar's months are from 0 to 11
You need to add 1 to the month Caladar is giving you.
I know, this is strange.
Try new Date(timeInResult);

Categories

Resources