How to swipe with finger in a View flipper? - android

I have a view flipper containing two layouts with button. Now the issue is i am not able to swipe along with finger. Basically the view should swipe along with the finger. I hope you are getting what i am trying to say. Here is the onTouchevent code :-
bottomFormatFlipper = (ViewFlipper)findViewById(R.id.bottomFormatFlipper);
public boolean onTouchEvent(MotionEvent touchevent)
{
switch (touchevent.getAction())
{
// when user first touches the screen to swap
case MotionEvent.ACTION_DOWN:
{
lastX = touchevent.getX();
break;
}
case MotionEvent.ACTION_UP:
{
float currentX = touchevent.getX();
// if left to right swipe on screen
if (lastX < currentX)
{
// If no more View/Child to flip
if (bottomFormatFlipper.getDisplayedChild() == 0)
break;
// set the required Animation type to ViewFlipper
// The Next screen will come in form Left and current Screen will go OUT from Right
bottomFormatFlipper.setInAnimation(this, R.anim.in_from_left);
bottomFormatFlipper.setOutAnimation(this, R.anim.out_to_right);
// Show the next Screen
bottomFormatFlipper.showNext();
}
// if right to left swipe on screen
if (lastX > currentX)
{
if (bottomFormatFlipper.getDisplayedChild() == 1)
break;
// set the required Animation type to ViewFlipper
// The Next screen will come in form Right and current Screen will go OUT from Left
bottomFormatFlipper.setInAnimation(this, R.anim.in_from_right);
bottomFormatFlipper.setOutAnimation(this, R.anim.out_to_left);
// Show The Previous Screen
bottomFormatFlipper.showPrevious();
}
break;
}
}
return false;
}
From this code, when i swipe, then after a second or so, the swipe animation occurs. I want that the view should swipe with my finger

Related

How to swipe list view item left in Android?

I have a list view and I want to make it so I can swipe each item to reveal a delete button:
I've figured out how to recognise the swipe event, I am using this code (listItem is of type View):
listItem.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event)
{
switch (event.getAction())
{
case MotionEvent.ACTION_DOWN:
_xSwipe1 = event.getX();
break;
case MotionEvent.ACTION_UP:
_xSwipe2 = event.getX();
float deltaX = _xSwipe2 - _xSwipe1;
if (deltaX < 0)
{
Log.e("SWIPE", "Right to Left swipe");
}
else if (deltaX >0)
{
Log.e("SWIPE", "Left to right swipe");
}
break;
}
return false;
}
});
And when I swipe, I can see in the logs that the swipe event is being recognised.
However, I'm not sure how to physically make the list item start disappearing to the left?
Any help would be greatly appreciated.
If you know when the swipe is happening and how much the user has swiped you could call View.setTranslationX() depending on how much the user has swiped. Alternatively, you could forego the swipe detection logic altogether and use a ViewPager with only 2 pages. You would then override PagerAdapter.getPageWidth(...) in your PagerAdapter so that the delete button only takes up a limited amount of space.

Android ViewFlipper with 2 views

I'm looking for help with creating ViewFlipper which will work like in android calendar when switching between months by swipe.
I want to have only two views inside my viewFlipper, and when i swipe right i want to do
mViewFlipper.setDisplayedChild( 1 );
and when i swipe left :
mViewFlipper.setDisplayedChild( 0 );
and when i swipe twice left, i want to get every time full in/out animation and the same for right swipping. Full animation works only when i swipe to next and to previous, never in direction of next -> next.
Thanks for help!
If you're only using 2 views, and at present and you're setting 0 and 1 manually as such, just call
mViewFlipper.showNext();
instead regardless of the swipe direction and apply the correct animation depending on if you want it to be sliding left or right. This will mean even if you're on view 1 and you slide right again (like you defined) then view 0 will come back in, like a never ending flipper between the 2 views.
http://developer.android.com/reference/android/view/animation/AnimationUtils.html
I may be totally misunderstanding your question however, it is rather vague.
See this link it may help.....
http://www.1mobile.com/effective-navigation-863044.html
http://developer.android.com/training/implementing-navigation/index.html
**folder in res = animator.......
translation animation xml file = s_in_fleft in animator folder in res folder.....
might help**
public boolean onTouchEvent(MotionEvent touchevent) {
switch (touchevent.getAction()) {
case MotionEvent.ACTION_DOWN: {
lastX = touchevent.getX();
break;
}
case MotionEvent.ACTION_UP: {
float currentX = touchevent.getX();
if (lastX < currentX) {
if (viewFlipper.getDisplayedChild() == 0)
break;
viewFlipper.setInAnimation(this, R.animator.s_in_fleft);
viewFlipper.setOutAnimation(this, R.animator.s_out_right);
viewFlipper.showNext();
}
if (lastX > currentX) {
if (viewFlipper.getDisplayedChild() == 1)
break;
viewFlipper.setInAnimation(this, R.animator.s_in_fright);
viewFlipper.setOutAnimation(this, R.animator.s_out_left);
viewFlipper.showPrevious();
}
break;
}
}
return false;
}

Flip images using viewflipper in android without buttons

