Change image while ImageButton is pressed - xamarin.android

I have one ImageButton in my android app, and I'd like the app to change the image while the button is pressed, and when the user release the button it returns to the default image.
P.S. I'm using mono for android and I have to do this programatically, I can't use the XML tag unfortunatelly.

You can hook into the views's Touch event to do that:
var button = FindViewById<ImageButton>(Resource.Id.MyImageButton);
button.Touch += (object sender, View.TouchEventArgs e) => {
if (e.Event.Action == MotionEventActions.Down) {
button.SetImageResource(Resource.Drawable.Icon);
} else if (e.Event.Action == MotionEventActions.Up) {
button.SetImageResource(Android.Resource.Drawable.IcMenuGallery);
}
};

Related

Nativescript Angular Detect Swipe to Back for IOS

In Nativescript Angular, I can swipe from left edge to right to go back on iOS. Is there any way i can detect or capture this swipe back?
The reason for this is because I have a button on screen that execute "this.routerExtensions.back()" when tapped. I would like to identify whether a "Back action" is from this button or iOS swipe event.
Thanks
NativeScript has this property on pages.
https://docs.nativescript.org/api-reference/classes/ui_page.page.html#enableswipebacknavigation
If you set that to false, you should then be able to create your own gesture for going back. Even on native iOS, I am not sure you can directly tap into the back gesture without instead replacing it with your own.
This is how I was able to solve it for Android and iOS:
adjust constructor to manipulate the pullToLeft function from Android, and disable the default swipe back from iOS:
constructor(...) {
if (isAndroid) {
Application.android.on(
AndroidApplication.activityBackPressedEvent,
(data: AndroidActivityBackPressedEventData) => {
data.cancel = true;
...yourCustomFunction()
}
);
} else {
page.enableSwipeBackNavigation = false;
}
}
for Android it is already working - for iOS you need then to write a function you can use on a template (this is also how you can recognize the swipe action):
public onSwipe = (args: SwipeGestureEventData) => {
if (!isAndroid) {
if (args.direction === SwipeDirection.right) {
...yourCustomFunction()
}
}
};
disable the default PopGestureSerializer and add the swipe function on top of your template/element and it should work on iOS as well:
<StackLayout [interactivePopGestureRecognizer]="false" (swipe)='onSwipe($event)'>
...
</StackLayout>

Windows Phone - XNA game - Back button

I have some problem with my back button. In my game I have two screens. One with title (menu) and second with the game. When I use once back button i pause game and back to title screen. When i use it again I need to kill the app process. How can i do that? Down below i show u how I use back button. I try use 2 gestures, but when I declared them nothing go right.
That's how i declared gesture in Initialize():
TouchPanel.EnabledGestures = GestureType.FreeDrag
First I declared
bool IsPlayingGame = true;
int endGame= 0;
Then in function Update:
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
{
if (!IsPlayingGame) this.Exit();
}
if (isTitleScreenShown)
{
UpdateGameScreen();
}
else if (isGameSceenShown)
{
UpdateTitleScreen();
// TODO: Add your update logic here
/* MY FUNCTIONS */
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
{
if (endGame == 5) base.Exit();
}
}
base.Update(gameTime);
In Visual studio, it works. I thought great, but on Nokia phone it doesn't. Why? Any help?
In the GamePage.xaml.cs there should be a method protected override void OnNavigatedFrom(NavigationEventArgs e)
This method is triggered when you touch the 'back' button on your device.

Hide Keyboard in iOS by using Tabris

