Toast Message Showing Continiously when Rapidly Click Button - xamarin

Here I am showing toast message in Xamarin Forms Android
My Code:
Inside my Android Class
public void ShortAlert(string message)
{
Toast.MakeText(Forms.Context, message, ToastLength.Short).Show();
}
I am showing this toast message when user click on A Button.But When user rapidly click the button then it will shoing continiously(5 times button click showung toast 5 times).
so I want if the user click on this button second time then previous toast should be cancel.
How to do this in Xamarin form android?

You can try cancelling the Toast than it wont show on screen for a long time
public void ShowToast(string message)
{
if (objToast!= null)
{
objToast.Cancel();
}
objToast = Toast.MakeText(Forms.Context, message, ToastLength.Short);
objToast.Show();
}
Declare objToast on class level like this
private Toast objToast;

You could add the effective time of a click, for example, only the first click is valid for multiple clicks within 3 seconds,so you could change the code like this :
public static int MIN_CLICK_DELAY_TIME = 3000;//the effective time
private long lastClickTime = 0;
public void ShortAlert(string message)
{
long currentTime = Calendar.Instance.TimeInMillis;
if (currentTime - lastClickTime > MIN_CLICK_DELAY_TIME)
{
lastClickTime = currentTime;
Toast.MakeText(Forms.Context, message, ToastLength.Short).Show();
}
}

Related

nsmenufx MenuToolkit.setApplicationMenu producing inconsistent behavior

I have a JavaFX application for Mac and I'm using NSMenuFX to create the menu bar. I create my application menu using MenuToolkit. If I use the method setApplicationMenu the menu displays correctly. For example it says Quit MyApplication instead of Quit com.example.MyApplication. However, I have code that cancels the closing of the application if certain conditions are met. I have an EventHandler on the Stage that handles the on close request. If the application should not close the WindowEvent is consumed. The problem is the application still closes. However, if I do not use setApplicationMenu, the menu does not display correctly (it says Quit com.example.MyApplication) but consuming the WindowEvent does stop the closing of the application. I am using Java 1.8u77. Any ideas on what I'm doing incorrectly? I cannot reproduce this problem in the sample code that comes with NSMenuFX. Below is the code that creates the menu bar.
private void createMenu(VBox appBox) {
// Create the menubar
MenuBar menuBar = new MenuBar();
menuBar.useSystemMenuBarProperty().set(true);
MenuToolkit tk = MenuToolkit.toolkit();
String appName = "MyApplication";
Menu appMenu = new Menu(appName);
menuBar.getMenus().add(appMenu);
MenuItem aboutItem = tk.createAboutMenuItem("MyApplication");
aboutItem.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent event) {
AboutDialog aboutDialog = new AboutDialog(null);
aboutDialog.initOwner(stage);
aboutDialog.showAndWait();
}
});
appMenu.getItems().add(aboutItem);
appMenu.getItems().addAll(new SeparatorMenuItem(),
tk.createHideMenuItem(appName), tk.createHideOthersMenuItem(),
tk.createUnhideAllMenuItem(),
new SeparatorMenuItem(), tk.createQuitMenuItem(appName));
tk.setApplicationMenu(appMenu);
// Add the File menu
Menu file = new Menu("File");
menuBar.getMenus().add(file);
// Add the Print item
print = new MenuItem("Print");
print.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent event) {
PrinterJob job = PrinterJob.createPrinterJob();
if (job != null && job.showPrintDialog(
lineChart.getChart().getScene().getWindow())) {
boolean success = job.printPage(lineChart.getChart());
if (success) {
job.endJob();
}
}
});
file.getItems().add(print);
// Window Menu
Menu windowMenu = new Menu("Window");
windowMenu.getItems().addAll(
tk.createMinimizeMenuItem(), tk.createZoomMenuItem(),
tk.createCycleWindowsItem(), new SeparatorMenuItem(),
tk.createBringAllToFrontItem());
menuBar.getMenus().add(windowMenu);
tk.autoAddWindowMenuItems(windowMenu);
tk.setGlobalMenuBar(menuBar);
}
NSMenuFX has been modified to correct this problem. Look for version 2.1.4 or higher.

launch activity while ConfirmationActivity is displayed

