Monotouch Hide Keyboard when Navbar is tapped using MonoTouch.Dialog - xamarin.ios

I am currently using this code to hide the keyboard in a monotouch iOS application when something outside the input elements are tapped.
var tap = new UITapGestureRecognizer ();
tap.AddTarget (() =>{
dvc.View.EndEditing (true);
});
dvc.View.AddGestureRecognizer (tap);
However, I would like to hide the keyboard when the user taps the top Navbar as well. I have seen this in other apps. How would I go about doing that?

Easiest way is to override TouchesEnded in your UIViewController.
This will give you any touch event inside the entire controller.
Then do something like this if you need ignore touches in a certain view:
public override void TouchesEnded(NSSet touches, UIEvent evt)
{
base.TouchesEnded(touches, evt);
if (evt.TouchesForView(viewYouWantToIgnore) == null) {
//Dismiss your keyboard here
}
}

Related

Can you override the Navigation Controllers 'InteractivePopGestureRecognizer' action?

I'm searching for ways to implement a swipe gesture recognizer which only triggers when you swipe from the outer left side of the screen to the right. We need the gesture to open our custom SideMenu. I tried to use a simple UISwipeGestureRecognizer with the SwipeDirection property set to right, but that gets triggered on every swipe from left to right, no matter what the startpoint of the swipe is.
Ideally, we want the animation of it to look and feel like the InteractivePopGestureRecognizer of a UINavigationController. We are already using a NavigationController, which pushed our MainView over our IntroView. Now, we disable the InteractivePopGestureRecognizer, so you aren't able to go back to the IntroView. That's our problem. If it is possible, we don't want to disable the gesture of the NavigationController, but change the action of it. So the swipe from the far left side of the screen to the right would not pop the current viewcontroller, but open our SideMenu.
Is it possible to override the InteractivePopGestureRecognizer to change the action of it? If this isn't possible, do you have another idea on how to create the exact same gesture recognizer? It has to be possible somehow because many apps only open their SideMenu if your startpoint of the gesture is the left (or right) side of the screen. (e.g. Reddit)
Thanks for any help in advance.
You could use Touch Events and UISwipeGestureRecognizer to do that.
The workaround that is override TouchesBegan method to detect the started point whether fit your needs, and if so add UISwipeGestureRecognizer for View.
SwipeGestureRecognizer rightSwipeGesture;
public override void TouchesBegan (NSSet touches, UIEvent evt)
{
base.TouchesBegan (touches, evt);
UITouch touch = touches.AnyObject as UITouch;
if (touch != null)
{
//code here to handle touch
CoreGraphics.CGPoint swipPoint = touch.LocationInView(View);
if(swipPoint.X < 0.5)
{
rightSwipeGesture = new SwipeGestureRecognizer { Direction = SwipeDirection.Right };
rightSwipeGesture.Swiped += OnSwiped;
View.AddGestureRecognizers(rightSwipeGesture);
}
}
}
public override void TouchesEnded (NSSet touches, UIEvent evt)
{
base.TouchesBegan (touches, evt);
if(null != rightSwipeGesture ){
rightSwipeGesture.Swiped -= OnSwiped;
View.RemoveGestureRecognizers(rightSwipeGesture);
}
}
=============================Update=================================
I found a workaround only use one GestureRecognizer will make it works. You could have a look at UIScreenEdgePanGestureRecognizer. Although it's a Pan gesture, however if you not deal with somethind with the added view, it will work as a swip gesture. In addition, UIScreenEdgePanGestureRecognizer only can work when on the screen edge. You could set the Left edge to handle your needs.
For example:
UIScreenEdgePanGestureRecognizer panRightGestureRecognizer = new UIScreenEdgePanGestureRecognizer();
panRightGestureRecognizer.Edges = UIRectEdge.Left;
panRightGestureRecognizer.AddTarget(() => HandleSwap(panRightGestureRecognizer));
View.AddGestureRecognizer(panRightGestureRecognizer);
private void HandleSwip(UIScreenEdgePanGestureRecognizer panRightGestureRecognizer)
{
Point point = (Point)panRightGestureRecognizer.TranslationInView(View);
if (panRightGestureRecognizer.State == UGestureRecognizerState.Began)
{
Console.WriteLine("Show slider view");
}
}

Switch vs toggle

