Custom DatePicker buttons - android

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

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.

Android - Handling event on an add button issue

I want to insert text from field into a String array every time the add button is pressed, and when done so, the field should empty itself for a new string.
Here is my code:
Editor note: no code provided as example, only a screenshot of the code
Is the add function ever called? Otherwise you are sitting with a button without an onClickListener.
Try assigning The onClickListener in oncreate instead.

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

TimePicker widget not getting current time

This question is about TimePicker behavior in Android.
To get the value of the Hour and the Minute I use getCurrentHour() and getCurrentMinute().
At first I found out that changes made by the user using the phone keyboard where not registered so calling getCurrentHour() or getCurrentMinute() did not show the values changed in the texboxes inside the widget.
This was quickly solved using setAddStatesFromChildren(true); With this I get those changes.
Now, my problem is that those changes are only registered if timepicker looses focus.
So, getting the value from timepicker inside a button gets a wrong value because it hasn't loose focus.
Any help?
What I need is: Can I force a timepicker to loose focus and get the real written value in text boxes?
I solved it and I write it here for someone who needs it.
Thanks to "Tarun Maheshwari" I found what I needed:
On onCreate do something like this:
MyTimerPicker.setAddStatesFromChildren(true);
On the onClick method, when getting the values, do this:
MyTimerPicker.clearFocus();
int iHour = MyTimerPicker.getCurrentHour();
int iMinute = MyTimerPicker.getCurrentMinute();
Call clearFocus() on the timepicker view after you get onTimeChanged callback.

Changing the title on a TimePicker?

I'm trying to look for a solution to change the title on a TimePicker dialog. Right now it says whatever the system time is (ex. "12:23 AM") but I want to change this to something a little more descriptive. Does anyone have any suggestions?
By request. :)
By TimePicker dialog do you mean the
actual TimePickerDialog? Because that
has a setTitle(CharSequence text)
method. The TimePickerDialog is what
is used in the official tutorial.
http://developer.android.com/reference/android/app/AlertDialog.html#setTitle(java.lang.CharSequence)
This question is rather old, but it shows up on Google results if you search this question. So, I thought I'll post my solution.
DatePicker has setCustomTitle(View view), where you can define your own view (e.g. TextView with your custom text) to be used as a title. This one does not update when changing values.
I dont know how to reply to the comments below question, so I use "Answer".
By looking into source code: SourceCode
You may realize that OnTimeChangedListener is implemented by TimePickerDialog.
To avoid title changed while adjusting time, you may derived from TimePickerDialog and override public void onTimeChanged(TimePicker view, int hourOfDay, int minute);
Notice, dont call super version, or it will setTitle again...

Categories

Resources