Yousef
Yousef

Reputation: 54

Getting Time Display in EditText of TimePickerDialog in Android

I'm using a TimePickerDialog in my application, I use a hint for the EditText (through which time is set), but once the user sets time in the TimePickerDialog the hint goes away and the time selected replaces it.

I want the present time (hour and minute) to be displayed in the EditText before the user sets the time, How can I do that?

Upvotes: 1

Views: 169

Answers (2)

Yousef
Yousef

Reputation: 54

I added the following code to my project and it worked:

SimpleDateFormat timeFormat = new SimpleDateFormat("HH:mm", Locale.getDefault());
String time = timeFormat.format(Calendar.getInstance().getTime());
editText.setText(time);

Upvotes: 1

Irfan
Irfan

Reputation: 956

get the current time and set it to the edit text.

EditText editText = (EditText) findViewById(R.id.editText);
Date currentDate = new Date(System.currentTimeMillis());
currentDate.setText(currentDate.toString());

and if you want to perform any actions on edit text click, perform on click like opening a date picker and on selection set seletted date again to the editText. Hope it helps.

Upvotes: 2

Related Questions