I'm trying to decide whether to use a switch or toggle for setting an alarm I'm my android application. On fairly new to android and don't know or quite understand all the ins and outs of the frame work. What would be the disadvantages of choosing to trigger the alarm with a switch over a toggle, or vice versa? Is there a slide toggle available in android framework?
The first thing you need to keep in mind is since which API do you want to compile your app?
Toggle Buttons are available since API 1 and Switches are only available since API 14.
Besides that is just a simple decision, which one is the best option for user interface/design
In my opinion Switches give a clear indication of what is currently selected.
Is there a slide toggle available in android framework?
Yes a switch can work as a slide toggle.
The code is very simple, you can basically use the same thought for these two buttons.
Here is an example of a Switch Button
btnSwitch = (Switch) findViewById(R.id.switch_1);
btnSwitch.setOnCheckedChangeListener(new OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {
if (isChecked) {
Log.i("Switch", "ON");
} else {
Log.i("Switch", "OFF");
}
}
});
Here's an example of a toggle Button
btnToggle = (ToggleButton) findViewById(R.id.toggle_1);
btnToggle.setOnCheckedChangeListener(new OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
Log.i("Toggle", "ON");
} else {
Log.i("Toggle", "OFF");
}
}
});

Display screen on button click in blackberry eclipse

I recently started creating an app for blackberry. I want the app to display a screen (Let's say it's called "InfoScreen") but as I said i am still very new to this. What code will I have to use if I want the "InfoScreen" to be show when the button "buttonInfo" is clicked?
Here is some code I used to create the button that is might relevant to this question.
//Create button
ButtonField buttonInfo = new ButtonField("Information", Field.FIELD_HCENTER);
//Center buttons
VerticalFieldManager vfm = new VerticalFieldManager(Field.USE_ALL_WIDTH);
vfm.add(buttonInfo);
add(vfm);
What code will have to be used for both touch-screen devices and those that use the track-pad?
Thanks
Try this:
FieldChangeListener infoListener = new FieldChangeListener() {
public void fieldChanged(Field field, int context) {
UiApplication.getUiApplication().pushScreen(new InfoScreen());
}
};
buttonInfo.setChangeListener(infoListener);

Is it possible to forbid selection in TextField?

I have a small problem: my text field keeps selecting itself, for example if I alt-tab from and to application.
For my application, text selection is not needed and will not be used - so I want to disallow this annoying behavior. Actually, just setting selection color to transparent or white will work fine.
Is there some way to do this?
The following css fixed the problem for me:
-fx-highlight-fill: null;
-fx-highlight-text-fill: null;
You can disable text selection for key events:
myTextField.addEventFilter(KeyEvent.KEY_TYPED, new EventHandler<KeyEvent>() {
#Override
public void handle(KeyEvent inputevent) {
if (!myTextField.getSelectedText().isEmpty()) {
myTextField.deselect();
}
}
});
For mouse events you could also use:
myTextField.addEventFilter(MouseEvent.MOUSE_DRAGGED, new EventHandler<MouseEvent>() {
#Override
public void handle(MouseEvent event) {
if (!myTextField.getSelectedText().isEmpty()) {
myTextField.deselect();
}
}
});
Super late for an answer, but I have a better solution.
Instead of disabling the style for selected text or managing mouse events, it's better to directly manage the selectedTextProperty().
The advantages of this solution are:
The selection properties will always be empty selection
Double clicks are also covered, not only mouse drag events
The code...
textField.selectedTextProperty().addListener((observable, oldValue, newValue) -> {
if (!newValue.isEmpty()) textField.deselect();
});

How to dismiss keyboard in case of a modal view?

Running on iPad.
I'm presenting a view controller modally. It is using MonoTouch.Dialog to show some text input fields, buttons, etc.
When tapping a textfield, the keyboard pops up. When tapping outside the textfield I would like to dismiss the keyboard but it won't go away. I read that dismissing the keyboard is not possible when presenting as FormSheet? Is that correct?
Here's my code. The selector gets called, but the keyboard stays where it is. As the textfields are created dynamicalls I cannot/don't want to call ResignFirstResponder() on each of them but instead let the view become first responder and then resign.
public override void ViewDidLoad ()
{
base.ViewDidLoad ( );
UITapGestureRecognizer oTapRecognizer = new UITapGestureRecognizer ( );
oTapRecognizer.AddTarget(this, new MonoTouch.ObjCRuntime.Selector("ViewTappedSelector:"));
this.View.AddGestureRecognizer ( o );
}
[Export( "ViewTappedSelector:" )]
public void ViewTapped ( UIGestureRecognizer sender )
{
this.View.BecomeFirstResponder ( );
this.View.ResignFirstResponder ( );
}
I read that dismissing the keyboard is not possible when presenting as FormSheet?
This is correct. If you'd like to dismiss the keyboard while showing a modal view, you'll have present it using a different modalStyle and you could resize the view to take the same frame as a formsheet.

Resources