How to create input dialog or date picker on map view? - android

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;
}
}

Related

Android DatePickerDialog Max Date visual bug

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.

Show 1980 as initial Year in Date Picker

How to show 1980 as initial Year in DatePicker dialog, still i am getting initial date as current date
see my code below, and let me know where i have to do changes in my code
#Override
protected Dialog onCreateDialog(int id) {
switch (id) {
case DIALOG_DATE:
return new DatePickerDialog(this, new OnDateSetListener() {
#Override
public void onDateSet(DatePicker view, int year,
int monthOfYear, int dayOfMonth) {
dateTime.set(year, monthOfYear, dayOfMonth);
editDate.setText(dateFormatter
.format(dateTime.getTime()));
}
}, dateTime.get(Calendar.YEAR),
dateTime.get(Calendar.MONTH),
dateTime.get(Calendar.DAY_OF_MONTH));
}
return null;
That is because the last 3 parameters on the DatePickerDialog constructor are the date to be shown initially in the DatePicker. You are setting the dateTime variable to the current day with
private Calendar dateTime = Calendar.getInstance();
So you must set your dateTime variable to the correct date you want to be shown on the first run. In your OnCreate() method do this
dateTime.set(1980, Calendar.JANUARY , 1);
Now your DatePicker should show the correct value on the first run and since you set the dateTime variable in the callback it should set the correct previous value if the user opens up the dialog again.

Trying to return a date from android date picker

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!

DatePicker date set in android

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.

Android Date Picker help!

i need a little help here.
In my app a user need`s select one date interval (when press one button), and i'm not know how to do this using date picker!
i'm following this example: http://developer.android.com/resources/tutorials/views/hello-datepicker.html , but i have two questions about this tutorial...
1) How can i do which onClick event called DatePickerDialog.OnDateSetListener ( in the case where i have two EditText that when user click in wich one, the app shows the Date picker Dialog)
2) How can i call the onClick two times after user press one button?
tnhx!
Sorry for poor English!
So from your explanation, you want to create a date interval picker from just one click on a button.
By using analogy from your example you have to do the following:
// create two constants
static final int DATE_DIALOG_FROM = 0;
static final int DATE_DIALOG_TO = 1;
// create case for two pickers
protected Dialog onCreateDialog(int id) {
switch (id) {
case DATE_DIALOG_FROM:
return new DatePickerDialog(this, fromDateSetListener, mYear, mMonth,mDay);
case DATE_DIALOG_TO:
return new DatePickerDialog(this, toDateSetListener, mYear, mMonth,mDay);
}
return null;
}
// create two listeners for both of the cases
private DatePickerDialog.OnDateSetListener fromDateSetListener = new DatePickerDialog.OnDateSetListener() {
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
Toast.makeText(AccountsActivity.this,"From:" + year+"."+monthOfYear+"."+dayOfMonth, Toast.LENGTH_SHORT).show();
showDialog(DATE_DIALOG_TO);
}
};
private DatePickerDialog.OnDateSetListener toDateSetListener = new DatePickerDialog.OnDateSetListener() {
public void onDateSet(DatePicker view, int year, int monthOfYear,int dayOfMonth) {
Toast.makeText(AccountsActivity.this,"To:" + year+"."+monthOfYear+"."+dayOfMonth, Toast.LENGTH_SHORT).show();
updateDisplay();
}
};
When you run your app, second picker will appear right after finishing with first.
Later you can optimize this code, add custom pickers etc...
If anyone knows a better way to create a DATE INTERVAL PICKER please let us know!

Categories

Resources