As i am new to android please help me to add calender view in dialog box on button click
I used following but it shows me scrollable day year date pattern calender
but i want to show calender like Jquery calender view which is month wise how can i show please help
by showing code thnks in advance
protected Dialog onCreateDialog( int id) {// to create Date dialog
switch (id) {
case OE_DT_DIALOG_ID:
final Calendar calendar = Calendar.getInstance();
int yy = calendar.get(Calendar.YEAR);
int mm = calendar.get(Calendar.MONTH);
int dd = calendar.get(Calendar.DAY_OF_MONTH);
// set date picker as current date
return new DatePickerDialog(this, datePickerListener, yy, mm,dd);
}
return null;
}
#SuppressWarnings("unused")
private DatePickerDialog.OnDateSetListener datePickerListener = new DatePickerDialog.OnDateSetListener() {
// when dialog box is closed, below method will be called.
#SuppressWarnings("deprecation")
public void onDateSet(DatePicker view, int selectedYear,int selectedMonth, int selectedDay) {
year = selectedYear;
month = selectedMonth;
day = selectedDay;
// set selected date into btn View
lblDate.setText(new StringBuilder().append(day).append("-").append(month + 1)
.append("-").append(year).append(" "));
}
};
to show calender like this
You can do this with a Pager View. Make your pager view as horizontal scrollable and get the instance of each month and display that using a fragment. Hope this would give an idea...
Please find the help here. It is a custom library you've to link with your code
Related
I'm experiencing a weird visual bug on new DatePickerDialog of Android widgets. I need to set a dialog's selectable date range as 1 month but it shouldn't also select a date from the future. Logic works as expected but the visuals have some sort of bug. Here is how it looks:
Note that the "5" is disabled, I cannot select it. However, it looks as selectable. What should I do to solve this issue? Any help is appreciated, thanks. (Today's day of month is also 5, I assume this bug comes from there.)
Edit: This is how I create the dialog.
#SuppressLint("ValidFragment")
public static class DateDialog extends DialogFragment implements DatePickerDialog.OnDateSetListener
{
private static final long MONTH = 2592000000L;
#Override
public Dialog onCreateDialog(Bundle savedInstanceState)
{
// Use previous selected date in the date picker
final Calendar c = Calendar.getInstance();
c.setTimeInMillis(DateUtils.getAtTimeTimestamp(startDate));
int year = c.get(Calendar.YEAR);
int month = c.get(Calendar.MONTH);
int day = c.get(Calendar.DAY_OF_MONTH);
DatePickerDialog dialog = new DatePickerDialog(getActivity(), R.style.AppTheme_DialogTheme, this, year, month, day);
Date date = new Date(); // get current time
// get target maximum date which can be 1 month above
long targetDateTime = DateUtils.getAtTimeTimestamp(startDate) + MONTH;
if (date.getTime() < targetDateTime)
dialog.getDatePicker().setMaxDate(date.getTime()); // now because it can't be future
else
dialog.getDatePicker().setMaxDate(targetDateTime); // should also be one month above from the start date.
dialog.getDatePicker().setMinDate(DateUtils.getAtTimeTimestamp(startDate));
return dialog;
}
#Override
public void onDateSet(DatePicker datePicker, int i, int i1, int i2)
{
// logic between date selections
}
}
DateUtils.getAtTimeTimestamp(String date) basically converts a string to long integer that is time in milliseconds.
Over the past 24 hours I have been trying to find articles about my issue but am still confused. If I have missed a place where this question was previously answered I apologize but I hope that you all will still help me understand what I am missing.
Synopsis: I want to populate a few text boxes with a date picker and am having trouble understanding how to return the selected date so that I can populate the appropriate textbox.
Basically I'm trying to implement a standard date picker targeting android devices >= 4.0.
public static class DatePickerFragment extends DialogFragment
implements DatePickerDialog.OnDateSetListener {
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Use the current date as the default date in the picker
final Calendar c = Calendar.getInstance();
int year = c.get(Calendar.YEAR);
int month = c.get(Calendar.MONTH);
int day = c.get(Calendar.DAY_OF_MONTH);
// Create a new instance of DatePickerDialog and return it
return new DatePickerDialog(getActivity(), this, year, month, day);
}
public void onDateSet(DatePicker view, int year, int month, int day) {
// Do something with the date chosen by the user
}
}
public void showDatePickerDialog(View v) {
DialogFragment newFragment = new DatePickerFragment();
newFragment.show(getSupportFragmentManager(), "datePicker");
}
You will notice that this is the code from the android documentation example that can be found here: http://developer.android.com/guide/topics/ui/controls/pickers.html
I am implementing this code in a larger project and the date picker comes up fine, the date is picked fine etc.
What I am having trouble with is getting the date after the user selects it. Basically the user selects the date and then nothing happens. What I want to do is populate a specific text box with the date after selection. This will happen in a few text boxes so I can't just hard code it into the onDateSet function.
My thought was to pass the textBox's id with the creation of the datepicker but I am not completely sure how I would do that.
This is my first android project and focused on comprehension and not simply getting a working project, thanks.
The "onDateSet" method should be called when the user selects a date.
In this method, you can use the year, month and day passed in parameter.
I don't known if that is the best way to do it, but I solve it this way:
I have two buttons, A and B.
When i click button A, a DatePicker appears and the user selects the date.
After confirm the date, the text in Button A is set to that date that was selected.
Same behavior for button B.
That is the function when button A or B is pressed:
public void selectDateButtons(View view){
if(view.getId() == R.id.buttonA)
{
clickedButton = R.id.buttonA;
}else{
clickedButton = R.id.buttonB;
}
DialogFragment newFragment = new DatePickerFragment();
newFragment.show(getSupportFragmentManager(), "datePicker");
}
clickedButton is a Static global variable.
Then declare the sub-class
public static class DatePickerFragment extends DialogFragment
implements DatePickerDialog.OnDateSetListener {
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Use the current date as the default date in the picker
final Calendar c = Calendar.getInstance();
int year = c.get(Calendar.YEAR);
int month = c.get(Calendar.MONTH);
int day = c.get(Calendar.DAY_OF_MONTH);
return new DatePickerDialog(getActivity(), this, year, month, day);
}
#Override
public void onDateSet(DatePicker view, int year, int month, int day) {
if(clickedButton == R.id.ButtonA)
{
ButtonA.setText(String.valueOf(day) + "/"
+ String.valueOf(month + 1) + "/" + String.valueOf(year));
} else {
ButtonB.setText(String.valueOf(day) + "/"
+ String.valueOf(month + 1) + "/" + String.valueOf(year));
}
}
}
Don't forget, your main activity should extends FragmentActivity.
Hope that works for you too!
Best regards!
I need a date-picker dialog in my application. First, I open the date picker dialog box and I select the date. The next time whenever open Date pickerDialog, the date of date picker should be current date of device. However, it shows last selected date in dialog. Please help me to set the code. Here is my code as I have it now:
#Override
protected Dialog onCreateDialog(int id)
{
switch (id)
{
case DATE_DIALOG_ID:
Date d=new Date();
Calendar c=Calendar.getInstance();
c.setTimeZone(tz);
c.setTime(d);
d=c.getTime();
int year = c.get(Calendar.YEAR);
int month = c.get(Calendar.MONTH);
int date = c.get(Calendar.DATE);
DatePickerDialog datePicker
= new DatePickerDialog(this, datePickerListener, year, month, date);
return datePicker;
}
return super.onCreateDialog(id);
}
private DatePickerDialog.OnDateSetListener datePickerListener
= new DatePickerDialog.OnDateSetListener()
{
// when dialog box is closed, below method will be called.
public void onDateSet(DatePicker view, int selectedYear, int selectedMonth, int selectedDay)
{
cors_year = selectedYear;
cors_month = selectedMonth;
cors_date = selectedDay;
getDate("calledfromDialog");
displaySunTime();
displayChoghadiya("Day");
displayTime(3, 1);
dayButtonClicked();
selected_DAY_NIGHT="Day";
}
};
Note that onCreateDialog was deprecated in v13, per the documentation:
Use the new DialogFragment class with FragmentManager instead; this is also available on older platforms through the Android compatibility package.
Which you would use along with this tutorial
However, if you want to stay with the DatePickerDialog, you can use code such as:
protected void onPrepareDialog (int id, Dialog dialog)
{
DatePickerDialog datePickerDialog = (DatePickerDialog) dialog;
// Get the current date
datePickerDialog.updateDate(year, month, day);
}
This is because Android only calls onCreateDialog once per dialog to re-use the dialog. onPrepareDialog is called in order for you to set the state of the Dialog appropriately before it is shown.
I created an activity which is show google map on the screen, and consists of 3 option menus to interact with user selection.
When user press on the sub menu I want to show pop-up input dialog box that user can enter date(dd/mm/yyyy) or pop-up date picker. I will use that date to query the location which kept at that date then mark on the map.
My problem is when I tried to created input dialog or whatever inside my sub menu, nothing show on screen except map view. I think it may have something special to do like this. So, I searched for many days, but still do not get an answer.
Any one have an idea or did this ?
Please, suggest me. This is my first time with google map. Appreciate every answer.
Your problem is not specifically with the MapActivity. I have an app that has this feature (a MapActivity subclass that shows a date picker) and haven't had any problems. I suspect the issue lies with how you are showing your date picker. Perhaps try it in an plain activity (ie one that doesn't derive from MapActivity) to confirm this.
So in your class that extends the MapActivity you will need something like:
// the id for your dialog
static final int DATE_DIALOG_ID = 0;
// the date from the picker
Date mDate;
// so you are defining a member variable mDateSetListener which implements
// onDateSet. This gets called when the user selects a date.
private DatePickerDialog.OnDateSetListener mDateSetListener =
new DatePickerDialog.OnDateSetListener() {
public void onDateSet(DatePicker view, int year,
int monthOfYear, int dayOfMonth) {
Calendar cal = GregorianCalendar.getInstance();
cal.set (year, monthOfYear, dayOfMonth);
// store the date that was picked
mDate = cal.getTime();
// update your map
// ...
}
};
#Override
protected android.app.Dialog onCreateDialog(int id) {
switch (id) {
case DATE_DIALOG_ID:
final Calendar c = Calendar.getInstance();
int year = c.get(Calendar.YEAR);
int month = c.get(Calendar.MONTH);
int day = c.get(Calendar.DAY_OF_MONTH);
return new DatePickerDialog(this,
mDateSetListener,
year, month, day);
}
return null;
}
#Override public boolean onMenuItemSelected(int featureId, MenuItem item)
{
switch(item.getItemId())
{
// handle your date selection menu item
case R.id.date:
showDialog(DATE_DIALOG_ID);
break;
}
}
in my application i have to date picker as From date and To date. I am using onCreateDialog() method. So first time i am selecting the dates from date picker and putting them in the edit text. then when again i am selecting the date from the date picker, in the date picker it is showing the date which i already selected. But if i move to other search in the same screen(Activity) and try to pick the date from date picker, date picker still is showing the previous date. not the current date.
protected Dialog onCreateDialog(int id) {
final Calendar c = Calendar.getInstance();
switch (id) {
case DATE_DIALOG_ID1:
return new DatePickerDialog(this, mDateSetListener, c.get(Calendar.YEAR),
c.get(Calendar.MONTH), c.get(Calendar.DAY_OF_MONTH));
case DATE_DIALOG_ID2:
return new DatePickerDialog(this, mDateSetListener2, c.get(Calendar.YEAR),
c.get(Calendar.MONTH), c.get(Calendar.DAY_OF_MONTH));
}
return null;
}
May be you can try writing your question clearly once again, but to my understanding of your question you might want to look at your onDateChanged() callback method. I assume you have a updateDialog() method inside this onDateChanged(), you might want to check that method for err's.
I suppose you looked this link where they showed how to use a date picker http://developer.android.com/resources/tutorials/views/hello-datepicker.html
good luck
For the 2nd question you asked about mixup in from and to date dialog
From what you are saying, I think there something like a small error like
1) R.id.datepicker2 being used in place of R.id.datepicker1
or
2) having mixs up in onDateChangedListener(). I am sure there is better way to do it, but try checking whether you have like this below 2 different listeners
final Calendar c = Calendar.getInstance();
mYear = c.get(Calendar.YEAR);
mMonth = c.get(Calendar.MONTH);
mDay = c.get(Calendar.DAY_OF_MONTH);
DatePicker fromDatePicker = (DatePicker) findViewById(R.id.datePicker1);
datePicker.init(mYear, mMonth, mDay, new OnDateChangedListener() {
#Override
public void onDateChanged(DatePicker view, int year,
int monthOfYear, int dayOfMonth) {
//things you want to happen when datepicker1 is changed in your case the From date field
}
});
DatePicker toDatePicker = (DatePicker) findViewById(R.id.datePicker1);
datePicker.init(mYear, mMonth, mDay, new OnDateChangedListener() {
#Override
public void onDateChanged(DatePicker view, int year,
int monthOfYear, int dayOfMonth) {
//things you want to happen when datepicker2 is changed in your case the To date field
}
});