Eizzuddin Eizzuddin -4 years ago 74
Android Question

TimePickerDialog popup on start of the apps

Im trying to make datepickerdialog and timepickerdialog to popup when clicked on textview. I have a problem where every time I started my apps the TimePickerDialog pop up itself eventhough I didn't even click on the text box yet. Any way I can fix this issue?

public class MainActivity extends AppCompatActivity implements OnClickListener{
EditText e_tarikh, e_masa;
String tarikh, masa;
private DBOperations db;
private EditText fromDateEtxt;
private EditText toTimeEtxt;
private int mHour, mMinute;

private DatePickerDialog fromDatePickerDialog;
private TimePickerDialog toTimePickerDialog;

private SimpleDateFormat dateFormatter;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

db = new DBOperations(this);
e_tarikh = (EditText) findViewById(R.id.tarikh);
e_masa = (EditText) findViewById(R.id.masa);


dateFormatter = new SimpleDateFormat("dd-MM-yyyy", Locale.US);
findViewsById();
setDateTimeField();

}
public void saveData(View view)
{

tarikh = e_tarikh.getText().toString();
masa = e_masa.getText().toString();

BackgroundTask backgroundTask = new BackgroundTask(this);
backgroundTask.execute("add_info", tarikh, masa);
finish();
}


private void findViewsById() {
fromDateEtxt = (EditText) findViewById(R.id.tarikh);
fromDateEtxt.setInputType(InputType.TYPE_NULL);
fromDateEtxt.requestFocus();

toTimeEtxt = (EditText) findViewById(R.id.masa);
toTimeEtxt.setInputType(InputType.TYPE_NULL);
}

private void setDateTimeField() {
fromDateEtxt.setOnClickListener(this);
toTimeEtxt.setOnClickListener(this);
final Calendar c = Calendar.getInstance();
mHour = c.get(Calendar.HOUR_OF_DAY);
mMinute = c.get(Calendar.MINUTE);

Calendar newCalendar = Calendar.getInstance();
fromDatePickerDialog = new DatePickerDialog(this, new OnDateSetListener() {

public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
Calendar newDate = Calendar.getInstance();
newDate.set(year, monthOfYear, dayOfMonth);
fromDateEtxt.setText(dateFormatter.format(newDate.getTime()));
}

},newCalendar.get(Calendar.YEAR), newCalendar.get(Calendar.MONTH), newCalendar.get(Calendar.DAY_OF_MONTH));

toTimePickerDialog = new TimePickerDialog(this,
new TimePickerDialog.OnTimeSetListener() {

@Override
public void onTimeSet(TimePicker view, int hourOfDay,
int minute) {

e_masa.setText(hourOfDay + ":" + minute);
}
}, mHour, mMinute, false);
toTimePickerDialog.show();
}

@Override
public void onClick(View view) {
if(view == fromDateEtxt) {
fromDatePickerDialog.show();
} else if(view == toTimeEtxt) {
toTimePickerDialog.show();
}
}

Answer Source

All you need to do is remove line below from your setDateTimeField() method, because you don't want to show toTimePickerDialog until you click on your button or textView. Now it is in your onCreate method, so it runs every time you run your app.

toTimePickerDialog.show();
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download