Call method when date is selected in datepicker - datepicker

I want to call a method when the user select a date on a datepicker. I don't know how to do that, there is no onDateSet method. Should I use a listener? Please help me.
EDIT: Here's the code regarding the datepicker:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
dp=(DatePicker)findViewById(R.id.dp);
c = Calendar.getInstance();
Month =c.get(Calendar.MONTH)+1;
Day = c.get(Calendar.DAY_OF_MONTH);
Year = c.get(Calendar.YEAR);
dp.init(c.get(Calendar.YEAR),(c.get(Calendar.MONTH)+1), Day, null);
Now, this view is in the main layout of my app, it have no buttons, just the datepicker with rolling number. I want to call a method when the user roll a different number on this datepicker. There is no positive button or any button at all, so the answer wouldn't work for me...

_datePickerDialog.setButton(DialogInterface.BUTTON_POSITIVE, "Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface arg0, int arg1) {
_done = true;
DatePicker datePicker = _datePickerDialog.getDatePicker();
_datePickerDialogCallback.onDateSet(datePicker, datePicker.getYear(), datePicker.getMonth(), datePicker.getDayOfMonth());
}
});
here is the link to the original article

Related

How to finish editing in DatePicker on Android TV if we control the remote control

I have a dialog that has two labels, a DatePicker, and two Back and Apply buttons. By default, the focus belongs to the "Apply" button. We start editing the DatePicker by pressing the up button on the remote. We have set the date and want to go down to the "Apply" button. But this is not possible, because with DatePicker you can not go to the button, because when you press the "Down" button on the remote, the date changes. I would like that after setting the date, I would press the Enter button on the remote control and focus would be given to the "Apply" button. Is it possible?
Below is the code of the dialog:
public class DateSelector {
public static void showSubscriptionDateDialog(Context context, long expiredAt, DateSetListener listener) {
Dialog dialog = new Dialog(context, R.style.DialogTheme);
dialog.setContentView(R.layout.dialog_subscription_date);
DatePicker datePicker = dialog.findViewById(R.id.date_picker);
TextView dateLabel = dialog.findViewById(R.id.subscription_date);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(expiredAt);
AtomicInteger dayForSave = new AtomicInteger();
dayForSave.set(calendar.get(Calendar.DATE));
AtomicInteger monthForSave = new AtomicInteger();
monthForSave.set(calendar.get(Calendar.MONTH));
AtomicInteger yearForSave = new AtomicInteger();
yearForSave.set(calendar.get(Calendar.YEAR));
setDateLabel(yearForSave, monthForSave, dayForSave, dateLabel);
Button buttonBack = dialog.findViewById(R.id.button_back);
buttonBack.setOnClickListener(v -> {
dialog.dismiss();
});
Button buttonClear = dialog.findViewById(R.id.button_clear);
buttonClear.setOnClickListener(v -> {
calendar.set(yearForSave.get(), monthForSave.get(), dayForSave.get());
listener.onDateSelected(calendar.getTimeInMillis());
dialog.dismiss();
});
datePicker.init(yearForSave.get(), monthForSave.get(), dayForSave.get(),
(view, year1, monthOfYear, dayOfMonth) -> {
yearForSave.set(year1);
monthForSave.set(monthOfYear);
dayForSave.set(dayOfMonth);
setDateLabel(yearForSave, monthForSave, dayForSave, dateLabel);
});
buttonClear.requestFocus();
dialog.show();
}
private static void setDateLabel(AtomicInteger yearForSave, AtomicInteger monthForSave,
AtomicInteger dayForSave, TextView dateLabel) {
Calendar calendar = Calendar.getInstance();
Locale locale = Locale.getDefault();
SimpleDateFormat dateFormat = new SimpleDateFormat("EE, d MMMM, yyyy", locale);
calendar.set(yearForSave.get(), monthForSave.get(), dayForSave.get());
String expirationDate = dateFormat.format(calendar.getTime());
dateLabel.setText(expirationDate);
}
public interface DateSetListener {
void onDateSelected(long expiredAt);
}
}
I have found a solution to this problem. You can hang the listener on the dialog and catch the click of the Apply button. Here is the code that solves this problem:
dialog.setOnKeyListener((dialog1, keyCode, event) -> {
if (buttonApply.hasFocus() || buttonBack.hasFocus()) return false;
if (event.getAction() == KeyEvent.ACTION_DOWN) {
if (keyCode == KeyEvent.KEYCODE_DPAD_CENTER) {
buttonApply.requestFocus();
return true;
}
}
return false;
});