is there an option to remove the keyboard from screen on iOS ? I use Tabris (http://developer.eclipsesource.com/tabris/) and Java in this context.
My problem is that i use two textfields to enter a user/passwort combination. After I filled these textfields and press a button to continue the keyboard from iOS is always shown but I want that the keyboard no longer appears. Only after I click somewhere then the keyboard disappears.
On Tabris, you can "open" the keyboard programmatically by setting the Focus on a Control which uses the keyboard (like org.eclipse.swt.widgets.Text ).
To hide the keyboard, just set the Focus to a Control which does not require a keyboard, like the parent composite of your Textfield.
In your case I would add al line to the SelectionListener of your Button to set Focus on the Parent of your Textfields, then start the Login procedure.
Here is some code to play and understand the Focus mechanism:
public class FocusTest implements EntryPoint {
public int createUI() {
Display display = new Display();
Shell shell = new Shell(display, SWT.NO_TRIM);
shell.setMaximized(true);
GridLayoutFactory.fillDefaults().applyTo(shell);
createContent(shell);
shell.open();
//while (!shell.isDisposed()) {
// if (!display.readAndDispatch()) {
// display.sleep();
// }
//}
return 0;
}
private void createContent(final Composite parent) {
Button buttonSingleText = new Button(parent, SWT.PUSH);
buttonSingleText.setText("Focus on SingleText");
GridDataFactory.fillDefaults().grab(true, false).align(SWT.FILL, SWT.CENTER).applyTo(buttonSingleText);
Button buttonMultiText = new Button(parent, SWT.PUSH);
buttonMultiText.setText("Focus on MultiText");
GridDataFactory.fillDefaults().grab(true, false).align(SWT.FILL, SWT.CENTER).applyTo(buttonMultiText);
Button buttonNoFocus = new Button(parent, SWT.PUSH);
buttonNoFocus.setText("Loose Focus");
GridDataFactory.fillDefaults().grab(true, false).align(SWT.FILL, SWT.CENTER).applyTo(buttonNoFocus);
final Text singleText = new Text(parent, SWT.SINGLE);
GridDataFactory.fillDefaults().grab(true, false).align(SWT.FILL, SWT.CENTER).applyTo(singleText);
final Text multiText = new Text(parent, SWT.MULTI);
GridDataFactory.fillDefaults().grab(true, false).align(SWT.FILL, SWT.CENTER).applyTo(multiText);
buttonSingleText.addSelectionListener(new SelectionAdapter() {
#Override
public void widgetSelected(SelectionEvent e) {
singleText.setFocus();
}
});
buttonMultiText.addSelectionListener(new SelectionAdapter() {
#Override
public void widgetSelected(SelectionEvent e) {
multiText.setFocus();
}
});
buttonNoFocus.addSelectionListener(new SelectionAdapter() {
#Override
public void widgetSelected(SelectionEvent e) {
parent.setFocus();
}
});
}
}
Did you set UITextField delegate and add the following method there?
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
[textField resignFirstResponder];
return YES;
}

How to change the picture of CustomButtonField on click event?

I have posted this question previously but the answer is not appropiate. The solution provided just change the picture when the custombutton has focus and unfocus. Suppose in my application I need to change the picture if the user clicks on the customButton, n i m doing this by calling the same screen (ie UiApplication.getUiApplication().pushScreen(new Screen2(b));) . Screen2 is the screen which holds the customButton. On the click evevt i m pushing the same screen by passing aint variable pic_status that determines which picture to be drawn in the CustomButton in the new screen. Is there any way to update the picture in the CustomButtonField on click event without pushing the same Screen again and again.
//code in Screen2
public void fieldChanged(Field field, int context)
{
if(field == bf1)
{
if(pic_status == 0)
{
pic_status=1;
}
UiApplication.getUiApplication().pushScreen(new Screen2(pic_status));
}
//code in CustomButtonField
CustomButtonField(String label,int pic_status,long style)
{
super(style);
this.label = label;
this.labelHeight = getFont().getHeight();
this.labelWidth = getFont().getAdvance(label);
this.notice = s;
if(pic_status ==0)
{
currentPicture1 = onPicture;
currentPicture2 = onPicture;
}
if(pic_status ==1)
{
currentPicture1 = clickPicture;
currentPicture2 = onPicture;
}
if( pic_status==2 )
{
currentPicture1 = onPicture;
currentPicture2 = clickPicture;
}
}
I need a way to update the customButtonField text and picture on the buttonClick event not on focus/unfocus event without pushing the same Screen again and again. If my above description of problem is not satisfactory, plz add a comment n i can give more details explanation of my problem?
We can override some methods of the CustomButtonField such as protected boolean keyChar(...), protected boolean navigationClick(...), protected boolean trackwheelClick(...), protected boolean touchEvent(...), etc and use them to change button image when click or select event occurred.
For example on protected boolean touchEvent(...) we can do following task..
Replace the image with desired ones when we get TouchEvent.ClICK.
Call invalidate().
On TouchEvent.UNCLICK restore original image.
Call invalidate().

markitup preview toggle

I want to add a button to a markitup set by which i can toggle preview, that is if i click on preview button i can see the preview, if i want to hide the preview - i can hide it by using that button. I've googleed for this but still no result. Any idea how to do that?
Nevermind, i've figured it out. I've added a button to hide it, code as below -
{name:'Hide Preview', call: function (markitUp){ miu.hidepreview()}, className:'hidepreview'}
Where hidepreview() as my custom function. I defined this function as below
miu = {
hidepreview : function(){
$('.markItUpPreviewFrame').remove();
}
}
It works for me.
You can do it by holding the Alt key and clicking on the Preview button - this will close the opened preview window.
It's in the else if statement of the preview() function:
} else if (altKey === true) {
if (iFrame) {
iFrame.remove();
} else {
previewWindow.close();
}
previewWindow = iFrame = false;
}

Resources