3

I got swipe up and swipe down for a layout by using setOnTouchListener (For understanding see below images)

Images

enter image description here enter image description here

But i want to swipe up and swipe down the layout when clicking on the button not when OnTouchListener. For this i tried almost all examples in the online but i didn't get any solution according to my requirement. So, please help me to make OnTouchListener event when clicking on the button

My Code

Activity

import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.RelativeLayout;

public class SwipeUpActivity extends Activity {

    RelativeLayout rlSwipeHolder, rlSwipe1, rlSwipe2;
    private float startY;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_swipe_up);

        rlSwipeHolder = (RelativeLayout) findViewById(R.id.rl_swipe_up_holder);
        rlSwipe1 = (RelativeLayout) findViewById(R.id.rl_swipe_up_1);
        rlSwipe2 = (RelativeLayout) findViewById(R.id.rl_swipe_up_2);
        rlSwipe2.setVisibility(View.GONE);



        rlSwipeHolder.setOnTouchListener(new OnTouchListener() {

            @SuppressLint("ClickableViewAccessibility")
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                switch (event.getAction()) {
                case MotionEvent.ACTION_DOWN:
                    startY = event.getY();
                    break;
                case MotionEvent.ACTION_UP: {
                    float endY = event.getY();

                    if (endY < startY) {
                        System.out.println("Move UP");
                        rlSwipeHolder.setVisibility(View.VISIBLE);
                        rlSwipe1.setVisibility(View.VISIBLE);
                        rlSwipe2.setVisibility(View.VISIBLE);
                    } else {
                        rlSwipeHolder.setVisibility(View.VISIBLE);
                        rlSwipe1.setVisibility(View.VISIBLE);
                        rlSwipe2.setVisibility(View.GONE);
                    }
                }

                }
                return true;
            }
        });
    }
}

Layout

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#FFFFFF"
    tools:context="com.app.swipeup.SwipeUpActivity" >

    <RelativeLayout
        android:id="@+id/rl_swipe_up_holder"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:background="#0000FF"
        android:padding="5dp" >

        <RelativeLayout
            android:id="@+id/rl_swipe_up_1"
            android:layout_width="match_parent"
            android:layout_height="30dp"
            android:background="#585858" >
        </RelativeLayout>

        <RelativeLayout
            android:id="@+id/rl_swipe_up_2"
            android:layout_width="match_parent"
            android:layout_height="30dp"
            android:layout_below="@+id/rl_swipe_up_1"
            android:background="#FE2E2E" >
        </RelativeLayout>
    </RelativeLayout>

</RelativeLayout>

Edit

Like this video i have to open the layout and close the layout (or swipe up the layout and swipe down the layout) when clicking the button

Bahu
  • 1,516
  • 2
  • 28
  • 49
  • Do you mean ? you want to enable and disable swipe up down. when you click on button ? – vabhi vab Apr 28 '16 at 06:07
  • @vabhi vab: Yes exactly – Bahu Apr 28 '16 at 06:26
  • @vabhi vab: i miss understood your above comment. So, i directed you in miss direction. My requirement is: When we clicking on the button we have to swipe up the layout and again clicking on the same button we have to swipe down the layout – Bahu Apr 28 '16 at 14:13

2 Answers2

0

Create object of your onTouchListenr.

 OnTouchListener ot = new OnTouchListener(){-------- YOUR CODE ---}

on enable button click

rlSwipeHolder.setOnTouchListener(ot);

on disable button clik

 rlSwipeHolder.setOnTouchListener(null);