GWTBootstrap - Masking Datepicker

Requirement : Mask the datepicker on blur / form submit.
I was able to get the masking done onBlur with forceparse set to false but i'm unable to navigate to the next month/year in the datepicker maybe because i'm overriding the DOM handlers.
dob.setForceParse(false);
dob.addDomHandler(new BlurHandler() {
#Override
public void onBlur(BlurEvent event) {
dob.getTextBox().setText(**masked value**);
}
}, BlurEvent.getType());
dob.addDomHandler(new FocusHandler() {
#Override
public void onFocus(FocusEvent event) {
dob.setValue(**unmasked value**);
}
}, FocusEvent.getType());
I am unable to use ChangeDateHandler, since it automatically assumes "1/1/19" as "1/1/1900" and masks the text mid-way of text enter. Is there any other way i can handle this?
If you are having problems with DatePicker's TextBox you can just hide it and use any other Widget instead.
TextBox is basically used to show the DatePicker when is focused, to show selected date and to enter the date manually.
Here is an example code how to use your own TextBox with DatePicker (I use VerticalPanel to show DatePicker just below the TextBox):
VerticalPanel container = new VerticalPanel();
final DatePicker dob = new DatePicker();
// hide DatePicker's TextBox
dob.getTextBox().getElement().getStyle().setDisplay(Display.NONE);
final TextBox box = new TextBox();
box.addFocusHandler(new FocusHandler() {
#Override
public void onFocus(FocusEvent event) {
dob.show();
box.setText("**unmasked value**");
}
});
dob.addHideHandler(new HideHandler() {
#Override
public void onHide(HideEvent hideEvent) {
box.setText("**masked value**");
}
});
dob.addChangeDateHandler(new ChangeDateHandler() {
#Override
public void onChangeDate(ChangeDateEvent evt) {
Window.alert("ChangeDateEvent");
}
});
container.add(box);
container.add(dob);
Just notice, that you need to call dob.show() when TextBox is focused. I also use DatePicker's HideHandler instead of TextBox's BlurHandler to show masked value but you may change it as you wish.
If you want to change the way of parsing dates like "1/1/19", now you can add your own parser to the TextBox.

Firing ListGrid selection item on GWT

When the user clicks a button, I want to fire the ListGrid Selection event. I called "resultControl.resultGrid.selectRecord(0);" but it didn't work.
From your initial question and your comment, I understand that you want to simulate a selection event in your ListGrid, through a button. Assuming that I understand well, and you are only interested in one record selection (the first one), all you have to do is the following:
final ListGrid listGrid = new ListGrid();
//Initialize your listgrid's data etc.
listGrid.addSelectionChangedHandler(new SelectionChangedHandler() {
#Override
public void onSelectionChanged(SelectionEvent event) {
SC.say("here my code");
}
});
IButton button = new IButton("Select");
button.addClickHandler(new ClickHandler() {
#Override
public void onClick(ClickEvent event) {
listGrid.selectRecord(0);
}
});
A last note, System.out or System.err won't produce anything when your application runs in production mode. Use a suitable logging solution or the SC.say(), if you want to provide the user with a message, instead.

how to pop up calendar when button is clicked in Java SWT?

