In my previous post, I wrote about Android Date Picker class which I developed on my own. Now I’m gonna introduce another class which I developed to get Time from the client. Here we have a Dialog to get hour and minute from the user. This tutorial contains MyTimePicker.java class and dialog_set_time.xml file. As I said before, I need a custom and simple time picker. It will return time data by an Interface call back. Tried to make it too much simple to use and also I would be happy if you help me to improve it.
MyTimePicker.java
public class MyTimePicker { Context context; String hour = "00"; String minute = "00"; public MyTimePicker(final Context context) { this.context = context; } public void showDialog(final OnDataReceive onDataReceive) { final Dialog dialog = new Dialog(context); dialog.requestWindowFeature(Window.FEATURE_NO_TITLE); dialog.setCancelable(true); dialog.setContentView(R.layout.dialog_set_time); final EditText edtHour = dialog.findViewById(R.id.edtHour); final EditText edtMinute = dialog.findViewById(R.id.edtMinute); LinearLayout btnSave = dialog.findViewById(R.id.btnSave); LinearLayout btnCancel = dialog.findViewById(R.id.btnCancel); edtHour.addTextChangedListener(new TextWatcher() { @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) { } @Override public void onTextChanged(CharSequence s, int start, int before, int count) { try { if (Integer.parseInt(edtHour.getText().toString()) > 23) { edtHour.setText(edtHour.getText().toString().substring(0, 1)); edtHour.setSelection(edtHour.getText().length()); Toast.makeText(context, "Invalid time!", Toast.LENGTH_SHORT).show(); } else { if (edtHour.getText().toString().length() == 2) edtMinute.requestFocus(); } } catch (Exception e) { } } @Override public void afterTextChanged(Editable s) { } }); edtMinute.addTextChangedListener(new TextWatcher() { @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) { } @Override public void onTextChanged(CharSequence s, int start, int before, int count) { try { if (Integer.parseInt(edtMinute.getText().toString()) > 59) { edtMinute.setText(edtMinute.getText().toString().substring(0, 1)); edtMinute.setSelection(edtMinute.getText().length()); Toast.makeText(context, "Invalid time!", Toast.LENGTH_SHORT).show(); } } catch (Exception e) { } } @Override public void afterTextChanged(Editable s) { } }); edtMinute.setOnEditorActionListener(new TextView.OnEditorActionListener() { @Override public boolean onEditorAction(TextView v, int actionId, KeyEvent event) { if (actionId == EditorInfo.IME_ACTION_DONE) { hour = String.format("%02d", Integer.parseInt(edtHour.getText().toString().trim())); minute = String.format("%02d", Integer.parseInt(edtMinute.getText().toString().trim())); onDataReceive.OnReceive(hour, minute); dialog.dismiss(); } return false; } }); btnSave.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { hour = String.format("%02d", Integer.parseInt(edtHour.getText().toString().trim())); minute = String.format("%02d", Integer.parseInt(edtMinute.getText().toString().trim())); onDataReceive.OnReceive(hour, minute); dialog.dismiss(); } }); btnCancel.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { dialog.dismiss(); } }); dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE); dialog.show(); } public interface OnDataReceive { void OnReceive(String hour, String minute); } } dialog_set_time.xml
public class MyTimePicker { Context context; String hour = "00"; String minute = "00"; public MyTimePicker(final Context context) { this.context = context; } public void showDialog(final OnDataReceive onDataReceive) { final Dialog dialog = new Dialog(context); dialog.requestWindowFeature(Window.FEATURE_NO_TITLE); dialog.setCancelable(true); dialog.setContentView(R.layout.dialog_set_time); final EditText edtHour = dialog.findViewById(R.id.edtHour); final EditText edtMinute = dialog.findViewById(R.id.edtMinute); LinearLayout btnSave = dialog.findViewById(R.id.btnSave); LinearLayout btnCancel = dialog.findViewById(R.id.btnCancel); edtHour.addTextChangedListener(new TextWatcher() { @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) { } @Override public void onTextChanged(CharSequence s, int start, int before, int count) { try { if (Integer.parseInt(edtHour.getText().toString()) > 23) { edtHour.setText(edtHour.getText().toString().substring(0, 1)); edtHour.setSelection(edtHour.getText().length()); Toast.makeText(context, "Invalid time!", Toast.LENGTH_SHORT).show(); } else { if (edtHour.getText().toString().length() == 2) edtMinute.requestFocus(); } } catch (Exception e) { } } @Override public void afterTextChanged(Editable s) { } }); edtMinute.addTextChangedListener(new TextWatcher() { @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) { } @Override public void onTextChanged(CharSequence s, int start, int before, int count) { try { if (Integer.parseInt(edtMinute.getText().toString()) > 59) { edtMinute.setText(edtMinute.getText().toString().substring(0, 1)); edtMinute.setSelection(edtMinute.getText().length()); Toast.makeText(context, "Invalid time!", Toast.LENGTH_SHORT).show(); } } catch (Exception e) { } } @Override public void afterTextChanged(Editable s) { } }); edtMinute.setOnEditorActionListener(new TextView.OnEditorActionListener() { @Override public boolean onEditorAction(TextView v, int actionId, KeyEvent event) { if (actionId == EditorInfo.IME_ACTION_DONE) { hour = String.format("%02d", Integer.parseInt(edtHour.getText().toString().trim())); minute = String.format("%02d", Integer.parseInt(edtMinute.getText().toString().trim())); onDataReceive.OnReceive(hour, minute); dialog.dismiss(); } return false; } }); btnSave.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { hour = String.format("%02d", Integer.parseInt(edtHour.getText().toString().trim())); minute = String.format("%02d", Integer.parseInt(edtMinute.getText().toString().trim())); onDataReceive.OnReceive(hour, minute); dialog.dismiss(); } }); btnCancel.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { dialog.dismiss(); } }); dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE); dialog.show(); } public interface OnDataReceive { void OnReceive(String hour, String minute); } } Usage
MyTimePicker myTimePicker = new MyTimePicker(MainActivity.this); myTimePicker.showDialog(new MyTimePicker.OnDataReceive() { @Override public void OnReceive(String hour, String minute) { Toast.makeText(MainActivity.this,"Hour: "+hour+" and Minute: "+minute,Toast.LENGTH_SHORT).show(); } });
4 Comments. Leave new
Awesome post! Keep up the great work! 🙂
Great content! Super high-quality! Keep it up! 🙂
Incredible this is a useful websites.
Maintain the exceptional job !! Lovin’ it!