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");
}
}
});
Related
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");
}
}
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
}
}
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();
});
I have the sample of softkeyboard. When I install it on a android 2.x, it has the following non-fullscreen view:
But when I install it on tablet 10in it gets whole of the screen like this:
How can I change the sample to have a non-fullscreen custom keyboard on tablet pcs like this image (default keyboard of tablet)?
The following code in InputMethodService solved my problem:
#Override
public boolean onEvaluateFullscreenMode() {
return false;
}
overide the following two methods from InputmethodService as bellow, this works always
#Override
public void onUpdateExtractingVisibility(EditorInfo ei) {
// TODO Auto-generated method stub
setExtractViewShown(false);
}
#Override
public boolean onEvaluateFullscreenMode() {
return false;
}
This worked for me :) and if you want to show keyboard in fullscreen mode always then change both false to true.
I am working my way through the NeHe OpenGL examples, using the LWJGL for the OpenGL binding inside an Eclipse RCP application.
My OpenGL graphics are displayed inside the RCP canvas, not in a separate window.
Lesson 07 shows how to use the keyboard. If I try to do a:
Keyboard.create();
I get an error that the (OpenGL) "Display" has not been created.
If I create an OpenGL "Display" with org.lwjgl.opengl.Display.create(), then I get a new Window.
So how do I access the Keyboard without creating a new Window?
You cannot use the Keyboard without a Display, because of how LWJGL works behind the scenes. The best way is to just use AWT events. You can write your own input class, that could go something like this.
public class Input implements KeyListener {
private boolean aDown; //is the A key down?
//Ect, for all needed keys
public void keyPressed(KeyEvent ke) {
switch (ke.getKeyCode()) {
case KeyEvent.VK_A: aDown = true; break;
//and so on for all other needed keys.
}
}
public void keyReleased(KeyEvent ke) {
switch (ke.getKeyCode()) {
case KeyEvent.VK_A: aDown = false; break;
//and so on for all other needed keys.
}
}
public void keyTyped(KeyEvent ke) {} //Do nothing
public void isADown() {return aDown;}
}