1

For example, i have a scrollView and a LinearLayout within one of my XML files. The LinearLayout is a small bar at the bottom of the screen used as navigation (70dp high).

I want to make it so that if a user swipes up/down within this LinearLayout it will display another LinearLayout just beneath it. Sort of like an expanded menu.

I have the extended layout created and placed and the animations are working. But i can't get it to swipe up/down within the LinearLayout.

How do i do this? Thanks in advance!

EDIT: Code Example

This is an example of the XML

<WebView  xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/wv"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_above="@+id/llSM"
    />
<LinearLayout
        android:layout_width="match_parent"
        android:layout_height="70dp"
        android:id="@+id/llSM"
        android:orientation="horizontal"
        android:layout_above="@+id/llNav"
        android:layout_alignWithParentIfMissing="true"
        >

        <ImageButton
            android:id="@+id/bWebsite"
            android:layout_width="70dp"
            android:layout_height="70dp"
            android:src="@drawable/nhome"
            android:clickable="true"
            android:scaleType="fitXY"
            android:background="#FFFFFF"
            android:layout_weight="1" />

        <ImageButton
            android:id="@+id/bShop"
            android:layout_width="70dp"
            android:layout_height="70dp"
            android:src="@drawable/nhome"
            android:clickable="true"
            android:scaleType="fitXY"
            android:background="#FFFFFF"
            android:layout_weight="1" />

        <ImageButton
            android:id="@+id/bFacebook"
            android:layout_width="70dp"
            android:layout_height="70dp"
            android:src="@drawable/nfacebook"
            android:clickable="true"
            android:scaleType="fitXY"
            android:background="#FFFFFF"
            android:layout_weight="1" />
</LinearLayout>
<LinearLayout
    android:id="@+id/llNav"
    android:layout_width="match_parent"
    android:layout_height="70dp"
    android:orientation="horizontal"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="false"
    android:layout_alignParentTop="false"
    android:layout_centerInParent="true"
    android:gravity="bottom|center_horizontal"
    >

    <ImageButton
        android:id="@+id/bHome"
        android:layout_width="70dp"
        android:layout_height="70dp"
        android:src="@drawable/nhome"
        android:clickable="true"
        android:scaleType="fitXY"
        android:background="#FFFFFF"
        android:layout_weight="1"
        />

    <ImageButton
        android:id="@+id/bContact"
        android:layout_width="70dp"
        android:layout_height="70dp"
        android:src="@drawable/ncontactus"
        android:clickable="true"
        android:layout_toRightOf="@+id/bHome"
        android:background="#FFFFFF"
        android:scaleType="fitXY"
        android:layout_weight="1"
        />

    <ImageButton
        android:id="@+id/bBooking"
        android:layout_width="70dp"
        android:layout_height="70dp"
        android:src="@drawable/onlinebooking"
        android:clickable="true"
        android:layout_toRightOf="@+id/bContact"
        android:background="#FFFFFF"
        android:scaleType="fitXY"
        android:layout_weight="1"
        />
</LinearLayout>

And this is the Main example:

protected void onCreate(Bundle savedInstanceState) {
    //Remove title bar
    this.requestWindowFeature(Window.FEATURE_NO_TITLE);
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_external);

    ll = (LinearLayout) findViewById(R.id.llNav);
    ll.setVisibility(View.GONE);

    animUp = AnimationUtils.loadAnimation(this, R.anim.anim_up);
    animDown = AnimationUtils.loadAnimation(this, R.anim.anim_down);

    NavBar(); //This just loads the buttons and their intents
    Links(); //This is for the second linear layout loading social media
    myWebView.setWebViewClient(new WebViewClient());
    myWebView.getSettings().setJavaScriptEnabled(true);
    myWebView.getSettings().setRenderPriority(WebSettings.RenderPriority.HIGH);
    myWebView.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
    myWebView.loadUrl("url");


    ll2 = (LinearLayout) findViewById(R.id.llSM);

    ll2.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            if(event.getAction() == MotionEvent.ACTION_DOWN){
                startY = event.getY();
                //hello you just touched me!!!
            } else if(event.getAction() == MotionEvent.ACTION_UP) {
                float endY = event.getY();
                if ((endY < startY) && (a==0)) {
                    ll.setVisibility(View.VISIBLE);
                    ll.startAnimation(animUp);
                    a = a+1;
                }
                else {
                    if ((endY > startY) && (a>=1)){
                        ll.startAnimation(animDown);
                        ll.setVisibility(View.GONE);
                        a = 0;
                    }
                }
            }


            return false;
        }
    });

}

The WebView scrolls within it so it will not work in there. I want to be able to swipe on the linearlayout labelled "llSM" to bring up the "llNav" bar just beneath it and push everything up. I also need to click the buttons on the "llSM" bar to navigate between different web views.

Noodelz
  • 143
  • 1
  • 11

1 Answers1

0

The LinearLayout you are talking about is a View + Group , so call View.OnTouchListener on it

thatParticularLinearLayout.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
    if(event.getAction() == MotionEvent.ACTION_DOWN){

        //hello you just touched me!!! 
        return true;
    }
    return false;
  }
});

EDIT 1

your code is great sir, but there's something in android called onInterceptTouchEvent() what is does is perfect for your case like i said linearLayout -(llSM and llNav) is a ViewGroup so you can call onInterceptTouchEvent() for it to intercept all touch events before his children even sees it.. maintain your touch event code..

okay so read more here, they even have code samples to support you.. and more , and more ..

