Menu like SKOUT and Sliding from One View to Another by touch in Android
Asked Answered
S

4

29

I want to implement a sliding Menu like FB or G+ app and I have found some sample code from FB Menu Demo and https://github.com/jfeinstein10/SlidingMenu

These are good to begin with, But I need something extra from them. Like here it works only on the click of the menu button but I want to move it by gestures as well. I want to have the behavior that there is a center view and on moving that center towards the right, one view will appear and on moving that towards left, the menu will appear. Say there are three views A,B,C and when I swipe C towards left then A appear and when I swipe C towards right then B appear. C is in the middle of A and B.

1.Middle view moves towards right

That screen is in the middle Move towards right Menu appear

2.Move the middle view towards left side

Move towards the left by touch Move towards leftThird view

Now my question is: What are the best practices to develop the views like that. I have heard from someone that I should use fragments and View pager as well. So how can I develop this? Is there any sample implementation done by anyone ? Any help and suggestions are appreciated.

For reference see this app which uses this type of sliding b/w views Skout app

Shankle answered 26/10, 2012 at 7:28 Comment(3)
You should read android.cyrilmottier.com/?p=701, author of GreenDroidVercelli
Well, just thinking, Add your left view and your right view above each other. Above them, add a ViewPager that has 3 items and the middle one would be your center view (the others blank). Once the user swipes to the left/right, toggle visibilitys as needed, and, I guess, the view you want will be there.Personal
Sliding Menu is the core android API for above requirement See SlidingPanel From Left to RightGuck
K
17

The simplest solution may be to use android-undergarment, which has bezel swiping built in, based on the project README:

The user will also be able to control the drawer by bezel swiping from the left side of the screen to open the drawer and doing the same from the right to close it. If you want to prevent this touch functionality, you can call setDrawerEnabled(false).

Kathe answered 29/10, 2012 at 18:38 Comment(3)
Thanks for the response. Is i have to use that as a library project.Also what i want to do is like there are three views A,B,C. When i move the C view to the left side then the A appear and if i move the C to right then B appear. What i found till now is just with two view. So please guide meShankle
This is a perfect Solution ... and simple oneArie
NOTE: Undergarment is deprecated. Please use the official DrawerLayout implementation located in the v4 Support Library. developer.android.com/training/implementing-navigation/…Quietude
B
11

You can simply use TranslateAnimationon the view which you wish to move along with a pop up for fade and another pop up window for you menu. I have implemented it in my application, and it works like a charm.
This image shows you when you need the animation, and other components


Code:

public class SlidingOptionsMenuActivity extends Activity {

    /**
     * Signifies that the menu is already visible
     */
    boolean alreadyShowing = false;
    /**
     * Width of the current window
     */
    private int windowWidth;
    /**
     * Height of the current window
     */
    private int windowHeight;
    /**
     * Reference of the {@link PopupWindow} which dims the screen
     */
    private PopupWindow fadePopup;
    /**
     * The translate animation
     */
    private Animation ta;
    /**
     * The view which needs to be translated
     */
    private RelativeLayout baseView;
    /**
     * Reference of the {@link LayoutInflater}
     */
    LayoutInflater inflater;

