I'm using a time picker to let the user enter his desired time to do a specific task, I'm using the DialogFragment class that's available in the support library for backward compatibility with older Android versions.
Here is my code to create the TimePickerFragment class, created in a seperate file, taken from: http://developer.android.com/guide/topics/ui/controls/pickers.html :
package com.calls.only;
import java.util.Calendar;
import android.app.Dialog;
import android.app.TimePickerDialog;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.widget.TimePicker;
public class TimePickerFragment extends DialogFragment
implements TimePickerDialog.OnTimeSetListener {
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Use the current time as the default values for the picker
final Calendar c = Calendar.getInstance();
int hour = c.get(Calendar.HOUR_OF_DAY);
int minute = c.get(Calendar.MINUTE);
// Create a new instance of TimePickerDialog and return it
return new TimePickerDialog(getActivity(), this, hour, minute, false);
}
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
// Do something with the time chosen by the user
}
}
Main Activity:
package com.calls.only;
import java.util.Calendar;
import java.util.TimeZone;
import android.os.Bundle;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.DialogFragment;
import android.view.Menu;
import android.view.View;
import android.widget.RadioButton;
import android.widget.TextView;
public class MainActivity extends FragmentActivity {
public void InputStartTime(View v) {
DialogFragment newFragment = new TimePickerFragment();
newFragment.show(getSupportFragmentManager(), "timePicker");
}
private TimePickerDialog.OnTimeSetListener mTimeSetListener =
new TimePickerDialog.OnTimeSetListener() {
//Overriding onTimeSet causes an error, see below
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
Log.i("TimePicker", "Time picker set!");
}
};
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
the onTimeset method is not being called as I can see from the log, if I try to override this method, I get the error: "The method onTimeSet(TimePicker, int, int) of type new TimePickerDialog.OnTimeSetListener(){} must override a superclass method"
can anyone tell me what the problem is? I've been trying to figure it out and it left me with too much frustration!