Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to move a layout content outside the screen android

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

like image 987
user924941 Avatar asked Jan 22 '13 13:01

user924941


1 Answers

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.

like image 131
Ryan Avatar answered Sep 21 '22 02:09

Ryan