TimePicker Dialog when pressing EditText

Simon A. Callaghan Jensen picture Simon A. Callaghan Jensen · Sep 22, 2016 · Viewed 8.9k times · Source

This question has been asked before, but the answers from back then, doesn't seem to work in Android Studio anymore, or else i'm missing something.

I want a timePicker dialog to show up when you press the edit text area, to set the time in the editText. However, for some reason, the normal keyboard simply pops up when pressed. I'm not getting any errors, though it still doesn't seem to work.

here is the code:

final EditText Edit_Time = (EditText) findViewById(R.id.time_view_edit);

    Edit_Time.setOnClickListener(new View.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(TimeActivity.this, new TimePickerDialog.OnTimeSetListener() {
                @Override
                public void onTimeSet(TimePicker timePicker, int selectedHour, int selectedMinute) {
                    Edit_Time.setText( selectedHour + ":" + selectedMinute);
                }
            }, hour, minute, true);
            mTimePicker.setTitle("Select Time");
            mTimePicker.show();

        }
    });

the xml part of editText:

<EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/time_view_edit"
            android:text="20:00"
            android:layout_weight="1"/>

If anyone can help, then it's much appreciated, Thanks!

Answer

SripadRaj picture SripadRaj · Sep 22, 2016

Add these properties to your EditText tag in xml to disable keyboard popping up

android:editable="false" 

and

android:focusable="false"

UPDATE:

android:editable="false" is deprecated now. use android:inputType="none" instead or in the code:

editText.setEnabled(false);