Hello I m new to android development I m developing an app which requires to get the hour and minute from timePicker so I did like this to get it:
public class MainActivity extends Activity {
TimePicker timePicker;
int hour, changedHour;
int minute, changedMinute;
TextView tv, tv1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv = (TextView) findViewById(R.id.textView1);
tv1 = (TextView) findViewById(R.id.textView2);
timePicker = (TimePicker) findViewById(R.id.timePicker1);
setCurrentTime();
timePicker.setOnTimeChangedListener(new OnTimeChangedListener() {
@Override
public void onTimeChanged(TimePicker arg0, int hourOfDay,
int minuteOf) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "Time set!",
Toast.LENGTH_SHORT).show();
changedHour = hourOfDay;
changedMinute = minuteOf;
tv.setText("Hour of Day is " + changedHour + " and Minute is "
+ changedMinute);
}
});
tv1.setText("Hour of Day is " + changedHour + " and Minute is "
+ changedMinute);
}
public void setCurrentTime() {
// TODO Auto-generated method stub
final Calendar c = Calendar.getInstance();
hour = c.get(Calendar.HOUR_OF_DAY);
minute = c.get(Calendar.MINUTE);
timePicker.setCurrentHour(hour);
timePicker.setCurrentMinute(minute);
}`
The problem is here
changedHour = hourOfDay;
changedMinute = minuteOf;
tv.setText("Hour of Day is " + changedHour + " and Minute is "
+ changedMinute);
I got the changed time and I set it like hour would be in hourOfDay = changedHour
and same for minute now outside the method when I m trying to use the variables the problem is its set to 0 HERE
tv1.setText("Hour of Day is " + changedHour + " and Minute is "
+ changedMinute);
Please help me!
You never actually assign to the changedHour
or changedMinute
variables before you print them using tv1.setText
, since it's very likely that onTimeChanged
hasn't ever fired by that point. For this reason, they'll have their default values of 0, since they haven't been assigned anything else yet.
Anyway, you can't return values out of a void
function. By definition, void
is a lack of a type.
How to fix this depends on how exactly you want the code to work. The path of least resistance would be to set changedHour
and changedMinute
from within setCurrentTime
, e.g.
public void setCurrentTime() {
// TODO Auto-generated method stub
final Calendar c = Calendar.getInstance();
hour = c.get(Calendar.HOUR_OF_DAY);
minute = c.get(Calendar.MINUTE);
// NEW CODE
changedHour = hour;
changedMinute = minute;
// END NEW CODE
timePicker.setCurrentHour(hour);
timePicker.setCurrentMinute(minute);
}
You could return the hour and minute through some other mechanism (return a struct or use out parameters), but given that you update changedHour/Minute
in your onTImeChanged
callback, it probably wouldn't fit well with the existing flow.
On a re-read though, this could all just be because you're using the wrong variable in tv1.setText
. You assign to the class variables hour
and minute
in setCurrentTime
, so you could just print those rather than changedHour
and changedMinute
, e.g.
tv1.setText("Hour of Day is " + hour + " and Minute is "
+ minute);