Reputation: 6703
I have a tabbed activity which contains two bottom tabs. The FrameLayout is filled with special layout file that I have created. This two layout files are run by java classes that are extending Fragment class. This is the brief description of what I have now.
Now comes the details about a problem. The layout file contains two EditText fields which must create a DatePickerDialog when clicked. I have implemented the dialogs using the DialogFragment class, one for both text fields. Hovewer, when I click the edittexts they produce several DatePicker dialogs, instead of one, and it is not clear what is the logic behind. It may produce 2, 3 or even 4 dialog windows.
How can I fix this problem? Here is my code:
Activity
public class Activity extends AppCompatActivity {
FragmentTabHost mFragmentTabHost;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_quick_calculation);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
mFragmentTabHost = (FragmentTabHost) findViewById(android.R.id.tabhost);
mFragmentTabHost.setup(QuickCalculationActivity.this, getSupportFragmentManager(), android.R.id.tabcontent);
mFragmentTabHost.addTab(mFragmentTabHost.newTabSpec("days").setIndicator("DAYS"), DaysTabView.class, null);
mFragmentTabHost.addTab(mFragmentTabHost.newTabSpec("date").setIndicator("DATE"), DateTabView.class, null);
}
}
One of the tabView classes
public class DaysTabView extends Fragment {
/*
* some code was not included to make easir to read
*/
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.days_tab_view, container, false);
mFromDate = (EditText) v.findViewById(R.id.fromDate);
mFromDate.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
mFromDate.setText("");
SetDateToDefault(1); //some method
Bundle args = new Bundle();
args.putInt("day", day1);
args.putInt("month", month1);
args.putInt("year", year1);
myDatePickerDialogFragment d1 = new myDatePickerDialogFragment();
d1.setArguments(args);
d1.setOnDateSetListener(datePickerListener1); //onDateSet Listener that is not included in this question
d1.show(getFragmentManager(), "days");
return false;
}
});
}
}
And DialogFragment class
public class myDatePickerDialogFragment extends DialogFragment{
int mDay, mMonth, mYear;
OnDateSetListener onSetDate;
public myDatePickerDialogFragment(){
}
@Override
public void setArguments(Bundle args) {
super.setArguments(args);
mDay = args.getInt("day");
mMonth = args.getInt("month");
mYear = args.getInt("year");
}
public void setOnDateSetListener(OnDateSetListener setDate){
onSetDate = setDate;
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
return new DatePickerDialog(getActivity(), onSetDate, mYear, mMonth, mDay);
}
}
Upvotes: 0
Views: 67
Reputation: 3195
Change your onTouchListener
code
mFromDate.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
final int actionPeformed = ev.getAction();
switch(actionPeformed){
case MotionEvent.ACTION_UP:{
mFromDate.setText("");
SetDateToDefault(1); //some method
Bundle args = new Bundle();
args.putInt("day", day1);
args.putInt("month", month1);
args.putInt("year", year1);
myDatePickerDialogFragment d1 = new myDatePickerDialogFragment();
d1.setArguments(args);
d1.setOnDateSetListener(datePickerListener1); //onDateSet Listener that is not included in this question
d1.show(getFragmentManager(), "days");
break;
}
return true;
}
});
Upvotes: 1
Reputation: 5287
I would recommend changing your onTouchListener
with onClickListener
on your EditTexts. The reason behind is that the touch listener will be triggered each time an event occured (such as finger down, finger move, finger up etc), whereas a click listener will only get triggered once.
So in the DaysTabView
class, change to this :
mFromDate.setOnClickListener(new View.OnClickListener() {
@Override
public boolean onClick(View v) {
mFromDate.setText("");
SetDateToDefault(1); //some method
Bundle args = new Bundle();
args.putInt("day", day1);
args.putInt("month", month1);
args.putInt("year", year1);
myDatePickerDialogFragment d1 = new myDatePickerDialogFragment();
d1.setArguments(args);
d1.setOnDateSetListener(datePickerListener1); //onDateSet Listener that is not included in this question
d1.show(getFragmentManager(), "days");
return false;
}
});
Upvotes: 1
Reputation: 2739
return true in setOnTouchListener instead of false.
if(event.getAction() == MotionEvent.ACTION_UP){
// Do what you want
return true;
}
return false;
}
It should be like:
mFromDate.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction() == MotionEvent.ACTION_UP){
mFromDate.setText("");
SetDateToDefault(1); //some method
Bundle args = new Bundle();
args.putInt("day", day1);
args.putInt("month", month1);
args.putInt("year", year1);
myDatePickerDialogFragment d1 = new myDatePickerDialogFragment();
d1.setArguments(args);
d1.setOnDateSetListener(datePickerListener1); //onDateSet Listener that is not included in this question
d1.show(getFragmentManager(), "days");
return true;
}
return false;
}
});
Upvotes: 1