1

I want to disable the Keyboard permanently from a DatePicker EditText so that I manually have to choose a date from a DatePicker dialog...

Here is my java code:

import android.annotation.TargetApi;
import android.app.Activity;
import android.app.DatePickerDialog;
import android.content.*;
import android.app.Dialog;
import android.app.FragmentTransaction;
import android.os.Build;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EdgeEffect;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;

import java.util.Date;

public class MainActivity extends AppCompatActivity implements AdapterView.OnItemSelectedListener{
    Spinner spinner;
    EditText Datetxt;
    int Date_x,Month_x,Year_x;
     static final int DIALOG_ID=0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        spinner=(Spinner)findViewById(R.id.spinner);
        ArrayAdapter adapter=ArrayAdapter.createFromResource(this,R.array.Gender,android.R.layout.simple_spinner_dropdown_item);
        spinner.setAdapter(adapter);
        spinner.setOnItemSelectedListener(this);
        showDialogonClick();
    }
    @Override
    public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
        //Toast.makeText(MainActivity.this, "you select", Toast.LENGTH_SHORT).show();

    }

    @Override
    public void onNothingSelected(AdapterView<?> adapterView) {

    }

    public void showDialogonClick()
    {
        Datetxt=(EditText)findViewById(R.id.Datetxt);
        Datetxt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showDialog(DIALOG_ID);
            }

        }

        );
}
    protected Dialog onCreateDialog(int id){
        if(id==DIALOG_ID)
            return new DatePickerDialog(this,dpickerListener,Date_x,Month_x,Year_x);
        else return null;
    }
    private DatePickerDialog.OnDateSetListener dpickerListener= new DatePickerDialog.OnDateSetListener() {
        @TargetApi(Build.VERSION_CODES.LOLLIPOP)
        @Override
        public void onDateSet(DatePicker datePicker, int Day, int Month, int Year) {
            Date_x=Day;
            Month_x=Month;
            Year_x=Year;
            //Datetxt.setShowSoftInputOnFocus(false);
            String Date1=Date_x+"."+Month_x+"."+Year_x;
            Datetxt.setText(Date1);

        }
    };

}

When I click on EditText both the calendar and keyboard are opened simultaneously.

Amit Vaghela
  • 22,772
  • 22
  • 86
  • 142
BHAGAT
  • 27
  • 1
  • 5

4 Answers4

8

You can also add these attributes to your EditText in xml file

android:focusableInTouchMode="false"
android:inputType="date"
Marat
  • 6,142
  • 6
  • 39
  • 67
3

Use this way it solve your Problem.

EditText et = (EditText)findViewById(R.id.et);
et.setInputType(InputType.TYPE_NULL);
Harshad Pansuriya
  • 20,189
  • 8
  • 67
  • 95
2

Use

 myTimePicker.setDescendantFocusability(TimePicker.FOCUS_BLOCK_DESCENDANTS);

to disable focus on the text views of the internal NumberPickers

Another Way . Add this in your XML:

android:descendantFocusability="blocksDescendants"

Amit Vaghela
  • 22,772
  • 22
  • 86
  • 142
1
android:focusable="false"
android:hint="@string/history_by_date_example"
android:inputType="none"

Setting inputType to none in your xml file will not open keyboard and you can open date picker dialog on click of your edittext

Draken
  • 3,134
  • 13
  • 34
  • 54
Bhavnik
  • 2,020
  • 14
  • 21