vabhi vab
  • 419
  • 4
  • 11
  • Hi, i changed the code like https://www.dropbox.com/s/tyi55f6lx5ms7j1/SwipeUpActivity.java but layout is not swipe up or down when clicking – Bahu Apr 28 '16 at 08:24
  • Take two buttons in your layout android set OnClickListener on both in one button btn1.setOnTouchListener(null); and in second btn2.setOnTouchListener(ot); When you will click on btn1 and then swipe view it will not swipe and after clicking btn2. your view swipe. Take these two buttons outside your swiping rl_swipe_up_1 and rl_swipe_up_2. – vabhi vab Apr 28 '16 at 08:46
  • Hi, i think you miss understood my question (I think it's my problem). I implemented as you said in the comment. At that time when clicking on the btn1 i'm not able to swipe and when clicking on btn2 i'm able to swipe. So, it's working as you understood. But my requirement is when we clicking on button (Take only one btn) our layout have to swipe up automatically and we click again on that button, then my layout have to swipe down – Bahu Apr 28 '16 at 10:16
  • For this you will have to use toggle button or spinner with two options (None,Enable,Disable), or you can also use normal button. Add a button to ur layout. set its text to Enable. set onClick on that button. Now inside onClick() method add this :- if(btn.getText().toString().equals("Disable")){ view.setOnTouchListener(null); btn.setText("Enable"); } if(btn.getText().toString().equals("Enable"){ view.setOnTouchListener(ot); btn.setText("Disable");} – vabhi vab Apr 28 '16 at 14:51
  • Bro you not understood what i'm saying. Check this video once https://www.dropbox.com/s/2gjrhi02rhi0u8x/swipeup.mp4?dl=0. Like this i have to open the layout and close the layout (or swipe up the layout and swipe down the layout) when click the button – Bahu Apr 28 '16 at 15:56
0

This is not the exact solution for this, but i'm posting here, because it may help others.

Answer

I saw lot of solutions for setOnTouchListener after setOnClickListener but i didn't get. So, i followed the animation for layout up and down as discussed in this link

and i did minute changes to the code as

Activity

import android.animation.Animator;
import android.animation.ValueAnimator;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.widget.RelativeLayout;


public class SwipeUpActivity extends Activity {

    RelativeLayout mRelativeLayout;
    RelativeLayout mRelativeLayoutHeader;
    ValueAnimator mAnimator;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_swipe_up);

        mRelativeLayout = (RelativeLayout) findViewById(R.id.expandable);
        // mLinearLayout.setVisibility(View.GONE);
        mRelativeLayoutHeader = (RelativeLayout) findViewById(R.id.header);

        // Add onPreDrawListener
        mRelativeLayout.getViewTreeObserver().addOnPreDrawListener(
                new ViewTreeObserver.OnPreDrawListener() {

                    @Override
                    public boolean onPreDraw() {
                        mRelativeLayout.getViewTreeObserver().removeOnPreDrawListener(this);
                        mRelativeLayout.setVisibility(View.GONE);

                        final int widthSpec = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
                        final int heightSpec = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
                        mRelativeLayout.measure(widthSpec, heightSpec);
                        mAnimator = slideAnimator(0, mRelativeLayout.getMeasuredHeight());
                        return true;
                    }
                });

        mRelativeLayoutHeader.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                if (mRelativeLayout.getVisibility() == View.GONE) {
                    expand();
                } else {
                    collapse();
                }
            }
        });
    }

    private void expand() {
        // set Visible
        mRelativeLayout.setVisibility(View.VISIBLE);

        mAnimator.start();
    }

    private void collapse() {
        int finalHeight = mRelativeLayout.getHeight();

        ValueAnimator mAnimator = slideAnimator(finalHeight, 0);

        mAnimator.addListener(new Animator.AnimatorListener() {
            @Override
            public void onAnimationEnd(Animator animator) {
                // Height=0, but it set visibility to GONE
                mRelativeLayout.setVisibility(View.GONE);
            }

            @Override
            public void onAnimationStart(Animator animator) {
            }

            @Override
            public void onAnimationCancel(Animator animator) {
            }

            @Override
            public void onAnimationRepeat(Animator animator) {
            }
        });
        mAnimator.start();
    }

    private ValueAnimator slideAnimator(int start, int end) {

        ValueAnimator animator = ValueAnimator.ofInt(start, end);

        animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator valueAnimator) {
                // Update Height
                int value = (Integer) valueAnimator.getAnimatedValue();

                ViewGroup.LayoutParams layoutParams = mRelativeLayout.getLayoutParams();
                layoutParams.height = value;
                mRelativeLayout.setLayoutParams(layoutParams);
            }
        });
        return animator;
    }
}

Layout

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true">

        <RelativeLayout
            android:id="@+id/header"
            android:layout_width="match_parent"
            android:layout_height="64dp"
            android:background="#FCF">
        </RelativeLayout>

        <RelativeLayout
            android:id="@+id/expandable"
            android:layout_width="match_parent"
            android:layout_height="64dp"
            android:layout_below="@+id/header"
            android:background="#FFF">


        </RelativeLayout>

    </RelativeLayout>
</RelativeLayout>
Community
  • 1
  • 1
Bahu
  • 1,516
  • 2
  • 28
  • 49