I am using the following method of flipping images: How to set dymanic images to ViewFlipper in android?
I do not want to flip using buttons. When the user touches the images or swipes the image I want to flip to the next image. Is there a way I can achieve this?
You can use ViewPager. See these links:
Implementing Horizontal View Swiping Using ViewPager and FragmentPagerAdapter in Android
Horizontal View Swiping with ViewPager, Updated
Android Viewpager as Image Slide Gallery (Swipe Gallery)
and this Library
Support Library
this might Help..... animator is a folder in res and the s_out_* is an xml file with a translate
public boolean onTouchEvent(MotionEvent touchevent) {
switch (touchevent.getAction()) {
case MotionEvent.ACTION_DOWN: {
lastX = touchevent.getX();
break;
}
case MotionEvent.ACTION_UP: {
float currentX = touchevent.getX();
if (lastX < currentX) {
if (viewFlipper.getDisplayedChild() == 0)
break;
viewFlipper.setInAnimation(this, R.animator.s_in_fleft);
viewFlipper.setOutAnimation(this, R.animator.s_out_right);
viewFlipper.showNext();
}
if (lastX > currentX) {
if (viewFlipper.getDisplayedChild() == 1)
break;
viewFlipper.setInAnimation(this, R.animator.s_in_fright);
viewFlipper.setOutAnimation(this, R.animator.s_out_left);
viewFlipper.showPrevious();
}
break;
}
}
return false;
}

differentiating left and right scroll in gallery

Hie all ,
is there any method in android that can differentiate the left and the right scrolling actions in android.
it is actually a customized gallery view so what I am trying to do is that on scroll actions i want to different images. I mean on left scroll I've different images and on right scroll I've different images to show. so i need to differentiate the let and right scroll.
Try this code:
new OnTouchListener() {
#Override
public boolean onTouch(View currentView, MotionEvent touchevent) {
switch (touchevent.getAction())
{
case MotionEvent.ACTION_DOWN:
{
oldTouchValue = touchevent.getX();
break;
}
case MotionEvent.ACTION_UP:
{
float currentX = touchevent.getX();
if((oldTouchValue - currentX) < 50 && (oldTouchValue - currentX) > -50) {
return false;
}
if (oldTouchValue < currentX)
{
// Left swipe...
}
if (oldTouchValue > currentX)
{
// Right swipe
}
break;
}
}
return false;
}
}

How to set finger touch functionality in android?

I am developing a small application in android in which i have few image in my application i want when user touch with one finger images can move left or right side and when user can touch with two fingers it could be zoom how can i do this please refer some tutorial code for me.
here is my code
and i used view flipper in xml v fdjf
public class Jaap extends Activity implements OnTouchListener{
float downXValue;
int counter = 0;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
// Set main.XML as the layout for this Activity
setContentView(R.layout.jaap);
// Add these two lines
LinearLayout layMain = (LinearLayout) findViewById(R.id.layout_main);
layMain.setOnTouchListener((OnTouchListener) this);
// Add a few countries to the spinner
}
public boolean onTouch(View arg0, MotionEvent arg1) {
// Get the action that was done on this touch event
switch (arg1.getAction())
{
case MotionEvent.ACTION_DOWN:
{
// store the X value when the user's finger was pressed down
downXValue = arg1.getX();
break;
}
case MotionEvent.ACTION_UP:
{
// Get the X value when the user released his/her finger
float currentX = arg1.getX();
// going backwards: pushing stuff to the right
if (downXValue < currentX)
{
// Get a reference to the ViewFlipper
ViewFlipper vf = (ViewFlipper) findViewById(R.id.details);
// Set the animation
// vf.setAnimation(AnimationUtils.loadAnimation(this, R.anim.push_left_out));
// Flip!
if(counter > 0){
vf.showPrevious();
counter--;
}
}
// going forwards: pushing stuff to the left
if (downXValue > currentX)
{
// Get a reference to the ViewFlipper
ViewFlipper vf = (ViewFlipper) findViewById(R.id.details);
// Set the animation
// vf.setInAnimation(AnimationUtils.loadAnimation(this, R.anim.push_left_in));
// Flip!
if(counter < 131){
vf.showNext();
counter++;
}
}
break;
}
}
// if you return false, these actions will not be recorded
return true;
}
}
Check out this step-by-step tutorial, it contains code examples how to zoom in/out a picture and move it around.
Handle the MotionEvent in the onTouchEvent(MotionEvent) of the Activity.
In this check for the MotionEvent.getAction().
switch(MotionEvent.GetAction()) {
case ACTION_DOWN:
//handle the finger down functionality here
break;
case ACTION_POINTER_DOWN:
//handle the second finger down functionality here
break;
}
A sequence of events would be issued, mostly with the actions as follows:
ACTION_DOWN - one finger touch down
ACTION_MOVE -> the finger is moved
ACTION_UP - one finger touch is removed
ACTION_POINTER_DOWN - second finger touch down
ACTION_POINTER_UP - second finger touch up
You will have to check the X,Y positions in the event and determine what should be donw...
will see if there are any good tutorials/samples to explain these better...
getPointerCount() of motionEvent tells you how many fingers are touch. In gives number of touch
Thanks

Categories

Resources