Reputation: 33
i have an activity in which i have an edittext. On click on edittext i want to open TIMEPICKER DIALOG. I had written the code for it and its working. The problem is that when i start the activity and click on edittext for first time-the virtual keyboard opens however for subsequent click on edittext TIMEPICKERDIALOG opens. I do not want virtual keyboard to open even for first time.I want TIMEPICKER DIALOG to open every time. Please help
Here is my code for timepickerdialog to open on edittext click
timeText=(EditText)findViewById(R.id.editText2);
timeText.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Calendar mcurrentTime = Calendar.getInstance();
int hour = mcurrentTime.get(Calendar.HOUR_OF_DAY);
int minute = mcurrentTime.get(Calendar.MINUTE);
TimePickerDialog mTimePicker;
mTimePicker = new TimePickerDialog(RemindDetail.this, new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker timePicker, int selectedHour, int selectedMinute) {
timeText.setText( selectedHour + ":" + selectedMinute);
calender[3]=selectedHour;
calender[4]=selectedMinute;
}
}, hour, minute, false);//Yes 24 hour time
mTimePicker.setTitle("Select Time");
mTimePicker.show();
}
});
Upvotes: 2
Views: 1760
Reputation: 268
Hi few days before I was facing the same problem. Then I used onTouchListener
on EditText
. What it does. it will open your datepicker when someone touch the EditText.
use onTouchListener rather then onClickListener
on EditText.
you can use this code. which I develop for android xamarin change it for android java. and Extend View.IOnTouchListener
DOB.SetOnTouchListener (this);
protected override Dialog OnCreateDialog (int id)
{
if (id == DATE_DIALOG_ID) {
return new DatePickerDialog (this, HandleDateSet, _date.Year,
_date.Month - 1, _date.Day);
}
return null;
}
void HandleDateSet (object sender, DatePickerDialog.DateSetEventArgs e)
{
_date = e.Date;
DOB.Text = _date.ToString ("d");
}
public bool OnTouch(View v, MotionEvent e)
{
if (e.Action == MotionEventActions.Up) {
ShowDialog (DATE_DIALOG_ID);
}
return true;
}
hope it might help you.
Upvotes: 0
Reputation: 675
Just set the EditText
property like android:focusable="false"
in your xml.
Upvotes: -2