How to move a layout content outside the screen android

10,058

Solution 1

To move the 'Screen' you'll need to call getLayoutParams() on the View, modify it as necessary and then call setLayoutParams() on the View.

But for a great tutorial to how to implement a slide in menu see here:-

http://android.cyrilmottier.com/?p=658

To add further help, here's a layout that achieve's what I think you're after:-

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
 >

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/blue" >


</LinearLayout>

<LinearLayout
    android:layout_width="200dip"
    android:layout_height="match_parent"
    android:layout_marginLeft="-100dip"
    android:background="@color/grey_divider"
    android:orientation="vertical" >

    <Button
        android:id="@+id/button1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Button" />

</LinearLayout>

This will result in the second LinearLayout appearing 50% off the left hand side of the screen. FYI the LinearLayout you're moving will need an absolute width. But you could define it to FILL_PARENT in your xml, and then get the width and set it to this as an absolute value in code the first time you set the margin to a negative value.

Hope this helps.

Solution 2

Use parent as liner layout, set weight for both left and right layouts view. put your left view in horizontalScrollview which need to be scrolled out of screen.

Share:
10,058
user924941
Author by

user924941

Updated on June 09, 2022

Comments

  • user924941
    user924941 over 1 year

    I need to move the content of the screen to the left, so I can make room for the slide menu on the right enter image description here

    Here is the code:

    // modify content layout params
        try {
            content = ((LinearLayout) act.findViewById(android.R.id.content)
                    .getParent());
        } catch (ClassCastException e) {
            /*
             * When there is no title bar
             * (android:theme="@android:style/Theme.NoTitleBar"), the
             * android.R.id.content FrameLayout is directly attached to the
             * DecorView, without the intermediate LinearLayout that holds the
             * titlebar plus content.
             */
            content = (FrameLayout) act.findViewById(android.R.id.content);
        }
    FrameLayout.LayoutParams pr = (android.widget.FrameLayout.LayoutParams) content
                        .getLayoutParams();
                pr.rightMargin = menuSize;
                content.setLayoutParams(pr);
    // add the slide menu to parent
        parent = (FrameLayout) content.getParent();
    
        try {
            parent = (FrameLayout) content.getParent();
        } catch (ClassCastException e) {
            /*
             * Most probably a LinearLayout, at least on Galaxy S3.
             */
            LinearLayout realParent = (LinearLayout) content.getParent();
            parent = new FrameLayout(act);
            realParent.addView(parent, 0); // add FrameLayout to real parent of
                                            // content
            realParent.removeView(content); // remove content from real parent
            parent.addView(content); // add content to FrameLayout
        }
    
        LayoutInflater inflater = (LayoutInflater) act
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        menu = inflater.inflate(R.layout.slidemenu, null);
    
        FrameLayout.LayoutParams lays = new FrameLayout.LayoutParams(menuSize,
                FrameLayout.LayoutParams.MATCH_PARENT, Gravity.RIGHT);
        lays.setMargins(0, statusHeight, 0, 0);
        menu.setLayoutParams(lays);
        parent.addView(menu);
    

    but what I get is:
    enter image description here

    the content is just resized to fit the screen, how can I make this work? Btw I can't modify the content layout its a relative layout with a surfaceview, but it should work with any layouts because I modify the DecorView which is the top view container

  • user924941
    user924941 almost 11 years
    i do this here FrameLayout.LayoutParams pr = (android.widget.FrameLayout.LayoutParams) content .getLayoutParams(); pr.rightMargin = menuSize; content.setLayoutParams(pr); and the menu and animations and all are ready i just need this fix
  • Ryan
    Ryan almost 11 years
    @user924941 I updated my answer yesterday with an example, does this help answer your question?