Reputation: 321
I'm using TimeDurationPicker for a dialog in an Android app I'm writing. I want the user to enter a duration for a timer, and have that duration passed back to the activity which calls it. I know there are already answered questions on SO regarding this issue, but I haven't been able to get anything to work.
Here's the activity:
public class train extends AppCompatActivity {
public Integer customTimerlength = null;
public Integer timerDurationSeconds = 180; // 3 minutes is a good default value
public boolean timerIsPaused;
public long millisLeftOnTimer;
Button startBreakTimerButton;
Button stopBreakTimerButton;
Button pauseBreakTimerButton;
TextView breakTimerOutput;
CountDownTimer countdowntimer;
private CountDownTimer mCountDownTimer;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_train);
startBreakTimerButton = (Button) findViewById(R.id.startBreakTimer);
stopBreakTimerButton = (Button) findViewById(R.id.stopBreakButton);
pauseBreakTimerButton = (Button) findViewById(R.id.pauseBreakButton);
breakTimerOutput = (TextView) findViewById(R.id.breakTimerOutput);
// Break timer long-click set time
breakTimerOutput.setOnLongClickListener(new OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
//customTimerlength = timerLengthInputAlert();
new RestDurationPicker().show(getFragmentManager(), "Session break length");
and here is the fragment:
import android.widget.Toast;
import mobi.upod.timedurationpicker.TimeDurationPicker;
import mobi.upod.timedurationpicker.TimeDurationPickerDialogFragment;
public class RestDurationPicker extends TimeDurationPickerDialogFragment {
@Override
protected long getInitialDuration() {
return 0; // Default to empty
}
@Override
protected int setTimeUnits() {
return TimeDurationPicker.MM_SS;
}
@Override
public void onDurationSet(TimeDurationPicker view, long duration) {
Toast.makeText(getContext(), "New break duration set", Toast.LENGTH_LONG).show();
}
}
I've found quite a few answers here on SO mentioning intents and interfaces, but I haven't been able to get anything to work and I'm at a loss. This is my first attempt at an Android app so I'm not sure what else to do.
I really appreciate your help in advance!
Upvotes: 1
Views: 245
Reputation: 321
Figured it out!
I added this to my activity:
// Break timer long-click set time
@Override
public void onDurationSet(long duration) {
Integer i = (int) (long) duration; // get integer i from duration (long)
customTimerlength = i / 1000; // convert millis to seconds
// Set the timer duration in seconds
timerDurationSeconds = customTimerlength;
// Assign the new custom timer duration to the timerduration variable
breakTimerOutput.setText(Integer.toString(timerDurationSeconds));
Log.d("NewTimer", "New Timer Duration: " + timerDurationSeconds);
}
public interface DurationListener {
void onDurationSet(long duration);
}
The fragment now passes the duration to the activity as desired.
Upvotes: 1
Reputation: 403
In order for your Activity to receive the duration from your DialogFragment, you need to create an interface. Here is an example
public interface DurationListener {
void onDurationSet(long duration);
}
Your activity should then implement this interface. In other words, the activity will have to follow the terms of the interface contract by implementing the onDurationSet() method.
public class TrainActivity extends AppCompatActivity implements DurationListener {
//skipping most of your code
@Override
public void onDurationSet(long duration) {
//Do something with the duration
}
}
Now in your DialogFragment, you need to change your constructor to accept a DurationListener and you need to call onDurationSet() on the listener when the duration is changed by the user.
public class RestDurationPicker extends TimeDurationPickerDialogFragment {
private final DurationListener mListener;
public RestDurationPicker() {}
//The modified constructor, which accepts a listener as a parameter
public RestDurationPicker(DurationListener listener) {
mListener = listener;
}
//Most of your code goes here
@Override
public void onDurationSet(TimeDurationPicker view, long duration) {
//When the duration is set by the user, notify the listener
listener.onDurationSet(duration);
}
}
Now when you create the DialogFragment, just pass the Activity to the Fragment and you are done!
//This code is in your onCreate method in your activity
breakTimerOutput.setOnLongClickListener(new OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
//customTimerlength = timerLengthInputAlert();
new RestDurationPicker(this).show(getFragmentManager(), "Session break length");
}
});
SIDENOTE: You should make sure your code follows the Java and Android coding guidelines, namely by using the correct naming conventions for classes and methods and by keeping class fields private or protected unless you have a reason to make them public. This link can help you with this: https://source.android.com/source/code-style.html
Upvotes: 0