I am trying to develop Java SWT application in eclipse.
I need to populate text box using DateTime Calendar in SWT when a button is clicked.
I tried the following code but not able to see the Calendar, though it is created.
Any help would be appreciated.
Thanks
public void createPartControl(final Composite parent) {
Button button;
Label label;
final Display dev = parent.getDisplay();
Image image = new Image(dev,"C:\\Users\\rm186021\\Desktop\\Calendar.gif");
GridLayout gridLayout = new GridLayout();
gridLayout.numColumns = 3;
parent.setLayout(gridLayout);
label = new Label(parent, SWT.NULL);
label.setText("Start date ");
final Text start = new Text(parent, SWT.SINGLE | SWT.BORDER);
Button calButton = new Button(parent, SWT.PUSH);
calButton.setImage(image);
calButton.addSelectionListener(new SelectionAdapter() {
#Override
public void widgetSelected(SelectionEvent e) {
final Display display = new Display();
final Shell shell2 = new Shell(display);
shell2.addListener(SWT.CALENDAR, new Listener() {
public void handleEvent(Event event) {
final DateTime calendar = new DateTime(shell2,SWT.CALENDAR | SWT.POP_UP);
calendar.addSelectionListener (new SelectionAdapter () {
public void widgetSelected (SelectionEvent e) {
start.setData(" " + calendar.getYear() + "-" + (calendar.getMonth() + 1) + "-" + calendar.getDay());
System.out.println(start.getData());
//calendar.dispose();
}
});
}
});
}
});
You're creating a Shell, but never even opening it. Try calling shell2.open().
You're adding an SWT.CALENDAR listener to the Shell. This isn't going to do what you want to do. Or anything, for that matter, since Shell doesn't fire SWT.CALENDAR events. Instead, you simply need to add the DateTime to a container and hook up selection listeners to the Calendar.
SWT.POP_UP is not an appropriate style bit for Calendar.
I would recommend subclassing Dialog (call it CalendarDialog, for example), setting a FillLayout on it, adding a Calendar to it and hooking up listeners that way. Then call CalendarDialog.open().
The DateTime really shouldn't be created with code like that :) Try this instead:
calButton.addSelectionListener(new SelectionAdapter() {
#Override
public void widgetSelected(SelectionEvent e) {
final Shell shell2 = new Shell(dev.getActiveShell());
// new Display() won't work on many platforms if one already exists
final DateTime calendar = new DateTime(shell2, SWT.CALENDAR);
// no need to add a listener to shell2, and POP_UP doesn't work for DateTime
calendar.addSelectionListener(...);
shell2.open();
// Edward Thomson noticed it wasn't called, I missed it
}
};

GWT DatePicker to recognize Calendar Icon click

I'm using a DatePicker widget, and I put a little calendar image next to it. When you click on the actual DatePicker textbox, the calendar popup pops up (as it should). I'm looking to add this click handling to the little calendar image too, so that the user can click either the box or the image to see the calendar popup. Is there an easy way to do this?
I thought it might be something like this:
calendarImage.addClickHandler(datePick.getTextBox().getClickHandler())
But it doesn't seem that anything like this exists.
Thanks in advance!
Just add a clickhandler to the image that sets the datepicker visible.
It will become something like this:
private DatePicker datePicker = new DatePicker();
private TextBox textBox = new TextBox();
private Image icon = new Image("calendar.png");
public void onModuleLoad() {
datePicker.addValueChangeHandler(new ValueChangeHandler<Date>() {
#Override
public void onValueChange(ValueChangeEvent<Date> event) {
Date date = event.getValue();
String dateStr = DateTimeFormat.getFormat(DateTimeFormat.PredefinedFormat.DATE_MEDIUM).format(date);
textBox.setText(dateStr);
datePicker.setVisible(false);
}
});
datePicker.setVisible(false);
icon.addClickHandler(new ClickHandler() {
#Override
public void onClick(ClickEvent event) {
datePicker.setVisible(true);
}
});
RootPanel.get().add(icon);
RootPanel.get().add(textBox);
RootPanel.get().add(datePicker);
}
Assuming you mean DateBox when you write DatePicker, simply call showDatePicker on click of the icon (and/or test isDatePickerShowing and call hideDatePicker)