    /*
     * (non-Javadoc)
     * 
     * @see android.app.Activity#onCreate(android.os.Bundle)
     */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        Display display = getWindowManager().getDefaultDisplay();
        windowWidth = display.getWidth();
        windowHeight = display.getHeight();
        inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        findViewById(R.id.btnOptions).setOnClickListener(new OnClickListener() {

            /*
             * (non-Javadoc)
             * 
             * @see android.view.View.OnClickListener#onClick(android.view.View)
             */
            @Override
            public void onClick(View v) {
                if(!alreadyShowing){
                    alreadyShowing = true;
                    openSlidingMenu();
                }
            }
        });
    }


    /**
     * Fades the entire screen, gives a dim background
     */
    private void showFadePopup() {
        final View layout = inflater.inflate(R.layout.fadepopup, (ViewGroup) findViewById(R.id.fadePopup));
        fadePopup = new PopupWindow(layout, windowWidth, windowHeight, false);
        fadePopup.showAtLocation(layout, Gravity.NO_GRAVITY, 0, 0);
    }

    /**
     * Opens the sliding Menu
     */
    private void openSlidingMenu() {
        showFadePopup();
        // The amount of view which needs to be moved out. equivalent to the
        // width of the menu
        int width = (int) (windowWidth * 0.6f);
        translateView((float) (width));
        int height = LayoutParams.FILL_PARENT;
        // creating a popup
        final View layout = inflater.inflate(R.layout.option_popup_layout,(ViewGroup) findViewById(R.id.popup_element));

        final PopupWindow optionsPopup = new PopupWindow(layout, width, height, true);
        optionsPopup.setBackgroundDrawable(new PaintDrawable());

        optionsPopup.showAtLocation(layout, Gravity.NO_GRAVITY, 0, 0);

        optionsPopup.setOnDismissListener(new PopupWindow.OnDismissListener() {

            public void onDismiss() {
                //Removing the fade effect
                fadePopup.dismiss();    
                //to clear the previous animation transition in
                cleanUp();
                //move the view out
                translateView(0);
                //to clear the latest animation transition out
                cleanUp();
                //resetting the variable
                alreadyShowing = false;
            }
        });
    }

    /**
     * This method is responsible for view translation. It applies a translation
     * animation on the root view of the activity
     * 
     * @param right The position to translate to
     */
    private void translateView(float right) {

        ta = new TranslateAnimation(0f, right, 0f, 0f);
        ta.setDuration(300);
        ta.setFillEnabled(true);
        ta.setFillAfter(true);

        baseView = (RelativeLayout) findViewById(R.id.baseView);
        baseView.startAnimation(ta);
        baseView.setVisibility(View.VISIBLE);
    }

    /**
     * Basic cleanup to avoid memory issues. Not everything is release after
     * animation, so to immediately release it doing it manually
     */
    private void cleanUp(){
        if (null != baseView) {
            baseView.clearAnimation();
            baseView = null;
        }
        if (null != ta) {
            ta.cancel();
            ta = null;
        }
        fadePopup = null;
    }
} //END of Class
//END of file

Hope this would help.

The actual screenshot.

Badderlocks answered 2/11, 2012 at 9:21 Comment(3)
I have to implement like the Skout app. The view can be move in both sides left and right by gesture and on button click. Can you provide me the demo.Shankle
I'll create a demo and get back to you.Badderlocks
@Naresh: Please let me know if you are having trouble with this.Badderlocks
S
10

Another open source library I've found that's been very good is SlidingMenu. It should suit your needs as you can open and close the drawer by a "Menu" click and by bezel swiping. I found integrating this along with an Actionbar library like ActionBarSherlock or johannilsson's android-actionbar library is simply a matter of changing a line or two of code in the library project. The Readme for the SlidingMenu library explains how to integrate with the ABSherlock library.

One thing worth noting is that the SlidingMenu example project demonstrates a number of different drawer open-close animations. These are some of the best animations I've seen for this style of menu/navigation.

Stearne answered 2/11, 2012 at 0:34 Comment(0)
Q
-1

There is an Official Way ... useful and light (by use of v4 Support Library):

Creating a Navigation Drawer:

https://developer.android.com/training/implementing-navigation/nav-drawer.html

Quietude answered 21/6, 2017 at 11:53 Comment(1)
A link to a solution is welcome, but please ensure your answer is useful without it: add context around the link so your fellow users will have some idea what it is and why it’s there, then quote the most relevant part of the page you're linking to in case the target page is unavailable. Answers that are little more than a link may be deleted.Defoliant

© 2022 - 2024 — McMap. All rights reserved.