Search code examples
androidandroid-datepicker

Setting dates on multiple edittext views


I have a situation with multiple edit texts that get rendered dynamically (edittext views are rendered programmatically) need to be populated with Dates when I click within each of the edittext views. I do not know how many edit texts will be needed and hence cannot hard code them in a layout XML file.

Although I am able to render the views properly, when I try to set the date on one edittext, the value always gets set on the last edittext in the group. I am not able to figure out how to send a unique id to identify the selected edittext so the date can be set properly. Tried looking through multiple posts on the site but couldnt get any of them to work yet.

I can paste the entire code if needed.

Code Snippet

            private DatePickerDialog fromDatePickerDialog;
            TableLayout table = new TableLayout(this);
            TextView textViewColumn = new TextView(this);
            textViewColumn.setGravity(Gravity.RIGHT);
            textViewColumn.setTextColor(Color.BLACK);
            textViewColumn.setPadding(0, 0, 5, 0);
            textViewColumn.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 15);
            textViewColumn.setTypeface(Typeface.SANS_SERIF, Typeface.BOLD);
            row.addView(textViewColumn);

            if(childObj.getType().equals("Date")){
                final EditText rowEditText = new EditText(this);
                setDateTimeField(rowEditText); //setting the date
                rowEditText.setMaxWidth(50);
                rowEditText.setMaxHeight(40);
                rowEditText.setTag(childObj.getName());
                row.addView(rowEditText);
            }

            table.addView(row);

Setting the date in the edit text - code snippet

private void setDateTimeField(final EditText dateEditText) {
    dateEditText.setOnClickListener(this);
    dateEditText.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 15);
    dateEditText.setTypeface(Typeface.SANS_SERIF, Typeface.BOLD);

    dateEditText.setGravity(Gravity.LEFT);

    Calendar newCalendar = Calendar.getInstance();
    fromDatePickerDialog = new DatePickerDialog(this, new DatePickerDialog.OnDateSetListener() {

        public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
            Calendar newDate = Calendar.getInstance();
            newDate.set(year, monthOfYear, dayOfMonth);
            dateEditText.setText(dateFormatter.format(newDate.getTime()));
        }

    },newCalendar.get(Calendar.YEAR), newCalendar.get(Calendar.MONTH), newCalendar.get(Calendar.DAY_OF_MONTH));

    fromDatePickerDialog.setButton(DialogInterface.BUTTON_NEGATIVE, getString(R.string.cancel), new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {
            if (which == DialogInterface.BUTTON_NEGATIVE) {
                dialog.cancel();
                dateEditText.setText("");
            }
        }
    });
}

onClick

@Override
public void onClick(View view) {
    fromDatePickerDialog.show();
}

Result

enter image description here


Solution

  • I think that every time you create a new EditText, your are you overrighting fromDatePickerDialog with a new instance by doing fromDatePickerDialog = new DatePickerDialog(this, ne, .. and the only one saved is the one with the last ediText. Your solution is maybe to create a new object DatePickerDialog for each editText created something like :

    private void setDateTimeField(final EditText dateEditText) {
            dateEditText.setOnClickListener(this);
            dateEditText.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 15);
            dateEditText.setTypeface(Typeface.SANS_SERIF, Typeface.BOLD);
    
            dateEditText.setGravity(Gravity.LEFT);
    
            Calendar newCalendar = Calendar.getInstance();
            DatePickerDialog datePickerDialog = new DatePickerDialog(this, new DatePickerDialog.OnDateSetListener() {
                public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
                    Calendar newDate = Calendar.getInstance();
                    newDate.set(year, monthOfYear, dayOfMonth);
                    dateEditText.setText(dateFormatter.format(newDate.getTime()));
                }
    
            }, newCalendar.get(Calendar.YEAR), newCalendar.get(Calendar.MONTH), newCalendar.get(Calendar.DAY_OF_MONTH));
    
            datePickerDialog.setButton(DialogInterface.BUTTON_NEGATIVE, getString(R.string.cancel), new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                    if (which == DialogInterface.BUTTON_NEGATIVE) {
                        dialog.cancel();
                        dateEditText.setText("");
                    }
                }
            });
        }
    

    and also you can set setOnClickListner inside the above function like :

    dateEditText.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                datePickerDialog.show();
            }
        });
    

    Hope this helps,