Difference between Click, Touch and Gesture in Android? - onclick

I am new to Android developing and there it is not very clear to me the difference between the Click, Touch and Gesture classes in Android.
Is on the generalization of the other?

If you're talking about specific classes its always good to include the fully qualified name so as to avoid ambiguity.
Click is pretty generic so I assume your talking about
android.view.View.OnClickListener. This is an interface your widget class can implement to use the call back method onClick(). Any code inside the onClick() method is executed when you press that view (button).
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Perform action on click
}
});
Touch android.view.View.OnTouchListener
The onTouchListener is an interface that exposes the onTouch() callback method and gives you access to the android.view.MotionEvent members like ACTION_BUTTON_RELEASE. The MotionEvent class is very powerful for movement related behaviour.
Below example is from thread https://stackoverflow.com/a/11690679/1005142
imageButton.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction() == MotionEvent.ACTION_UP){
// Do what you want
return true;
}
return false;
}
});
Gesture android.view.GestureDetector.OnGestureListener
This class is used to pick up how the user gestures their finger with your UI. There is already a lot of information on the Android dev site in the gesture section http://developer.android.com/training/gestures/detector.html.
An example of using this class would be if you were writing fluidly with your finger on the keyboard where you need to listen for touch, movement and acceleration.

Related

How to navigate to page when on resume is called in app.xaml.cs for mvvm in xamarin forms app

I have an issue with navigation. When I click home screen button of device and get back to app I get app homescreen instead of pin page. Ideally it should show pin page and its working fine with back button of device.
OnStart() method has navigationasync but the same is not working with OnResume() method.
Do I have to go to each of the Platform project cs file and add the navigation there like for Android OnRestart()/OnResume() method?
If anyone knows the solution please let me know
Most commonly when writing your Xamarin Application with Prism you will have something like:
protected override void OnInitialized()
{
NavigationService.NavigateAsync("SomePage");
}
OnInitialized is called each time the App's ctor is invoked. This is an important consideration here because this means that any time that the native platform tombstones the app in the background or otherwise refreshes the app by calling OnCreate in your MainActivity or FinishedLaunching in your AppDelegate, then OnInitialized will be invoked resetting your App's Navigation stack to SomePage.
You can however override the OnStart/OnResume in PrismApplication and use whatever business logic you need to determine where to navigate and how you might want to restore your application.
public override void OnStart()
{
NavigationService.NavigateAsync("MainPage");
}
public override void OnResume()
{
if(someCondition)
{
NavigationService.NavigateAsync("SomePage");
}
else
{
NavigationService.NavigateAsync("AnotherPage");
}
}

Handling Keyboard Input at the Page Level with Xamarin Forms

I am working on a Xamarin Forms project for which one requirement is to recognize certain key presses to trigger hot key actions. The devices that we will be deploying the application to have physical keyboards attached. For now, Android is the only platform that is being targeted.
From some research that I did yesterday afternoon, it sounds as though a custom page renderer is what is required. As I played with this concept this morning, I stumbled upon the On* key methods of the Activity class.
I tried adding the following to the MainActivity class in the Android project:
public override bool OnKeyUp([GeneratedEnum] Keycode keyCode, KeyEvent e)
{
return base.OnKeyUp(keyCode, e);
}
Placing a breakpoint on this method seems to show that this code is what is needed (read, this method is fired whenever I press a key on the keyboard).
The issue is that this method is also fired when an Entry control on the page has focus. Shouldn't the key press be handled by the Entry control and not bubbled up to the page?
Generally speaking, is this the right approach for what I am trying to accomplish? Are there other approaches that someone can point me to that might work better?
When I was working with hardware devices, I had to do something similar. I created a custom renderer for an entry on the Xamarin.android side. This captures the Enter key press for both hard and soft key in different events. I think creating custom render for a page like you did could work too but this only captures key presses for elements that are in focus. This works for me as I have the entry in focus when user presses the hardware Enter key.
[assembly: ExportRenderer(typeof(CustomEntry), typeof(CustomEntryRenderer))]
namespace Project.Droid.Controls {
public class CustomEntryRenderer : EntryRenderer {
public CustomEntryRenderer(Context context) : base(context) {
}
protected override void OnElementChanged(ElementChangedEventArgs<Entry> e) {
base.OnElementChanged(e);
Control.EditorAction += Control_EditorAction;
Control.KeyPress += NativeEditText_KeyPress;
}
// Fires only for Soft Keyboard
private void Control_EditorAction(object sender, Android.Widget.TextView.EditorActionEventArgs e) {
if (e.ActionId == ImeAction.Done) {
// your code
e.Handled = true;
}
}
// Fires for Hard Keyboard
private void NativeEditText_KeyPress(object sender, KeyEventArgs e) {
if (e.KeyCode == Keycode.Enter && e.Event.Action == KeyEventActions.Up) {
// your code
e.Handled = true;
}
else
e.Handled = false;
}
}
}
FYI: I also tried the MainActivity event that you are using and it did not work for me. I cannot recall why.

