Unselect a selected date in CalendarView in Kotlin - android

Some of the shown dates in my calendar view shouldn't be selected. If some of them are pressed I show an AlertDialog that explains the reason why it cannot be selected.
I want to achieve that, apart from that alertDialog showing, the selected Date that has caused it to open automatically unselects.
In kotlin pseudocode, it would be something like this for my app.
calendar_view.onDateClickListener = { date ->
[...]
[Condition that shows AlertDialog with showDialog()];calendar_view.getLastSelectedDate().unSelect()
[...]
fun showDialog()
The functions I've declared to try to get the last selected day from calendar_view are made up, it's just so it's clear what my intention is.

Related

Date picker dialog button text change

I am using several languages in the app, so I need to switch the texts on the date picker buttons.
I'm writing a simple code that looks like this:
val datePickerDialog = builder.build()
datePickerDialog.setButton(DialogInterface.BUTTON_NEGATIVE, R.string.common_dialog_cancel)
datePickerDialog
I'm returning the dialog on the last line. The problem is that the second line identifies the errors I cannot resolve. "None of the following functions can be called with the arguments supplied."
As far as I know, alertDialog should have a method setButton() that takes a button and a string as arguments, but something is not right in my code. Any suggestions?
According to the docs there is no function that supports these 2 arguments. I guess you should use this one:
public void setButton (int whichButton,
CharSequence text,
DialogInterface.OnClickListener listener)
Link to docs
So you have to add a 3rd argument with the click listener.

Add option to given spinner through AlertDialog

My app consists of multiple spinners to allow users to quickly fill out a form. I am trying to add a feature where if the user cannot find the best answer in a spinner, there is option at the bottom of the spinner labeled "add answer".
I understand how to register when "add answer" is clicked, and open a AlertDialog with a TextView entry:
case R.id.C1Lspin:
if(C1LSpin.getSelectedItem() == "Add Option..."){
addOption(C1LAdapter); //call AlertDialog
}
break;
My issue is on return from the AlertDialog I am struggling to notify which list of spinner options should be updated. My instinct is to bundle the specific spinner's arrayAdapter so that when I override the AlertDialog positive button it can use the arrayAdapter that had been bundled.
Is there any way to bundle an arrayAdapter to be passed through an AlertDialog. I am also wondering if there is any other way to notify the override function of the AlertDialog so it know which Spinner arrayAdapter to update. I am planning on having about 10 spinners so I really don't want to create a specific AlertDialog for each spinner.
I ended up using a class reference variable.
I set the reference before I start the asynchronous process, then on return from the process the reference variable is grabbed, then used, and set to null to avoid any data leak.
Not fancy or sleek but for now it works.

Custom DatePicker buttons

I would like to highlight that I am looking for a Xamarin solution. I have found Java solutions, but I can't seem to convert them to Xamarin. I want to have the user set their birthday and then have the app calculate their age, and display it as text. I have everything functioning. All I am looking to do is change the text on the "ok" button to "Calculate age." I am able to change the text, but the button doesn't grab the date from the picker then. This is how I create my datepicker:
DatePickerDialog setDate = new DatePickerDialog(this, onDateSet, date.Year, date.Month-1, date.Day);
then I use this method to change the text
setDate.SetButton("calculate age", EventHandler<DialogClickEventArgs>)
I have created an EventHandler called Age use for the second argument. What code do I put in the EventHandler to make the button function like the "ok" button? if I set handler:null the method works, I can also get the method to do other things, change text in textbox for a example. Any solution is welcomed.
Wow. No love for Xamarin? I discovered the solution was much simpler. Following from what I learned here: Change DatePickerDialog's button texts. I began throwing darts into the code and this is what stuck. Where I set handler to null I simply set listener to the dialog object like so. setDate.SetButton ("calculate age", listener:setDate);

Robotium: let Solo click on DatePicker in localized build

I am performing a click on the "Set"-button in a DatePickerDialog with Robotium via
solo.clickOnButton("Set");
If I now change the language of the testing device to a different language, Robotium is not able to find the button, as the text is not "Set" anymore but the translated word.
Is there any possibility to access the button in the Picker in a different way?
As in Jelly Bean the DatePicker lost the "Cancel" button, I cannot use the clickOnButton(int index) method.
The only idea I have would be to use setButton on the DatePickerDialog to have access to the localized string resource of the button text or keep a reference to the button.
But maybe someone knows of a better way to gain access without the need of custom button text.
Regards
Kim
If you have access to the source code, you can use both getString() and getView():
Button button = (Button) solo.getView(R.id.x);
solo.clickOnView(button);
There is also solo.getString(R.string.x) that is good to use for localized builds.
I know that it's not the best solution but it works for me:
solo.clickOnButton(0);
Here's my suggestion (assuming you are showing the dialog via a DialogFragment): I have a SelectDateDialogFragment with a unique TAG and an onCreateDialog() method which creates a DatePickerDialog. I then show the dialog via selectDateDialogfragment.show(getFragmentManager(), SelectDateDialogFragment.TAG). In the Robotium tests, I use code like the following to click the dialog's buttons:
solo.clickOnView(editDateButton);
solo.waitForFragmentByTag(SelectDateDialogFragment.TAG);
solo.setDatePicker(0, 2000, 1, 1);
SelectDateDialogFragment dialogFragment = (SelectDateDialogFragment) activity.getFragmentManager()
.findFragmentByTag(SelectDateDialogFragment.TAG);
DatePickerDialog dialog = (DatePickerDialog) dialogFragment.getDialog();
Button okButton = dialog.getButton(DialogInterface.BUTTON_POSITIVE);
solo.clickOnView(okButton);
solo.waitForDialogToClose();
I would like to share some details with you.
First:
solo.clickOnButton(0);
worked well for me some time. But as the new Dialogs don't have the "Set" and "Cancel" buttons, but instead "Cancel" and "OK", this solution would now select the cancel button on newer devices while just switching to
solo.clickOnButton(1);
would break the test for older devices.
So I migrated to csoltenborn's solution with two modifications:
as I want to stay compatible with older devices I use the SupportFragmentManager
as my fragment is nested in another fragment depending on the device and it's orientation, I sometimes have to access a certain fragments ChildFragmentManager.
This is my solution, maybe it can add to csoltenborn's good answer:
DialogFragment dialogFrag;
Fragment outerFragment = getActivity().getSupportFragmentManager().findFragmentByTag("outerFragmentTAG");
if (outerFragment == null) {
dialogFrag = (DialogFragment)getActivity().getSupportFragmentManager().findFragmentByTag("datePicker");
} else {
dialogFrag = (DialogFragment)outerFragment.getChildFragmentManager().findFragmentByTag("datePicker");
}
Button okButton = ((DatePickerDialog)dialogFrag.getDialog()).getButton(DialogInterface.BUTTON_POSITIVE);
solo.clickOnView(okButton);

Android: Force recreation of dialog

I am using the android dateslider custom dialog class in order to let the user edit the date for several different rows of a table.
The dateslider lets you limit the user to only select dates between a minimum date and maximum date that you can specify.
Each table row requires the dateslider to limit the user to a different minimum date and maximum date, however because you specify the min and max dates inside the onCreateDialog method, I need to be able to dynamically modify these dates when the user clicks the row.
I have tried calling the onCreateDialog method again when the user clicks the dialog, and it is ran, however the new limits are not taken into account, suggesting that the originally created dialog is still used instead.
How would I go about achieving my goal?
Thanks,
Max.
If you need to change dialogs before you use them, you need to use onPrepareDialog.
Update:
The dialog that is passed in to onPrepareDialog is the dialog that was created in onCreateDialog. Modify it however you like (don't create a new one). You might have to add some setters to your custom dialog class:
protected void onPrepareDialog(int id, Dialog dialog) {
switch(id) {
case YOUR_DIALOG_ID:
YearMonthDayHourMinute myDialog = (YearMonthDayHourMinute) dialog;
myDialog.setInitialTime(initialTime);
myDialog.setMinTime(minTime);
myDialog.setMaxTime(maxTime);
break;
}
}

Categories

Resources