Need some help here...How can I create a Button that will visible when the time in my device is 6:00am-8:59am...and when the time is 9:00am the button will gone. any help please, I'm just newBie learning new things in android. Thank you!
Asked
Active
Viewed 85 times
0
-
Creating service won't be good. Since button will only be visible when you are inside some activity. How about checking the logic there itself? So as soon activity is destroyed. the code to do the same. – rohitanand Mar 31 '17 at 05:08
-
@SouravGanguly I just want to do is... when the time is 6am to 8:59am the button is visible...and when the time is 9:00am the button will Gone. – koroku Mar 31 '17 at 05:14
2 Answers
1
You can use java.util.Calendar
Calendar SIX_AM = Calendar.getInstance();
SIX_AM.set(Calendar.HOUR_OF_DAY,6);
SIX_AM.set(Calendar.MINUTE,0);
SIX_AM.set(Calendar.SECOND,0);
Calendar NINE_AM = Calendar.getInstance();
NINE_AM.set(Calendar.HOUR_OF_DAY,9);
NINE_AM.set(Calendar.MINUTE,0);
NINE_AM.set(Calendar.SECOND,0);
Calendar SIX_PM = Calendar.getInstance();
SIX_PM.set(Calendar.HOUR_OF_DAY,18);
SIX_PM.set(Calendar.MINUTE,0);
SIX_PM.set(Calendar.SECOND,0);
Calendar NINE_PM = Calendar.getInstance();
NINE_PM.set(Calendar.HOUR_OF_DAY,21);
NINE_PM.set(Calendar.MINUTE,0);
NINE_PM.set(Calendar.SECOND,0);
Calendar current = Calendar.getInstance();
if(current.compareTo(SIX_AM)>=0 && current.compareTo(NINE_AM)<0) {
btnAm.setVisibility(View.VISIBLE);
} else {
btnAm.setVisibility(View.GONE);
}
if(current.compareTo(SIX_PM)>=0 && current.compareTo(NINE_PM)<0) {
btnPm.setVisibility(View.VISIBLE);
} else {
btnPm.setVisibility(View.GONE);
}
Hope that helps

AnswerBot
- 447
- 2
- 7
-
hi where did I put that code? I just created this a while ago.. if it's wrong please correct me...Thank you btnAm = findViewById(R.id.btn_am); btnPm = findViewById(R.id.btn_pm); btnAm.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { if (no code yet...){ } } }); – koroku Mar 31 '17 at 04:07
-
I would recommend putting it in onResume method of your activity. Also I haven't tried the code myself , so do let me know it it is working. Also put else condition where visibility is set to false. – AnswerBot Mar 31 '17 at 04:17
-
-
@AnswerBot I found the error...not this code [Calendar.HOUR_OF_DAY] because it's give 24hour in time. I change it in [Calendar.HOUR] so that I can get the 12Hour in time – koroku Mar 31 '17 at 05:47
-
@AnswerBot I just want to do that the button will show only if the time is 6to9am .. how can I do that? please help. – koroku Mar 31 '17 at 06:08
-
@koroku I think Calendar.HOUR_OF_DAY is correct as by it you can control AM and PM time by setting value less than or greater than 12 respectively. I this case as it is AM so they are 6 and 9 (less than 12). Also I modified the code . Please check. – AnswerBot Mar 31 '17 at 06:28
-
@AnswerBot how can I change the code for the "PM"?? to be clear: your code is working well in 6to9am..but I want to add 6to9pm too... but it didn't work.... I replace the "SIX_AM" to "SIX_PM" same in NINE. but it didn't work. – koroku Mar 31 '17 at 06:36
-
For changing AM to PM. Add 12 in hour value. Please check modified code it will work for both 6AM to 9AM and 6PM to 9PM – AnswerBot Mar 31 '17 at 06:43
-
@AnswerBot I have 2button which is btnAm and btnPm for btnAm the code is worked, but the btnPm is always VISIBLE. what is the problem? – koroku Mar 31 '17 at 06:59
-
@AnswerBot that's the same in my code "edited" from yours. but it didn't work also... – koroku Mar 31 '17 at 07:12
-
@koroku are you sure you didn't make any mistakes while typing as the above code is working fine in the sample application I made. (Please make sure you are using SIX_PM is second if condition) – AnswerBot Mar 31 '17 at 07:28
-
@AnswerBot yes I copy your given code to be clear...but it didn't work to my activity...but the btnAm is working... when I set my time let say in 8:49pm it will show the btnAM not the btnPm... – koroku Mar 31 '17 at 07:34
-
@koroku I don't know what else to say as the same code is working in the sample app. Please debug your code. Also check whether you have labeled the buttons correctly. – AnswerBot Mar 31 '17 at 07:44
-
@AnswerBot Thanks for the help ... thumbs up for you! if you don't mind can I know your email so that we can hangout :D – koroku Mar 31 '17 at 07:51
-
@AnswerBot maybe you have idea to add a if condition here to know if the downloaded file is not empty. http://stackoverflow.com/questions/43200028/successfully-downloaded-the-files-but-no-content-inside-the-folder – koroku Apr 04 '17 at 06:56
0
In your case AlarmManager will work. set two alarm
- at 6:00 a.m. (Handle by logic it is time to show button)
- at 8:59 a.m. (Handle by logic it is time to hide button)
How to start Service using Alarm Manager in Android?
for you i will suggest to use setExact instead of setRepeating. Here is the code...
AlarmManager am = (AlarmManager)
context.getSystemService(Context.ALARM_SERVICE);
int ALARM_TYPE = AlarmManager.RTC_WAKEUP;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)
am.setExact(ALARM_TYPE, calendar.getTimeInMillis(), pendingIntent);
else
am.set(ALARM_TYPE, calendar.getTimeInMillis(), pendingIntent);
Remember setExact does not provide repeating feature so every time you have to set it from your service again...
-
Great! But sad to say I don't know where do I put that code. can you please modify it, my button ID for am is btn_am, and btn_pm please use this so that I can easily review. – koroku Mar 31 '17 at 04:09