I want to checked if set time is less than calender(mobile device)time. It shows alarms set and if set time is not more that 1.5 hr it shows "ruko ruko" message. If both conditions are false then it will go to else condition. It does not gave me expected results. Here is my code:
GregorianCalendar mcalender=new GregorianCalendar();
mcalender.set(2013, 7, 17, 13, 0);
if(mcalender.getTimeInMillis()<=cal.getTimeInMillis()){
Toast.makeText(getBaseContext(), "sets ", Toast.LENGTH_SHORT).show();
}
else if(mcalender.getTimeInMillis()+5400000<=cal.getTimeInMillis()){
Toast.makeText(mContext, "ruko ruko", Toast.LENGTH_SHORT).show();
}
else{
Toast.makeText(getBaseContext(), "Time not gone ", Toast.LENGTH_SHORT).show();
}
The problem is that mcalender.getTimeInMillis() is checked first. For example, when you alarm is set for 5:30, at 4:30 the first part will be true already, so the else if section is entirely skipped. Just got to reverse the order and that will do :)
if(mcalender.getTimeInMillis()+5400000<=cal.getTimeInMillis()){
//If still have more than 1.5 hours to go (i.e. 2 hours left etc)
Toast.makeText(mContext, "ruko ruko", Toast.LENGTH_SHORT).show();
}
else if(mcalender.getTimeInMillis()<=cal.getTimeInMillis()){
//Not more than 1.5 hours, but still not time up yet.
Toast.makeText(getBaseContext(), "sets ", Toast.LENGTH_SHORT).show();
}
else{
//Time is up. Run action.
Toast.makeText(getBaseContext(), "Time not gone ", Toast.LENGTH_SHORT).show();
}
Related
I want to display the value toast in text view
Code :
public void onAnimationEnd(Animation animation) {
#SuppressLint({"WrongConstant", "ShowToast"}) Toast toast = Toast.makeText(this, " " + String.valueOf((int)(((double)this.intNumber)
- Math.floor(((double)this.lngDegrees) / (360.0d / ((double)this.intNumber))))) + " ",0);
toast.setGravity(49,0,0);
this.blnButtonRotation = true;
b_start.setVisibility(View.VISIBLE);
}
i agree with Laura. It's hard to guess what you are asking for here.
If you want to show the Toast you should add .show() at the end of your toast and change the duration to Toast.LENGHT_SHORT or Toast.LENGTH_LONG instead of "0".
e.g:
Toast.makeText(this, "yourString", Toast.LENGTH_LONG).show()
If you want to access the the value of the String inside the Toast, there is currently no way to do something like that. So the only option is to store it in its own variable:
String numberAsString = String.valueOf((int(((double)this.intNumber) - Math.floor(((double)this.lngDegrees) / (360.0d / ((double)this.intNumber)))));
textView.setText(numberAsString);
//Toast toast = Toast.makeText(this, numberAsString, Toast.LENGHT_LONG).show();
I am trying to compare the current date with the date stored in the array, but I am not able to compare the date using if condition.
I use this code to compare the date:
DateFormat TO = new SimpleDateFormat("yyyy-MM-dd ");
Today = TO.format(Calendar.getInstance().getTime());
for (int i = 0; i < jsonArray.length(); i++) {
// Today has a value 2018-03-13
// ScheduleDates[i] has a value 2018-03-13
// ScheduleDates[10] has a value 2018-03-13
if (Today.equals(ScheduleDates[i])) {
Toast.makeText(Main2Activity.this, "Match Found" + ScheduleDates[i] + "--"
+ StartTimes[i] + "--" + Endtimes[i], Toast.LENGTH_LONG).show();
break;
} else {
Toast.makeText(Main2Activity.this, "Match Not Found", Toast.LENGTH_LONG).show();
break;
}
}
But when I run the loop it shows only "match not found". I cannot find what error I had made, can anyone help to find the error?
Use Log.d("Today",String.valueOf(Today)); to see what is in your Today variable. Also use
for(int i=0;i<jsonArray.length();i++){
Log.d("Array",String.valueOf(ScheduleDates[i]));
}
Try to verify with your own eyes using the logcat that the exact date that is stored in your Today variable also exists in your array. If not, there is your problem. Your if statement works just fine, there just isn't any date in the array that is the same with the one in Today.
Edit: It is not visible in your code but i am willing to acept that jsonArray and ScheduleDates have the same length and that the second derives from the first. Otherwise your for loop would crash.
Edit2: Your problem actually is your break statements . Your for loop only runs once, so if the first item of the array matches the date in Today it stops and prints your success message. If the first item of your array does not match the date in Today it again stops and prints the failure message which is also the case as you have said.
So, remove the break statements and then the for loop will run as many times as it should and print the appropriate message whether it is for success or failure.
If you just want to find the first match and then stop, remove the break statement in your else but keep the one in your if.
only remove extra space define in date format like below ...
DateFormat TO = new SimpleDateFormat("yyyy-MM-dd");
I am trying to make an app for students, and in the home screen of the app I would like to display a message like "Hey! Good Morning" and it should change to "Hey!Good evening" when it is after 15:00hrs of a day and so on.
How can I achieve this ?
Please check this one.It may help you.
Calendar c = Calendar.getInstance();
int timeOfDay = c.get(Calendar.HOUR_OF_DAY);
if(timeOfDay >= 0 && timeOfDay < 12){
Toast.makeText(this, "Good Morning", Toast.LENGTH_SHORT).show();
}else if(timeOfDay >= 12 && timeOfDay < 16){
Toast.makeText(this, "Good Afternoon", Toast.LENGTH_SHORT).show();
}else if(timeOfDay >= 16 && timeOfDay < 21){
Toast.makeText(this, "Good Evening", Toast.LENGTH_SHORT).show();
}else if(timeOfDay >= 21 && timeOfDay < 24){
Toast.makeText(this, "Good Night", Toast.LENGTH_SHORT).show();
}
you can get the time or hour by
import java.util.Calendar
Calendar c = Calendar.getInstance();
int hours = c.get(Calendar.HOUR);
from this you can choose a String for your textview:
TextView mytextview=(TextView) findViewByID(...);
String message="";
if(hours<11){
message="good morning"
}else if(hours>=11 & hours<17){
message=...}
mytextview.setText(message);
You can place a check for time of day in your activity in onResume(). This will check whenever the screen which has to display the text comes to foreground. So, even when you return to it from a different screen, the check will be made. For realtime check you will need a mechanism for absolute callback of the event [of 1500 hrs].
This might be a bit cumbersome and we need more info to decide on a mechanism. You should also decide if this is exactly what you want to do or not.
Not showing checking code because of its trivial nature.
I have made an calculation, took of the decimals but now i would like to make so that it show a down rounded number.
I found that i can use math.floor but i can't get it in the code.
I took the decimals off but can't get it to round down.
code is like this (the part which does the math):
public void onClick(View view) {
if (etBarfles.getText().toString().equals("")) {
Toast.makeText(getActivity(), "Vul zowel bar in fles, inhoud fles als liters per " +
"minuut in!", Toast.LENGTH_SHORT).show();
} else {
if (etInhoudfles.getText().toString().equals("")) {
Toast.makeText(getActivity(), "Vul zowel bar in fles, inhoud fles als liters per " +
"minuut in!", Toast.LENGTH_SHORT).show();
} else {
if (etLitersperminuut.getText().toString().equals("")) {
Toast.makeText(getActivity(), "Vul zowel bar in fles, inhoud fles als liters " +
"per minuut in!", Toast.LENGTH_SHORT).show();
} else {
}
tvResult.setText(nodecimals((((Float.parseFloat(etBarfles.getText().toString()) *
(Float.parseFloat(etInhoudfles.getText().toString())) / (Float.parseFloat
(etLitersperminuut.getText().toString()))) / 60))));
}
}
}
private String nodecimals(float val) {
return String.format("%.0f", val);
}
Try the following
tvResult.setText(String.valueOf((int) Math.floor(((Float.parseFloat(etBarfles.getText()
.toString()) *
(Float.parseFloat(etInhoudfles.getText().toString())) / (Float.parseFloat
(etLitersperminuut.getText().toString()))) / 60))));
(int) Math.floor() will convert the value to an integer floor value, and then String.valueOf() will convert that int to String, so that you can set the same to the TextView.
THINGS TO IMPROVE:
You can make your code better by following these guidelines:
You are using the same line etBarfles.getText().toString() in multiple places for your check conditions. Its best if you initialize a variable with that value. Something like
String etBarFilesText = etBarfles.getText().toString();
Your condition etBarfles.getText().toString().equals("") can be written the Android way as follows
!TextUtils.isEmpty(etBarFilesText)
Your strings should be defined in your strings.xml. So if your error msgd is named error_string in the strings.xml then your Toast message will look something like this.
Toast.makeText(getActivity(), getString(R.string.error_string), Toast.LENGTH_SHORT).show();
Your line
tvResult.setText(nodecimals((((Float.parseFloat(etBarfles.getText().toString()) *
(Float.parseFloat(etInhoudfles.getText().toString())) / (Float.parseFloat
(etLitersperminuut.getText().toString()))) / 60))));
could have been wayy simpler, and more readable if you used different variables, instead of writing everything in a big single sentence.
My app add an integer value to a vector, and then wants to know the size of that vector.
if(nearSelected||middleSelected||farSelected){
ArrayList<Integer> distance = new ArrayList<Integer>();
//Which distance(s) has the user selected?
if(nearSelected){distance.add(1);}
if(middleSelected){distance.add(2);}
if(farSelected){distance.add(3);}
//Attempt to display the number of choices picked to the user
try {
Toast.makeText(getBaseContext(), distance.size(), Toast.LENGTH_SHORT).show();
} catch (Exception e) {
Log.i(LOG_TAG, e.toString());
}
}
Unfortunately, attempting to get distance.size() causes a NotFoundException. All other parts of the code run fine, just this part crashes. Where have I screwed up?
Distance.size() is not what causes a Resources.NotFoundException, its Toast.makeText. When called with an integer as a parameter, it looks for a string resource with that integer as an id. If you want to show the number as a string, then you'll have to tell it so:
Toast.makeText(getBaseContext(), Integer.toString(distance.size()), Toast.LENGTH_SHORT).show();