'How to use datepicker in Android
I want to use date picker in Android . Here is my XML code. Depart Date should start from current date and the return date should be greater than my departDate
<LinearLayout
android:id="@+id/dateLayout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/fromToLayout"
android:baselineAligned="false"
android:orientation="horizontal" >
<LinearLayout
android:id="@+id/departdateLayout"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2"
android:orientation="vertical" >
<TextView
android:id="@+id/deparDateTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="7dp"
android:gravity="center_horizontal"
android:text="@string/departdate" />
<TextView
android:id="@+id/departDate"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:text="dd/mm/yyyy" />
</LinearLayout>
<LinearLayout
android:id="@+id/returndateLayout"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2"
android:orientation="vertical" >
<TextView
android:id="@+id/returndata"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="7dp"
android:gravity="center_horizontal"
android:text="@string/returndate" />
<TextView
android:id="@+id/returnDate"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:text="dd/mm/yyyy"/>
</LinearLayout>
</LinearLayout>
In place of the TextView whose id returnDate and depart date i want to use datepicker so that the user clicks on that a calender gets open from there he can select the date and that date get filled in this Texview
Solution 1:[1]
Try the following way.
TextView departDate = (Button) findViewById(R.id.departDate);
departDate.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
Calendar calender = Calendar.getInstance();
Dialog mDialog = new DatePickerDialog(IncomeActivity.this,
mDatesetListener, calender.get(Calendar.YEAR),
calender.get(Calendar.MONTH), calender
.get(Calendar.DAY_OF_MONTH));
mDialog.show();
}
});
Then write the following code as seperate method.
private DatePickerDialog.OnDateSetListener mDatesetListener = new OnDateSetListener() {
public void onDateSet(DatePicker arg0, int arg1, int arg2, int arg3) {
arg2 = arg2 + 1;
String my_date = arg1 + "-" + arg2 + "-" + arg3;
departDate.setText(my_date);
}
};
I hope this will help you.
Solution 2:[2]
YOu can do something like this :-
public class mExample extends Activity {
private TextView returnDate;
private int year;
private int month;
private int day;
static final int DATE_PICKER_ID = 1111;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
returnDate= (TextView) findViewById(R.id.returnDate);
// Get current date by calender
final Calendar c = Calendar.getInstance();
year = c.get(Calendar.YEAR);
month = c.get(Calendar.MONTH);
day = c.get(Calendar.DAY_OF_MONTH);
// Show current date
returnDate.setText(new StringBuilder()
// Month is 0 based, just add 1
.append(month + 1).append("-").append(day).append("-")
.append(year).append(" "));
returnDate.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// On click show datepicker dialog
showDialog(DATE_PICKER_ID);
}
});
}
@Override
protected Dialog onCreateDialog(int id) {
switch (id) {
case DATE_PICKER_ID:
// open datepicker dialog.
// set date picker for current date
// add pickerListener listner to date picker
return new DatePickerDialog(this, pickerListener, year, month,day);
}
return null;
}
private DatePickerDialog.OnDateSetListener pickerListener = new DatePickerDialog.OnDateSetListener() {
// when dialog box is closed, below method will be called.
@Override
public void onDateSet(DatePicker view, int selectedYear,
int selectedMonth, int selectedDay) {
year = selectedYear;
month = selectedMonth;
day = selectedDay;
// Show selected date
returnDate.setText(new StringBuilder().append(month + 1)
.append("-").append(day).append("-").append(year)
.append(" "));
}
};
}
Solution 3:[3]
Try dis code this solve u r all problems
public class SearchFlight extends Activity {
EditText depart,returnDate;
Calendar myCalendar;
int val;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_search_flight);
depart=(EditText)findViewById(R.id.departDate);
returnDate=(EditText)findViewById(R.id.returnDate);
myCalendar = Calendar.getInstance();
final DatePickerDialog.OnDateSetListener date = new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear,
int dayOfMonth) {
// TODO Auto-generated method stub
myCalendar.set(Calendar.YEAR, year);
myCalendar.set(Calendar.MONTH, monthOfYear);
myCalendar.set(Calendar.DAY_OF_MONTH, dayOfMonth);
updateLabel(val);
}
};
depart.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
new DatePickerDialog(SearchFlight.this, date, myCalendar
.get(Calendar.YEAR), myCalendar.get(Calendar.MONTH),
myCalendar.get(Calendar.DAY_OF_MONTH)).show();
val=1;
}
});
returnDate.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
new DatePickerDialog(SearchFlight.this, date, myCalendar
.get(Calendar.YEAR), myCalendar.get(Calendar.MONTH),
myCalendar.get(Calendar.DAY_OF_MONTH)).show();
val=2;
}
});
}
private void updateLabel(int val) {
String myFormat = "dd/MM/yy"; //In which you need put here
SimpleDateFormat sdf = new SimpleDateFormat(myFormat, Locale.US);
if(val==1)
depart.setText(sdf.format(myCalendar.getTime()));
else
returnDate.setText(sdf.format(myCalendar.getTime()));
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | |
| Solution 2 | Pooja Gaonkar |
| Solution 3 |
