Search code examples
androidandroid-timepicker

How to get TimePickerDialog on edittext appears at first click


How to get TimePickerDialog on edittext appears at first click? It takes twice click to get the dialog appears (the first click shows keyboard). Gradle : minSdkVersion 9 and targetSdkVersion 23

Layout :

<EditText
         android:id="@+id/txtTime"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:layout_weight="1">
         <requestFocus></requestFocus>

      </EditText>

Component :

txtTime.setOnClickListener(new View.OnClickListener() {
         @Override
         public void onClick(View v) {


            // Process to get Current Time
            final Calendar c = Calendar.getInstance();
            mHour = c.get(Calendar.HOUR_OF_DAY);
            mMinute = c.get(Calendar.MINUTE);

            // Launch Time Picker Dialog
            TimePickerDialog tpd = new TimePickerDialog(Pulse7DatePickerDialogActivity.this,
                new TimePickerDialog.OnTimeSetListener() {

                   @Override
                   public void onTimeSet(TimePicker view, int hourOfDay,
                                         int minute) {
                      // Display Selected time in textbox
                      txtTime.setText(hourOfDay + ":" + minute);
                   }
                }, mHour, mMinute, false);
            tpd.show();

         }
      });

Solution

  • add android:focusableInTouchMode="false" in layout declaration

    <EditText
         android:id="@+id/txtTime"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:focusableInTouchMode="false"
         android:layout_weight="1">         
    
      </EditText>