Overriding back button in sencha app for Android - android

While using a sencha touch app on Android, is it possible that the device back button behaves completely as it behaves for a native Andriod app? This thread addresses the same problem. The suggested solution is
if (Ext.os.is('Android')) {
document.addEventListener("backbutton", Ext.bind(onBackKeyDown, this), false);
function onBackKeyDown(eve) {
eve.preventDefault();
//do something
alert('back button pressed');
}
}
but in this function, how can i go to the previously visited screen?

Of course it depends on how your user interface is built and how the views are connected. Simplest way I can think of is to detect somehow the presence of a back button in the page and trigger a click on it.
In my application each view that needs to handle the backbutton event has a button with cls = 'back' that implements a tap listener responsible of the logic to go back to the previous view.
About how to implement this logic: there is no magic wand for that. If a particular view can be reached through one and only one parent view, then you can hardcode it. Else, if a view can be reached from more than one, I save in a property of the view's controller a reference to the view which I came from, and then on backbutton's tap I move to that view.
The animation of moving to a view can usually be obtained by Ext.Viewport.animateActiveItem(parentView, {type: 'fade'}); (but again it depends on how you implemented your views tree)
So in my handler for the backbutton I check for such a button with [cls=back] (but you could also check for ui or whatever you want) and I fire the tap event on it.
Moreover, when i detect I am on the root view I ask the user if he wants to exit the application and if so I call exit().
_handleBackButtonEvent : function(e) {
// First look if the current view is the mainView
var item = Ext.Viewport.getActiveItem();
// If we are on mainView, quit.
// Sayonara, goodbye, adios
if (item.getId() === 'mainView') {
navigator.notification.confirm(
"Really quit?",
function(option) {
if (option == 1) {
navigator.app.exitApp();
}
},
"Confirm",
"Yes,No"
);
} else {
// else look if the current view has a backbutton
var titlebar = item.down('titlebar[docked=top]');
var backbutton = null;
if (titlebar) {
backbutton = titlebar.down('button[iconCls=back]');
}
if (backbutton && !backbutton.isHidden() && !backbutton.isDisabled()) {
backbutton.fireEvent('tap');
} else {
// if not, fallback to mainView
item = Ext.getCmp('mainView');
Ext.Viewport.animateActiveItem( item, {type: 'slide', direction: 'right'});
}
}
e.preventDefault();
}

Related

Is there a way to detect the "LongClick" event of a NumberPicker in Android?

I am trying to set the background of the NumberPicker as long as the user either scrolls with a touch swipe gesture or with the long press gesture.
Currently I can only react on a touch swipe gesture.
The following code snippet works, but only if the user performs a touch swipe gesture (like touching the screen and swiping up or down).
NumberPicker.OnScrollListener myListener = (view, scrollState) -> {
if(scrollState == NumberPicker.OnScrollListener.SCROLL_STATE_IDLE) {
//change background to eg. white
}
else {
//change background to eg. black
}
}
myPicker.setOnScrollListener(myListener);
However If I long press e.g. on the "3" or "20" as seen in this picture the scroll state doesn't change even though the numberPicker is visually scrolling.
How can I react on such an event?
I tried to set a setOnLongClickListener (see following code snippet) but I don't even get a LongClick event.
View.OnLongClickListener myClickListener = v -> {
//change background
return true;
};
myPicker.setOnLongClickListener(myClickListener);

How do I switch the focus of a current touch down from View A to View B?

I'm trying to detect a touch down on a view which is created after a long-press, and while the user still has their finger down on the screen.
I am currently building a simple app with a pop-up menu which appears when the user executes a long press. This app has an Activity, and a Fragment which appears over the whole screen to display the menu.
The user does a long-press down on the Activity, and this is detected by the activity, which then creates a Fragment, which overlays the whole screen.
The functionality I wish for is the Fragment to then contain code with onTouchListeners which detect this current touch down event (as the user still has their finger on the screen), and assign it directly to views in the Fragment
Here is the code containing the longClickListener() which detects the touch in the Activity, and then adds the Fragment.
private fun setRootViewLongTouchListener() {
rootLayout.setOnLongClickListener {
displayOverlay()
false
}
}
private fun displayOverlay() {
supportFragmentManager
.beginTransaction()
.add(R.id.rootLayout, OverlayFragment.newInstance(touchXPosition, touchYPosition), "overlayFragment")
.addToBackStack(null)
.commit()
}
Here is the code inside the Fragment which I'd wish to detect the finger's current touch down (the finger is not lifted between displayOverlay() above being called, and Fragment being created, and the view in the Fragment detecting a touch on it).
view.setOnTouchListener { view, motionEvent ->
if (motionEvent.action == MotionEvent.ACTION_DOWN || motionEvent.action == MotionEvent.ACTION_MOVE) {
displayCircleAtTouchPosition(centreCircleOnX(motionEvent.x), centreCircleOnY(motionEvent.y))
} else if (motionEvent.action == MotionEvent.ACTION_UP) {
popFragment()
}
false
}

