I'm using two timepickers in the same activity to let the user choose a start time and a stop time for a specific task, I'm using the DialogFragment class that's available in the support library for backward compatibility with older Android versions.
I set the timepickers and they're showing up correctly, but I can't figure out how to get the values the user inputs for both of these timepickers, I don't have any background or experience with implementing dialogs so your guidance with code example is highly appreciated!
Here is my code to create the TimePickerFragment class, created in a seperate file, taken and edited slightly 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.app.TimePickerDialog.OnTimeSetListener;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
public class TimePickerFragment extends DialogFragment {
@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);
if (!(getActivity() instanceof OnTimeSetListener)) throw new IllegalStateException("Activity should implement OnTimeSetListener!");
OnTimeSetListener timeSetListener = (OnTimeSetListener) getActivity();
// Create a new instance of TimePickerDialog and return it
return new TimePickerDialog(getActivity(), timeSetListener, hour, minute, false);
}
}
Inside My MainActivity:
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 implements TimePickerDialog.OnTimeSetListener {
public void InputStartTime(View v) {
DialogFragment newFragment = new TimePickerFragment();
newFragment.show(getSupportFragmentManager(), "timePicker");
}
public void InputEndTime(View v) {
DialogFragment newFragment = new TimePickerFragment();
newFragment.show(getSupportFragmentManager(), "timePicker");
}
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;
}
}
Any ideas on how to implement two timepickers and differentiate between their inputs by adding something to the code above?