disable back button xamarin.forms

I am sorry, if this question is asked before, but I am unable to find my answer that is why I am asking again this question.
My Scenario is I have placed a back button on my axml views from which I am performing Navigation of Going back on previous views using GoBack() Method.
what I need I want to disable back button on my hardware so that my app should not go back to previous screens which are available in the navigation stack. I am using Prism MVVM for my app, so is there any possibility to disable this button or have some overrideable action method on my ViewModel from which I should stop it.
Hope you could understand my question.
B.R
I found the best way to do this was to use the provided override of OnBackPressed in the android MainActivity and then access a bool I saved somewhere. I use the settings plugin personally but the example below uses the built in application properties (it should work but I haven't tested it).
public class MainActivity : FormsApplicationActivity
{
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
Forms.Init(this, bundle);
LoadApplication(new App());
}
public override void OnBackPressed()
{
var disable = (bool) App.Current.Properties["isBackButtonDisabled"];
if (disable) return;
base.OnBackPressed();
}
}

auto hidden layout in vaadin

i want to make a layout that be visible on mouse over and hide automatically on mouse out. i try this code but it not give me any result;
public class DemoLayout extends VerticalLayout implements MouseOverHandler,
MouseOutHandler,MouseUpHandler {
/**
*
*/
private static final long serialVersionUID = 7610044813670041530L;
public DemoLayout() {
super();
}
#Override
public void onMouseOver(MouseOverEvent event) {
// TODO Auto-generated method stub
System.out.println("Mouse over");
}
#Override
public void onMouseOut(MouseOutEvent event) {
// TODO Auto-generated method stub
System.out.println("Mouse out");
}
#Override
public void onMouseUp(MouseUpEvent event) {
// TODO Auto-generated method stub
System.out.println("Mouse up");
}
}
vaadin doesn't support layout mouse listener?
how can i implement this feature?
thank you
I may be wrong, but I don't know of such listeners for layouts up to Vaadin 7.4.3. The good news is that you have at least a few options:
the handlers you're implementing are from com.google.gwt.event.dom.client package which is bundled in the vaadin-client jar. You're, probably unintentionally, mixing client-side & server-side classes which is most likely not what you're after. However you can try to create your own widget and implement those handlers on the client side as suggested here and/or here
you may be able use the CSS :hover pseudo-class to some degree as discussed here
(perhaps the simplest) you could achieve a similar effect with a click. Take a look at the SliderPanel add-on to avoid re-inventing the wheel
You have two options:
Depending on your exact use case, you could probably just use CSS and the :hover pseudo class.
If :hover doesn't work then you could use the excellent Mouse Event Extension addon.

Calling a non-parental Activity method from fragment without creating a new instance

I have my MainActivity and inside that I have a number of fragments. I also have another activity that works as my launcher and does everything to do with the Google Drive section of my app. On start up this activity launches, connects to Drive and then launches the MainActivity. I have a button in one of my fragments that, when pushed, needs to call a method in the DriveActivity. I can't create a new instance of DriveActivity because then googleApiClient will be null. Is this possible and how would I go about doing it? I've already tried using getActivity and casting but I'm assuming that isn't working because DriveActivity isn't the fragments parent.
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//TODO for test only remove
directory = new Directory(SDCARD + LOCAL_STORAGE);
byte[] zippedFile = directory.getZippedFile(SDCARD + STORAGE_LOCATION + directory.getZipFileName());
//Here I need to somehow call DriveActivity.uploadFileToDrive(zippedFile);
//((DriveActivity)getActivity()).uploadFileToDrive(zippedFile);
}
});
Right, so I'm having a bit of difficulty with the heirarchy but I think what you want to do is define a method in the fragment that the activity will be required to override to use.
This will allow you to press the button, and then fire a method whos actual implementation is inside the parent.
public interface Callbacks {
/**
* Callback for when an item has been selected.
*/
public void onItemSelected(String id);
}
example implementation:
private static Callbacks sDummyCallbacks = new Callbacks() {
#Override
public void onItemSelected(String id) {
//Button fired logic
}
};
so in the child you'd do just call:
this.onItemSelected("ID of Class");
EDITED
In retrospect what I believe you need is an activity whos sole purpose is to upload files, not fire off other activities.
Heres an example of a 'create file' activity:Google Demo for creating a file on drive
Heres an example of the 'base upload' activity' Base Service creator

Resources