java – How to prevent an Android EditText from showing the keyboard when it receives focus (without disabling the control)?

Question:

It happens that I have an Edit Text of type textPersonName and when I pass the focus to another Edit Text of type date it shows me the virtual keyboard but I want it not to be shown, since instead I want a dialog to be shown that contains a DatePicker (which I already have)

This question has already been asked and the solution I read is:

TuEditText.setInputType(InputType.TYPE_NULL); 

and in the xml:

android:inputType="none"

but this only removes the focus and I have to click on the EditText to show the DatePicker dialog and I want it to keep the focus but not show the virtual keyboard and show my DatePicker dialog

Another solution I saw is to remove the focus from the EditText date with the following code:

android:focusable="false"

but this is not what I intend to do since when I remove the focus I must click on the EditText to show the DatePicker dialog I repeat I intend to keep the focus but not show the virtual keyboard and show my DatePicker dialog

Here is the xml code:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin">

 <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inputType="textPersonName"
        android:ems="10"
        android:id="@+id/txt_practica"
        android:hint="Practica"
        android:layout_marginTop="9dp"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />

<EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inputType="date"
        android:ems="10"
        android:hint="Fecha de uso"
        android:id="@+id/txt_fechadeuso"
        android:layout_below="@+id/txt_practica"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:layout_marginTop="16dp"
        android:onClick="mostrarCalendario" />

</RelativeLayout>

Answer:

You can try this:

if(tuEdtittext.requestFocus()) {
esconderTecladoyAbrirElDatePicker();
}

Also try making use of OnFocusChangeListener :

 tuEdittext .setOnFocusChangeListener(new OnFocusChangeListener() {          

    public void onFocusChange(View v, boolean hasFocus) {
        //Escondes el teclado y muestras el datePicker
    }
});
Scroll to Top