Android viewflow validation - java

Currently I have a few forms using this horizontal sliding view.
https://github.com/pakerfeldt/android-viewflow
Is their a way to prevent the user from sliding to the next screen when the form is filled in incorrectly? e.g. disable the horizontal scroll.

Yes, change this portion of your copy of the ViewFlow
#Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
....
case MotionEvent.ACTION_MOVE:
//Add an if / else here that validates your input.
//If it is incorrect don't let the rest of the code that is here do the scrolling.

I had the same problem, just added a Boolean variable and a setter and getter:
...
public class ViewFlow extends AdapterView<Adapter> {
private Boolean flagAllowScroll = false;
//the rest of the code
...
public Boolean getFlagAllowScroll() {
return flagAllowScroll;
}
public void setFlagAllowScroll(Boolean flagAllowScroll) {
this.flagAllowScroll = flagAllowScroll;
}
And the put it on the onTouchEvent function
...
#Override
public boolean onTouchEvent(MotionEvent ev) {
if (!getFlagAllowScroll())
return false;
//the rest of the code
...
And other in the onInterceptTouchEvent function
...
#Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
if (!getFlagAllowScroll())
return false;
//the rest of the code
...
And change it of this way:
...
//Allows changes
viewFlow.setFlagAllowScroll(true);
...
//Doesn't Allows changes
viewFlow.setFlagAllowScroll(false);
...
I hope this works for you =)

Related

How to call dispatchTouchEvent with a 'delay' in onTouch?

I have two views which are siblings of each other. They both cover the full screen. So one is behind the other. If the upper one gets touched (onTouch), I delegate the touch events to the one underneath it (with dispatchTouchEvent).
But sometimes I want to delay that delegation, till the next time onTouch gets called. But somehow that does not work.
An example to clarify:
To viewA - which is in front of viewB - I have applied the following (simplified) code:
viewA.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View view, MotionEvent event) {
int touchType = event.getActionMasked();
if (touchType == MotionEvent.ACTION_DOWN) {
savedEvent = event;
return true; // I also tried returning false here
} else {
if (savedEvent != null) {
viewB.dispatchTouchEvent(savedEvent);
savedEvent = null;
}
viewB.dispatchTouchEvent(event);
return true; // I also tried returning false here
}
}
});
To test the dispatchTouchEvent call, I have the following code for viewB
viewB.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View arg0, MotionEvent event) {
Log.d("test", "test"); // this code gets logged, so it is being called, but the view seems to not execute any of the touch events
return true;
}
});
When I change to code for viewA to this:
viewA.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View view, MotionEvent event) {
viewB.dispatchTouchEvent(event);
return true;
}
});
everything works just fine.
But the thing is that for my use case, I sometimes have to call the dispatchTouchEvent method with the event parameter outside its originating onTouch method, so to speak.
Is this even possible? And if yes, how?
So I found out what prevents it from working. If you manually call dispatchTouchEvent you should pass through an event that you created yourself with MotionEvent.obtain().
Working example:
viewA.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View view, MotionEvent event) {
int touchType = event.getActionMasked();
if (touchType == MotionEvent.ACTION_DOWN) {
// do not do this, somehow passing on a reference of the event directly does not work:
// savedEvent = event;
// this works though:
savedEvent = MotionEvent.obtain(event);
return true;
} else {
if (savedEvent != null) {
viewB.dispatchTouchEvent(savedEvent);
savedEvent = null;
}
viewB.dispatchTouchEvent(event);
return true;
}
}
});
Although I don't understand why that does work and just passing the event reference directly does not, I tested this and it does work perfectly.

Android processing priority

