How to set the value of the datePicker in to EditText?

23,900

Solution 1

What you need to do is get a reference to the EditText and the DatePicker:

EditText editText = (EditText) findViewById( R.id.dateSelectionEditText );
DatePicker datePicker = (DatePicker) findViewById( R.id.datePicker );

Then you need to create an OnDateChangedListener:

private class MyOnDateChangedListener implements OnDateChangedListener {
  @Override
  public void onDateChanged(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
     editText.setText( "" + dayOfMonth + "-" + (monthOfYear + 1) + "-" + year );
  }
};

All thats left to do is just initialize the DatePicker:

datePicker.init( year, monthOfYear, dayOfMonth, new MyOnDateChangedListener() ).

Solution 2

Check this out Date pick tutorial

It shows you the best way to implement this. Then you can just change it to your liking.

Solution 3

set like this

public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {

        selectedDate = Integer.toString(mMonth)+"/"+Integer.toString(mDay)+"/"+Integer.toString(mYear);
        date_text.setText(selectedDate);
        System.out.println("selected date is:"+selectedDate);
        edittext.setText(selectedDate );
    }
};

Solution 4

Initialize these variable globally in your activity ,

private EditText zopenDate;
        Button calendar;

        private int mYear;
        private int mMonth;
        private int mDay;
        private DatePickerDialog.OnDateSetListener mDateSetListener;

inside your instance

calendar=(Button)findViewById(R.id.datePicker);
zopenDate=(EditText)findViewById(R.id.dateSelectionEditText);

inside your instance use this listeners,

mDateSetListener =
            new DatePickerDialog.OnDateSetListener() {

                public void onDateSet(DatePicker view, int year, 
                                      int monthOfYear, int dayOfMonth) {
                    mYear = year;
                    mMonth = monthOfYear;
                    mDay = dayOfMonth;
                    updateDisplay();
                }
            };



calendar.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {
                    showDialog(DATE_DIALOG_ID);
                }
            });




final Calendar c = Calendar.getInstance();
            mYear = c.get(Calendar.YEAR);
            mMonth = c.get(Calendar.MONTH);
            mDay = c.get(Calendar.DAY_OF_MONTH);

            // display the current date (this method is below)
            updateDisplay();

the function updatedisplay() outside createInstance(),

private void updateDisplay() {
        zopenDate.setText(
            new StringBuilder()
                    // Month is 0 based so add 1
                    .append(mMonth + 1).append("-")
                    .append(mDay).append("-")
                    .append(mYear).append(" "));
    }
Share:
23,900
Shreyash Mahajan
Author by

Shreyash Mahajan

About Me https://about.me/sbm_mahajan Email [email protected] [email protected] Mobile +919825056129 Skype sbm_mahajan

Updated on June 25, 2020

Comments

  • Shreyash Mahajan
    Shreyash Mahajan almost 4 years

    My XMl Layout is as like below:

    <RelativeLayout  android:id="@+id/dateSelectionLayout"  android:layout_width="fill_parent" android:layout_height="wrap_content"
            android:orientation="vertical" android:visibility="visible">
    
                <EditText android:layout_height="wrap_content" android:layout_width="fill_parent" 
                    android:singleLine="true" android:id="@+id/dateSelectionEditText" android:gravity="center"
                    android:textColor="#000000" android:textSize="14sp" android:cursorVisible="false"
                    android:focusable="false"
                    android:hint="tax code" android:layout_weight="1"/>
    
                <DatePicker android:id="@+id/datePicker"
                    android:layout_width="fill_parent" android:layout_height="wrap_content"
                    android:layout_weight="1" android:layout_below="@+id/dateSelectionEditText"/>
    
        </RelativeLayout>
    

    Now i want to change the value of the editText that is based on the datePicker date. If User change the date then it should be reflected on the editText at that time. how it is Possible ?

    Edited: I have done like this: Have set the resourcec like:

    datePicker = (DatePicker) findViewById(R.id.datePicker);
            dateSelectionEditText = (EditText)findViewById(R.id.dateSelectionEditText);
    

    And have set the override method like this:

    @Override
    public void onDateChanged(DatePicker view, int year, int monthOfYear,
            int dayOfMonth) {
        dateSelectionEditText.setText(dayOfMonth+"/"+monthOfYear+"/"+year);
    
    }
    

    But still not getting any value on the changing of date picker value.

    Edited:

    After Kasper Moerch's answer i got the solution. But there is little problem. I am using this code to init the datepicker.

    final Calendar c = Calendar.getInstance();  
            //dateSelectionEditText.setText( "" + dayOfMonth + "-" + monthOfYear + "-" + year );
            datePicker.init(c.get(Calendar.YEAR), c.get(Calendar.MONTH), c.get(Calendar.DAY_OF_MONTH), new MyOnDateChangedListener());
            Toast.makeText(getApplicationContext(), ""+position+"", Toast.LENGTH_SHORT).show();
    

    Now with this, I am able to see the changed value from datePicker. But it takes 0 as a First month (from January) instead of the "1". So why it is happend like this ?

    Thanks.

  • Shreyash Mahajan
    Shreyash Mahajan over 12 years
    Please see the Updated question.
  • RajaReddy PolamReddy
    RajaReddy PolamReddy over 12 years
    what is the back ground for that
  • Shreyash Mahajan
    Shreyash Mahajan over 12 years
    if i change the datePickerValue then should it reflact on the editText at that time.
  • Shreyash Mahajan
    Shreyash Mahajan over 12 years
    If i change the Value of the Month or date or year then should it be reflacted on the editText ?
  • Shreyash Mahajan
    Shreyash Mahajan over 12 years
    Should it be reflacted on the EditText if i change the day or month or year of the datePicker ?
  • Shreyash Mahajan
    Shreyash Mahajan over 12 years
    Where should i have to init the datePicker ?
  • kaspermoerch
    kaspermoerch over 12 years
    You should init it right after your get the reference to your EditText and DatePicker.
  • Chet
    Chet over 12 years
    i m not sure but try ontouch/onChange event for datePicker
  • RajaReddy PolamReddy
    RajaReddy PolamReddy over 12 years
    do it date picker through code, if you don't know come to chat room i will tell..
  • Shreyash Mahajan
    Shreyash Mahajan over 12 years
    If i am going to init the datePicker after get the reference then i am not able to get the variable year, monthOfYear and dayOfMonth in that init function.
  • Shreyash Mahajan
    Shreyash Mahajan over 12 years
    Please tell me How to set this. as i am not able to get it.
  • Shreyash Mahajan
    Shreyash Mahajan over 12 years
    By Your answer i am near to solution. Just See my Updated question.
  • RajaReddy PolamReddy
    RajaReddy PolamReddy over 12 years
    create date picker dialog through code .. check this link stackoverflow.com/questions/3299392/date-picker-in-android