Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TimePicker Dialog when pressing EditText

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!

like image 748
Simon A. Callaghan Jensen Avatar asked Sep 22 '16 08:09

Simon A. Callaghan Jensen


People also ask

What is timepicker in Android app?

Android TimePicker is a user interface control for selecting the time in either 24-hour format or AM/PM mode. It is used to ensure that users pick the valid time for the day in our application. The time picker interface exists basically in two modes one is under XML layout and another is a dialog.

How to change the time in Android edittext?

You will build an Android app that contain an EditText, once you tap on it you will see an Android TimePickerDialog which allows you to choose the time and the selected time will appear later inside Android EditText. By the end of this tutorial, you will have an app that looks like this.

How to use Kotlin timetime picker dialog in Android?

Time Picker Dialog in Android 1 Step 1: Create an empty activity project#N#Create an empty activity Android Studio project and select Kotlin as the... 2 Step 2: Working with the activity_main.xml file#N#The main layout of the application contains a button and TextView to... 3 Step 3: Working with MainActivity.kt file More ...


2 Answers

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);
like image 95
SripadRaj Avatar answered Sep 28 '22 01:09

SripadRaj


This worked for me on androidx projects. Create showTimePicker method.

    private void showTimePicker(){
        Calendar mcurrentTime = Calendar.getInstance();
        int hour = mcurrentTime.get(Calendar.HOUR_OF_DAY);
        int minute = mcurrentTime.get(Calendar.MINUTE);
        TimePickerDialog timePickerDialog = new TimePickerDialog(EditPharmacyActivity.this, new TimePickerDialog.OnTimeSetListener() {

            @Override
            public void onTimeSet(TimePicker timePicker, int selectedHour, int selectedMinute) {

            }
        }, hour, minute, false);
        timePickerDialog.show();
    }

And in onCreate method add followings to init the views.

        etOpenHours.setClickable(true);
        etOpenHours.setLongClickable(false);
        etOpenHours.setInputType(InputType.TYPE_NULL);

        etOpenHours.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
               
                showTimePicker();
            }
        });

like image 41
emkarachchi Avatar answered Sep 28 '22 02:09

emkarachchi