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.
Use this to reflect date in edit text
Check this out Date pick tutorial
It shows you the best way to implement this. Then you can just change it to your liking.
What you need to do is get a reference to the
EditText
and theDatePicker
:Then you need to create an
OnDateChangedListener
:All thats left to do is just initialize the DatePicker:
Initialize these variable globally in your activity ,
inside your instance
inside your instance use this listeners,
the function updatedisplay() outside createInstance(),
set like this