EDIT 2 okay, from what you want try extending ViewFlipper i love it, its great. or ViewAnimator they will make your work easy.. lets see,

   public class Noodles extends ViewFlipper { // for your situation i will say extend this.. i am not saying this is
// the only way but that's the best and easiest i can think of..
// this viewgroup will be in a viewgroup maybe linear layout.. but those child linearlayout
// declare them like in different layouts okay..

Context context;// context to use
boolean intercept; // i dont know if i will use this but to indicate if it will intercept
View[] children;
float inx,iny = (float) 0.000; // this is the touched position that will be captured in on down in your touch
// i am not a OMG mathmatician but if you equate it to 0, its allways going to fail.. so the number of infinity
// depends on you.d thats just btw, so try it and see maybe i might be wrong

public Noodles(Context context,View[] children/* or LinearLayout[] children*/) { // construtor.. with the child layouts 
    super(context);     
    this.context = context;
    this.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT)); // setting params..
    this.setInAnimation(AnimationUtils.loadAnimation(context, someSickAnimationThatwILLSlidInFromTop)); // the animation should follow the pattern like this "R.anim.abc_slide_in_top" this is a default in animation
     /// but the spelling might differ
    this.setOutAnimation(AnimationUtils.loadAnimation(context, someSickAnimationThatwILLSlidOutFromTop));
    this.children = children;
    for(int i=0;i<children.length;i++){
        this.addView(children[i]);
    }
}

@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {

    // here you can use math catch the movement, something like 0,0 [x,y];
    // get the point loaction at with it was touched
    // when y increases it is coming down, when it decresse it is going up - all in respect to the intial
    // point.. you can intercept when y increase by a set of marging lets say like 20..or 10, because
    // eventually x will increase as is it is not straight line..but i can not rilly say, because i am conjuring from how it worksk,maybe im right maybe i am not   
    if(ev.getAction() == MotionEvent.ACTION_DOWN){
        Log.v("Noodles", "triggered");
        intercept = false;
    }
    if(ev.getAction() == MotionEvent.ACTION_MOVE){
        Log.v("Noodles", "noodles intercept fired"); 
        intercept = true;

        return true;
    }
    if(ev.getAction() == MotionEvent.ACTION_UP){
        if(intercept)  // yes i do 
            return true;
    }
    Log.v("Noodles", "noodles intercept ignored");
    return false;
}

@Override
public boolean onTouchEvent(MotionEvent ev) {

    // i think you can do the calculation 
    // to know if its going up or down respectively
    // use this.showNext(); for showing the next item from bottom to top the this.showPrevious vice versa
    Log.v("Noodles", "noodles touch fired");
    //i am trying the calculation, use and see if it works
    float inx,iny = 0;
    if(ev.getAction() == MotionEvent.ACTION_DOWN){
        Log.v("touchy", "triggered");
        iny = ev.getY();
        inx = ev.getX();
        Log.v("touchy", "x : " +String.valueOf(inx));
        Log.v("touchy", "y : " +String.valueOf(iny));
    }

    if(ev.getAction() == MotionEvent.ACTION_MOVE){
        int retval = Float.compare(iny, ev.getY());

        if(retval > 0){
            //noodles i am goin up 
            // this flips the current view out with upward movement animation you declared the in
            Log.v("touchy", "i am going up"); 
        }else if(retval < 0){
            // noodles i am goin down
            // this flips the current view out with downward movement
            Log.v("touchy", "i am going down");
        }
        // you can put your animations in their respective statements and rememba to call this.showNext(); in the upward movement and this.ShowPrevious(); the other
        iny = ev.getY();
        Log.v("touchy", "y : " +String.valueOf(iny));
        return true;
    }

    if(ev.getAction() == MotionEvent.ACTION_UP){
        // finger is up..
        //return true; //this goes agains the logic but i am thinking to prevent the return false argument, depends on how
        // you playing.. 
    }


}

}

Comming to your MainActivity instatiate this Noodles here

// in your actvity instatiate it
Noodles n = new Noodles(this,View1,View2); 
// then add it to your parent view or your layout that is to be your navigation

Please if you copy and paste this, you might see some unusual error, just correct them ok, because they are pretty chubby i thinking when i get to my laptop i will correct them for future use

Let me know if it helps..

Community
  • 1
  • 1
Elltz
  • 10,730
  • 4
  • 31
  • 59
  • I just tried to implement this, however it didn't work :( Could it be because there are image buttons within the linearlayout? I will edit my post above with code. – Noodelz Feb 03 '15 at 01:28
  • Code added. I've probably done everything wrong as i'm new to Android. Thanks for your help! – Noodelz Feb 03 '15 at 01:39
  • oh okay, so both llsm and llnav will both receive this swipe right? okay hold up@Noodelz – Elltz Feb 03 '15 at 01:44
  • i have editted it, and included links that have codes to help your case if you still find it tedious then when im pretty free, i will do codes @Noodelz , sorry for the delay SIr, things arising – Elltz Feb 03 '15 at 02:25
  • Thanks so much! I will give this a shot and see how I go :) If i can't get it working then i might be back haha. – Noodelz Feb 03 '15 at 03:17
  • If you're bored and feel like throwing up an example then it would be greatly appreciated :P I've read over the examples linked and tried to implement then, but failed a few different ways. Now i'm stuck scratching my head again ha. – Noodelz Feb 05 '15 at 02:40
  • i have edited to add some deep code Sir @Noodelz but the only problem is i spelt yur name with a "s" instead of "z". hope it helps – Elltz Feb 05 '15 at 20:26
  • After trying to implement this, i gave up and changed the whole design ha. Now i'm going with a Navigation Drawer instead and just swiping from the left. Thanks for your help though! It was greatly appreciated, but i just couldn't figure it out :/ – Noodelz Feb 11 '15 at 02:13