I just started learning Android Studio and this is my first project(Hello World Application). The project is to create an app where I have to return a greeting according to the time of day. The problem I am facing is that when I enter a name it shows "null (name) " instead of the appropriate greeting. I cannot understand why this is happening. I just need a hint or a nudge in the right direction.
Here is the result on the emulator: (https://prnt.sc/g5lc7e)
Here is my code for button pressed event:
@Override
public void onClick(View v) {
// get a reference to the TextView on the UI
TextView textMessage = (TextView) findViewById(R.id.textMessage);
//get a reference to the EditText so that we can read in the value typed
// by the user
EditText editFriendName = (EditText) findViewById(R.id.editFriendName);
// get the name of the friend typed in by the user in the EditText field
String friendName = editFriendName.getText().toString();
//Get the time of day
Date date = new Date();
Calendar cal = Calendar.getInstance();
cal.setTime(date);
int hour = cal.get(Calendar.HOUR_OF_DAY);
//Set greeting
String greeting = null;
if(hour>=6 && hour<12){
greeting = "Good Morning";
} else if(hour>= 12 && hour < 17){
greeting = "Good Afternoon";
} else if(hour >= 17 && hour < 21){
greeting = "Good Evening";
} else if(hour >= 21 && hour < 24){
greeting = "Good Night";
}
//Change string displayed by TextView
switch (v.getId()) {
case R.id.greetButton:
//set the string being displayed by the TextView to the greeting
//message for the friend
textMessage.setText( greeting + " " + friendName + "!");
break;
default:
break;
}
}