Android - DatePicker in an editText

4

Next, I would like a date selector to appear as soon as I click on the editText that I have, but what I found is automatically placing the current date, I want it to be chosen ...

Here is the code I've tried so far:

public class AtividadePrincipal extends AppCompatActivity {
ListView lvLista;
List<String> nomes;
EditText data1;
Calendar dateSelected = Calendar.getInstance();
private DatePickerDialog datePickerDialog;
String myFormat = "dd-MM-yyyy";
SimpleDateFormat sdf = new SimpleDateFormat(myFormat);

 protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_atividade_principal);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    View view = this.getCurrentFocus();
    if (view != null) {
        InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
    }
 data1.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            setDateTimeField();
           // showDialog(DATE_DIALOG_ID);
            return false;
        }
    });

private void setDateTimeField() {
    Calendar newCalendar = dateSelected;
    datePickerDialog = new DatePickerDialog(this, new DatePickerDialog.OnDateSetListener() {

        public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
            dateSelected.set(year, monthOfYear, dayOfMonth, 0, 0);
            data1.setText(sdf.format(dateSelected.getTime()));
        }

    }, newCalendar.get(Calendar.YEAR), newCalendar.get(Calendar.MONTH), newCalendar.get(Calendar.DAY_OF_MONTH));
    data1.setText(sdf.format(dateSelected.getTime()));
}
}

follows the xml of the editText data1:

<EditText
                android:id="@+id/data1"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginBottom="15dp"
                android:layout_marginTop="15dp"
                android:background="@drawable/cantos_arredondados"
                android:drawableRight="@drawable/icone_cor_diferente"
                android:hint="Data1"
                android:inputType="date"
                android:paddingTop="8dp"
                android:textColor="@color/colorAccent" />
    
asked by anonymous 23.05.2017 / 07:41

1 answer

4

Try this way:

Calendar myCalendar = Calendar.getInstance();

DatePickerDialog.OnDateSetListener date = new DatePickerDialog.OnDateSetListener() {

    @Override
    public void onDateSet(DatePicker view, int year, int monthOfYear,
            int dayOfMonth) {
        myCalendar.set(Calendar.YEAR, year);
        myCalendar.set(Calendar.MONTH, monthOfYear);
        myCalendar.set(Calendar.DAY_OF_MONTH, dayOfMonth);
        updateLabel();
    }

    };

   edittext.setOnClickListener(new OnClickListener() {

   @Override
   public void onClick(View v) {
      new DatePickerDialog(classname.this, date, myCalendar
               .get(Calendar.YEAR), myCalendar.get(Calendar.MONTH),
                myCalendar.get(Calendar.DAY_OF_MONTH)).show();
        }
    });

    private void updateLabel() {

        String myFormat = "dd/MM/yyyy"; //In which you need put here
        SimpleDateFormat sdf = new SimpleDateFormat(myFormat, new Locale("pt","BR"));

        edittext.setText(sdf.format(myCalendar.getTime()));
    }
    
23.05.2017 / 14:51