I am using android eclipse for programming.
I will use this basic calculation to fit to what I want to happen.
Here:
I got two buttons add and minus. if i press add it will obviously call the method add.
But my problem is. if I will keep pressing add button. It will keep adding multiple times and
if I will click 2 buttons at the same time it will also do add and minus. What I want is that
if i click both button at the same time there's priority that add button will execute first
and the minus button will not send data.
Add(){
a = b + c;
}
Minus(){
a = b - c;
}
public void add(View view){
Add();
}
public void subtract(View view){
Minus();
}
Just set button_minus.setClickable(false); in Add method and
button_add.setClickable(false); in Minus method. Then enable them back.
try:
private boolean isAddButtonPressed;
private void setListener(){
buttonAdd.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction() == MotionEvent.ACTION_DOWN)
isAddButtonPressed = true;
else
isAddButtonPressed = false;
return false;
}
});
}
public void add(View view){
doMath(true);
}
public void subtract(View view){
if(!isAddButtonPressed)
doMath(false);
}
// "synchronized" means that this method can ran only one time at a time
private synchronized void doMath(boolean isAdd) {
if(isAdd) {
Add();
} else {
Minus();
}
}

vaadin viewchangelistener change newView

I try to change the newView of a navigation if the user is not authenticated and the newView is an instance of SecuredView. My approach was to check inside the beforeViewChange method if the constraints are given.
public boolean beforeViewChange(ViewChangeEvent event) {
if (event.getNewView().getClass().isInstance(SecuredView.class) && VaadinService.getCurrentRequest().getUserPrincipal() == null) {
event.getNavigator().navigateTo("login");
return false;
}
return true;
}
}
But that will result in a endless loop of redirection... How can i change the newView and stop the current navigation event?
1) If your View is not 'a correct view' it will end up in an endless loop. Try this simple view:
public class SecuredView extends VerticalLayout implements View {
public SecureView() {
addComponent(new Label("Secured View"));
}
#Override
public void enter(ViewChangeEvent event) {
}
}
2) Your if statement needs to be changed:
instead of:
event.getNewView().getClass().isInstance(SecuredView.class)
write:
SecuredView.class.isInstance(event.getNewView());

Is there any method to find out the display-changing events of android smartphone?

I'm a student of Yonsei graduate school, Korea.
I want to make a simple application that measures an time interval - between
touching the screen and display-updating.
I found that following method catch the touching event.
public boolean onTouchEvent(MotionEvent event) {
super.onTouchEvent(event);
if(event.getAction() == MotionEvent.ACTION_DOWN ){
...
Now I'm searching the Android APIs, but I coudn't find the method which catches
display-updating event. If you have any information about this problem,
please show mercy to me. Thank you.
Try adding a ViewTreeObserver.onDrawListener to a view in your Activity's content view. You can make a class that implements the that interface. When you get the touch event, call a method on your draw listener to record the time of the next draw event.
private MyDrawListener myDrawListener = new MyDrawListener();
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(...);
findViewById(...).getViewTreeObserver().addOnDrawListener(myDrawListener);
}
public boolean onTouchEvent(MotionEvent event) {
super.onTouchEvent(event);
if (event.getAction() == MotionEvent.ACTION_DOWN) {
myDrawListener.recordNextDrawTime();
}
}
public static class MyDrawListener implements ViewTreeObserver.OnDrawListener {
private boolean recordNextDrawTime;
public void recordNextDrawTime() {
recordNextDrawTime = true;
}
#Override
public void onDraw() {
if (recordNextDrawTime) {
Log.d("MyDrawListener", "Draw time = " + System.currentTimeMillis());
recordNextDrawTime = false;
}
}
}

Android NativeActivity - intercepting input at the Java level

Is there a way to intercept input in a NativeActivity before it gets dispatched to the AInputQueue in native code? The reason I need to intercept input in Java is to support gamepad/joystick events that I can't capture using any of the android/input.h functions, ie. MotionEvent.getAxisValue(MotionEvent.AXIS_RZ).
This following does not work (my manifest correctly points to my derived NativeActivity class):
public class CustomNativeActivity extends NativeActivity
{
private View.OnTouchListener touchListener = new View.OnTouchListener() {
public boolean onTouch (View v, MotionEvent event)
{
// This is never called!
System.out.println("onTouch");
return false;
}
};
public void setContentView(View view)
{
// This method is called, but registering a touch listener does nothing!
view.setOnTouchListener(touchListener);
super.setContentView(view);
}
public boolean dispatchTouchEvent(MotionEvent ev)
{
// This is never called either!
System.out.println("dispatchTouchEvent!");
return super.dispatchTouchEvent(ev);
}
}

Categories