I have an activity that launches a confirmationActivity onclick(). I want that, while the confirmation activity is displayed, a new activity is launched so that when the confirmation activity ends, the new activity is displayed.
#Override
public void onTimerFinished(View view) {
//delayed confirmation
Intent intent = new Intent(getActivity().getBaseContext(), ConfirmationActivity.class);
intent.putExtra(ConfirmationActivity.EXTRA_ANIMATION_TYPE,
ConfirmationActivity.SUCCESS_ANIMATION);
intent.putExtra(ConfirmationActivity.EXTRA_MESSAGE,
"Run Saved!");
startActivity(intent);
Intent mainIntent = new Intent(getActivity(), WearUploadRunActivity.class);
startActivity(mainIntent);
mResults.finishIt();
end = true;
}
my code is like this atm. In this case, the confirmation activity is not displayed because the parent activity(mResults) is killed before it is displayed...
Can someone help me with this issue? I just want to activity A launch confirmationActivity and then when the confirmationactivity ends the activity B is displayed.
EDIT:
#Override
public void onTimerSelected(View v) {
if (animation == false) {
mDelayedView.start();
mDelayedView.setListener(this);
animation = true;
} else {
mDelayedView.reset();
animation = false;
}
}
You shouldn't start both activities as you are doing there. You should start your ConfirmationActivity and then, you can use the callbacks onTimerFinished() or onTimerSelected() of the ConfirmationActivity to trigger the start of the second activity.

How to get user input from a popup control

I have a UserControl that utilizes a popup window in wp7. The user control has a text box for input, and a submit button. My issue is that the code does not halt once the popup is shown. It continues on through the code and does not wait for the user to press submit.
What is a good practice for making the code "halt" similar to a message box with an "Okay" button?
//my custom popup control
InputBox.Show("New Highscore!", "Enter your name!", "Submit");
string name = InputBox.GetInput();
//it does not wait for the user to input any data at this point, and continues to the next piece of code
if (name != "")
{
//some code
}
You could accomplish this with either an event, or an async method. For the event you would subscribe to a Closed event of your popup.
InputBox.Closed += OnInputClosed;
InputBox.Show("New Highscore!", "Enter your name!", "Submit");
...
private void OnInputClosed(object sender, EventArgs e)
{
string name = InputBox.Name;
}
You would fire the event when the user pushes the OK button
private void OnOkayButtonClick(object sender, RoutedEventArgs routedEventArgs)
{
Closed(this, EventArgs.Empty);
}
The other option is to use an async method. For this you need the async Nuget pack. To make a method async you use two main objects, a Task and a TaskCompletionSource.
private Task<string> Show(string one, string two, string three)
{
var completion = new TaskCompletionSource<string>();
OkButton.Click += (s, e) =>
{
completion.SetResult(NameTextBox.Text);
};
return completion.Task;
}
You would then await the call to the show method.
string user = await InputBox.Show("New Highscore!", "Enter your name!", "Submit");
I believe the Coding4Fun toolkit also has some nice input boxes

How to dismiss a Alert Dialog in Mono for android correctly?