how to implement long touch in titanium

I am working at an Android app in Titanium. My question is how to implement long touch in Titanium? I need something like this: when user keep pressed a certain view to call some function. I tried this :
arrowright.addEventListener('touchstart', function(e) {
touched = true;
setTimeout(function() {
if (touched) {
arrowright.fireEvent('longTouch');
}
},100);
});
arrowright.addEventListener('touchmove', function(e) {
touched = false;
});
arrowright.addEventListener('touchend', function(e) {
touched = false;
});
arrowright.addEventListener('longTouch',function(){
clickTheView(e);
},
false);
but this is not working like I want. My function clickTheView(e) is call every time I click the view and it is not called when I touch for a long time the view.
Any idea is welcome. Thanks in advance.
If you have the latest Ti SDK its included. See http://developer.appcelerator.com/apidoc/mobile/latest/Titanium.UI.Button.longpress-event.html

Android:ViewFlipper with onBackPressed

Ia m using ViewFlipper in my app, and to move inside the pages, I use a Button for next and capturing the onBackPressing to return back.
the behavior is the following:
1) I click on button and move to 2 page.
2) click back and code work
3) click again on the button next
4) click back and now wont work anymore
on the step 4, I can feel the vibration, so the event fire, but my viewflipper wont to go back.
Any suggestion?
Thank's
public void onBackPressed() {
if (flipView.getDisplayedChild() == 1) {
flipView.setDisplayedChild(0);
} else if (flipView.getDisplayedChild() == 0) {
flipView.setDisplayedChild(1);
}
}
This works perfectly for me. Change onBackPressed to what ever method is calling the back button.
Just to add something to the other answer.
Let's say we have only two views that we are flipping through, doing:
public void onBackPressed() {
if (mViewFlipper.getDisplayedChild() == 1) {
mViewFlipper.setDisplayedChild(0);
} else if (mViewFlipper.getDisplayedChild() == 0) {
flipView.setDisplayedChild(1);
}
}
isn't enough. As a matter of fact, it creates another problem for you.
If the view is in 0 (the first), and you then press the back button, NOTHING happens. The activity doesn't exit. This is because you haven't called super.onBackPressed(). Now, adding super.onBackPressed() to the code above also creates another problem. When you flip from 1 (the second view) it goes to the first view (0) and then exits the activity, which is wrong if not for anything but for the weird animation of skipping a view while transitioning from one activity to another.
The best way to implement onBackPressed() for your activity containing a ViewFlipper is this:
public void onBackPressed() {
int displayedChildId = mViewFlipper.getDisplayedChild(); //get current view's number
if (displayedChildId > 0) { //if this number is greater than 0(let's say 5)
mViewFlipper.setDisplayedChild(displayedChildId - 1);//We then go down that number by 1. That is 5 - 1, which is 4. This happens until displayedChildId isn't greater than 0 anymore, which is then the first view. if we press back from here, we exit the activity.
} else {
super.onBackPressed();
}
}
I hope this makes sense

Handling touch events in order

I want to write a program that handles touch events in a specific order within a single activity.
For example:
A few views are shown.
If the user doesn't tap on the first view, I will show another activity.
If the user taps on it however, I want to detect a tap on the second, then the third, etc.
How can I handle multiple touch events?
I think I need an onTouchEvent method and in it I need an if-else statement for the first click but I don't know how I can monitor for the subsequent touch events.
It may help you.I always do like this
public void onClick(View v){
if(v==imageView1){
//do ssomething
}
if(v== imageView2){
//do something
}
if(v==imageView3){
//do something
}
like this u can do according to different button or imageview
I'm assuming the picture is an ImageView inside the main view. Why not just append touchlisteners to each view?
Set the onClickListener for the n+1 view only when nth view is clicked.
Like this
view1.setOnClickListener(new OnClickListener() {
void onClick(View v) {
view2.setOnclickListner(new Onclicklistener() {
void onClick(View v) {
// add further view's click listeners else do what ever if this
// is the last view.
}
});
}
});
Not an elegant solution but should work IMHO.
From your problem statement it seems you have an ordered list of views, each should have a touch listener, but the listener for the second view should not fire unless the listener for the first view has fired first.
This can easily be done by keeping a counter in your activity:
private int highestIndexTapped. When a view is tapped, check whether its index is such that index == highestIndexTapped + 1.
If it is, increase highestIndexTapped by 1 and fire the listener. Otherwise either eat the touch event or pass it on to the next part of your pipeline.

Categories

Resources