I created one screen in my Android app which contains a datepicker like so:
TextView userDateView = (TextView) getView().findViewById(R.id.text_user_date);
userDateView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
DatePickerFragment newFragment = new DatePickerFragment();
newFragment.setListner(OverviewFragment.this);
newFragment.show(getFragmentManager(), "datePicker");
}
});
and I created a callback which receives the date back and updates the userDateView, which all works fine. In another screen I now want to have two dates next to eachother which the user can both select. Unfortunately the callback doesn't know from which of the two dates the user started the datepicker fragment.
Does anybody know how I can somehow know from which button the datepickerFragment was started? All tips are welcome!
A simple approach is to supply a parameter to each DatePickerFragment and send this parameter back to your callback, like requestCode on Activity.onActivityResult.
See how to supply parameters to Fragments:
http://developer.android.com/reference/android/app/Fragment.html#setArguments(android.os.Bundle)
Btw, you can find a different approach here:
Multiple DatePickers in same activity
Try something like this:
DatePickerFragment newFragment = new DatePickerFragment();
newFragment.setListner(new DatePickerDialog.OnDateSetListener() {
#Override
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {}
});
newFragment.show(getFragmentManager(), "datePicker");
Related
Currently I start testing not using interface for passing concrete listener from activity to fragments.
this code works:
public Dialog onCreateDialog(Bundle savedInstanceState) {
//......Skip
return new DatePickerDialog(getActivity(), (DatePickerDialog.OnDateSetListener)getActivity(), year, month, day);
}
But when I do it same between fragments, it seem doesn't work.
public Dialog onCreateDialog(Bundle savedInstanceState) {
//...Skip
return new TimePickerDialog(getActivity(), ((TimePickerDialog.OnTimeSetListener)((newFragment)getParentFragment())) ,hour, minute,false);
}
I tested with log and the listener from parent fragment seem doesn't called.
I have also tried with getContext.
Am I passing wrong listener reference? thanks.
Try this : your fragment must implements TimePickerDialog.OnTimeSetListener , DatePickerDialog.OnDateSetListener
DatePickerDialog datePickerDialog = DatePickerDialog.newInstance(
FragmentName.this, //pass fragment name
Year,
Month,
Day
);
Try with
public Dialog onCreateDialog(Bundle savedInstanceState) {
//...Skip
return new TimePickerDialog(getActivity(), ((TimePickerDialog.OnTimeSetListener)getParentFragment()) ,hour, minute,false);
}
Make sure parent fragment has implemented TimpePickerDialog.OnTimeSetListener
And yout listener object in TimePickerDialog is initialized with the ParentFragment instance you are using.
When the user selects a date from a DatePicker dialog in a fragment the dialog and fragment are then dismissed. I want to be able to notify the originating activity so it can then run a toast. I'd rather not add code to the fragment because it is already being used in many other places in the project.
Problem with current code is the toast runs as soon as the fragment dialog is created (the DatePicker). Any ideas on how to fix?
Activity file:
...
DatePickerFragment newFragment = new DatePickerFragment();
newFragment.show(getSupportFragmentManager(), "datePicker");
Toast saveToast = Toast.makeText(this, "Now Click the Save Button", Toast.LENGTH_LONG);
saveToast.show();
In your fragment instance add code like this...
DatePickerFragment newFragment = new DatePickerFragment();
newFragment.setCallBack(ondate);
newFragment.show(getSupportFragmentManager(), "datePicker");
and Initialize onDate as this
DatePickerDialog.OnDateSetListener ondate = new DatePickerDialog.OnDateSetListener()
{
#Override
public void onDateSet(DatePicker dp, int selectedYear,int selectedMonth, int selectedDay)
{
Calendar c = Calendar.getInstance();
c.set(selectedYear, selectedMonth, selectedDay);
String strDate = formatter.format(new Date(c.getTimeInMillis()));
// here you can add toast or any thing
}
};
When an EditText line in the UI gains focus, a DatePickerFragment launches for the user to enter a date. On orientation change, if the EditText line has focus and a previously entered date (length() > 0) and is launching a DatePickerFragment due to the below code. I don't want the DatePickerFragment to launch though after an orientation change. Is there a way to modify or add code so that if the Activity is newly created and the EditText line has focus it won't automatically launch the DatePickerFragment ? Would it be a good idea to do something in onResume()? Or do something with !=null?
ListenerEditText.setOnFocusChangeListener(new OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
...
if (hasFocus && (fListenerEditText.getText().length() == 0)) {
DatePickerFragment newFragment = new DatePickerFragment();
newFragment.show(getSupportFragmentManager(), "datePicker");
}
else if (hasFocus) {
...
DatePickerFragment newFragment = new DatePickerFragment();
newFragment.show(getSupportFragmentManager(), "datePicker");
}
}
});
If you have requestFocus() on your Edittext remove it also go for something like this; move the below line outside the interface
DatePickerFragment newFragment = new DatePickerFragment();
and inside the interface go for a boolean attack
if(newFragment.isShow())// true if shown false if not
and your if statment checks out this way
if & only if EditText has focus and the length is 0 show dialog, or if it has focus show dialog,there is no greater than 0 here
Hope i helped
Before adding a new DatePickerFragment check to see if one already exists. If it doesn't exist then go ahead and add the DatePickerFragment. I'm assuming that "datePicker" is the tag that is used for the fragment transaction. You can check for the fragment by using
if (getSupportFragmentManager().findFragmentByTag("datePicker")==null) {
//Add new DatePickerFragment
}
Let me know if that helps out.
I have created an app, in which I have a DatePicker and a TimePicker. First I made the Datepicker and I displayed it when I selected the Date textview, this worked perfectly fine. Then I wanted to do the same for the Time EditText but instead of displaying a DatePicker it is supposed to display a TimePicker when selected.
After I implemented the code for the TimePicker: The error is that the "protected Dialog onCreateDialog(int id)" is already defined and I cannot use this method twice.
Any help would be appreciated,
Thanks in Advance
I assume that you have only one DialogFragment for both date and time pickers... this will create a conflict as you have already implemented the onCreateDialog.
what I usually do is I create another class for each component that i would like to have a different implementation of onCreateDialog.
public class DatePickerDialog extends DialogFragment implements DatePickerDialog.OnDateSetListener{
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
...
}
public class TimePickerDialog extends DialogFragment implements
TimePickerDialog.OnTimeSetListener{
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
...
}
then I would call it in the activity like ...
DatePickerDialog dialog = new DatePickerDialog();
FragmentManager fm = getSupportFragmentManager();
dialog.show(fm, "Date Picker");
this way you could also add custom callbacks or listeners for each picker and use them multiple times in one activity.
I have a Fragment that has its own layout and has a Choose Time button along with a TextView beneath it. I am opening a TimePickerFragment from this Fragment when the user clicks this button.
Now when the user selects a time, and clicks done, I want to populate the TextView contained in the fragment. I am not sure how to do this.
All the materials/tutorials online invoke the timepicker from an Activity. I do (obviously) have a Parent Activity, but since this TimePicker is only being used in this Fragment, I would like to limit the scope as much as possible.
So the flow is as follows:
ParentActivity > Fragment (contains TextView and button) > DialogFragment (TimePicker)
Here is my implementation:
public class TimePickerFragment extends DialogFragment implements TimePickerDialog.OnTimeSetListener {
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
int hour = 0;
int minute = 0;
boolean is24Hour = false;
// ... Some code to read shared preference, get the correct time format, theme, etc.
return new TimePickerDialog(getActivity(), dialog_theme, this, hour, minute, is24Hour); //DateFormat.is24HourFormat(getActivity())
}
#Override
public void onTimeSet(TimePicker view, int hour, int minute) {
//TextView textView = (TextView) getView.findViewById(R.id.time_value); //NullPointerException!
TextView textView = (TextView) view.findViewById(R.id.time_value); //Also NullPointerException!
// Do something with the time chosen by the user
textView.setText(TimeAndDate.getDisplayableTime(time));
textView.clearFocus();
}
}
It belongs to the Fragment which invokes the DialogFragment
findViewById looks for a view with the id in the current view hierarchy. The TextView is not a child of TimerView.
So you need to initialize TextView in Fragment not DialogFragment.
All Fragment-to-Fragment communication is done through the associated Activity. Two Fragments should never communicate directly. Use interface as a call back to the Activity and then communciate the time to the Fragment.
http://developer.android.com/training/basics/fragments/communicating.html
DialogFragment --> ParentActivtiy --> Fragment
You can check the example #
Simple timepicker for a fragment activity