In my application i have a Custom AlertView, which works quite good so far. I can open it the first time, do, what i want to do, and then close it. If i want to open it again, i'll get
java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first
so, here some code:
public Class ReadingTab
{
...
private AlertDialog AD;
...
protected override void OnCreate(Bundle bundle)
{
btnAdd.Click += delegate
{
if (IsNewTask)
{
...
AlertDialog.Builer adb = new AlertDialog.Builer(this);
...
View view = LayoutInflater.Inflate(Resource.Layout.AlertDView15ET15TVvert, null);
adb.setView(view)
}
AD = adb.show();
}
}
}
that would be the rough look of my code.
Inside of btnAdd are two more buttons, and within one of them (btnSafe) i do AD.Dismiss() to close the Alert dialoge, adb.dispose() hasn't done anything.
the first time works fine, but when i call it the secon time, the debugger holds at AD = adb.show(); with the Exception mentioned above.
So what do i have to do, to remove the Dialoge from the parent? i can't find removeView() anywhere.
If you are setting up an AlertView once and then using it in multiple places (especially if you are using the same AlertView across different Activities) then you should consider creating a static AlertDialog class that you can then call from all over the place, passing in the current context as a parameter each time you want to show it. Then when a button is clicked you can simply dismiss the dialog and set the instance to null. Here is a basic example:
internal static class CustomAlertDialog
{
private static AlertDialog _instance;
private const string CANCEL = #"Cancel";
private const string OK = #"OK";
private static EventHandler _handler;
// Static method that creates your dialog instance with the given title, message, and context
public static void Show(string title,
string message,
Context context)
{
if (_instance != null)
{
throw new Exception(#"Cannot have more than one confirmation dialog at once.");
}
var builder = new AlertDialog.Builder(context);
builder.SetTitle(title);
builder.SetMessage(message);
// Set buttons and handle clicks
builder.SetPositiveButton(OK, delegate { /* some action here */ });
builder.SetNegativeButton(CANCEL, delegate { /* some action here */});
// Create a dialog from the builder and show it
_instance = builder.Create();
_instance.SetCancelable(false);
_instance.Show();
}
}
And from your Activity you would call your CustomAlertDialog like this:
CustomAlertDialog.Show(#"This is my title", #"This is my message", this);

How do I mask the current page behind a modal dialog box in vanilla GWT?

I've built a log-in composite that I am displaying in my application entry-point to the user. Upon entry of the username and password, I am sending the username and password to the server via a RemoteService and will receive back an object containing the ClientSession. If the ClientSession is a valid object (recognised username and password), I wish to display the main application panel otherwise I want to display the login dialog again (with an error message).
My question is, that during the async call to the server, how to I mask the screen so that the user cannot click anything whilst the Session is obtained from the server?
I know that the login should be fast, but the Session object contains a lot of Client Side cached values for the current user that is used to generate the main panel. This may take a fraction of a second or up to 5 seconds (I can't control the speed of the underlying infrastructure unfortunately) so I want to mask the screen until a timeout is reached then allow the user to try again.
I have done this exact operation before using GWT Ext, but vanilla GWT seems to have a lot less samples unfortunately.
Thanks
Chris
The GWT class PopupPanel has an optional "glass panel" that blocks interaction with the page underneath.
final PopupPanel popup = new PopupPanel(false, true); // Create a modal dialog box that will not auto-hide
popup.add(new Label("Please wait"));
popup.setGlassEnabled(true); // Enable the glass panel
popup.center(); // Center the popup and make it visible
You might want to check out GlassPanel from the GWT Incubator project. AFAICT it's not perfect, but should be of some help nevertheless ;)
You can also use a dialog box for this purpose.
Here is the code how to use it.
public class NTMaskAlert extends DialogBox {
private String displayText;
private String message;
private static NTMaskAlert alert;
Label lable;
private NTMaskAlert(String text) {
setText(text);
setWidget(new Image(GWT.getModuleBaseURL()
+ "/images/ajax-loader_1.gif"));
setGlassEnabled(true);
setAnimationEnabled(true);
super.show();
super.center();
WorkFlowSessionFactory.putValue(WorkFlowSesisonKey.MASKING_PANEL, this);
}
public static void mask(String text) {
if (text != null)
new NTMaskAlert(text);
else
new NTMaskAlert("Processing");
}
public static void unMask() {
NTMaskAlert alert = (NTMaskAlert) WorkFlowSessionFactory
.getValue(WorkFlowSesisonKey.MASKING_PANEL);
if (alert != null) {
alert.hide();
alert = null;
}
}
public void setDisplayText(String displayText) {
this.displayText = displayText;
alert.setText(displayText);
}
public String getDisplayText() {
return displayText;
}
public void setMessage(String message) {
this.message = message;
lable.setText(message);
}
public String getMessage() {
return message;
}
}
Use static mask and unmask method for operations.
This is my solution:
public class CustomPopupPanel extends PopupPanel {
private Label label = new Label();
public CustomPopupPanel() {
super(false, true); // Create a modal dialog box that will not auto-hide
super.setGlassEnabled(true); // Enable the glass panel
super.add(label); // Add the widget label into the panel
}
public CustomPopupPanel(String text) {
this();
this.mask(text);
}
public final void mask(String text) {
label.setText(text);
super.center(); // Center the popup and make it visible
}
public void unmask() {
super.hide(); // Hide the popup
}
}

Resources