Chris Sim
Chris Sim

Reputation: 4132

Android - SlidingPaneLayout - set width on the Pane

I'm using a SlidingPaneLayout in my activity:

<android.support.v4.widget.SlidingPaneLayout     xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/myslidingpanelayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

<!-- menu left -->
<LinearLayout
    android:id="@+id/menu"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:background="#8d305f"
    android:orientation="vertical" >
...
</LineareLayout>

<!-- main page right-->
<LinearLayout
    android:id="@+id/right_main"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:background="#fff"
    android:orientation="vertical" >
...
</LineareLayout>
</android.support.v4.widget.SlidingPaneLayout>

I want the menu to cover 3/4 of the page I want it to work on all the phones so I can't put for example

android:layout_width="300dp"

I want to calculate the screen width and set it to the left pane

Thank for your help

Upvotes: 2

Views: 6050

Answers (4)

Nicos44k
Nicos44k

Reputation: 131


I didn't get a close look on this issue but I think @Selecsosi answer is not complete.
It's just missing to set "layout_width" attributes to 0dp.

Complete answer would be (I'm using it that way in my project and it works):

<android.support.v4.widget.SlidingPaneLayout     
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/myslidingpanelayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <!-- menu left -->
    <LinearLayout
        android:id="@+id/menu"
        android:layout_width="0dp"
        android:layout_weight="3"
        android:layout_height="match_parent"
        android:background="#8d305f"
        android:orientation="vertical" >
    ...
    </LineareLayout>

    <!-- main page right-->
    <LinearLayout
        android:id="@+id/right_main"
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="match_parent"
        android:background="#fff"
        android:orientation="vertical" >
    ...
    </LineareLayout>
</android.support.v4.widget.SlidingPaneLayout>

Source: Accepted answer here -> "The layout_weight is used to determine how to spread the left-over space between the elements in a layout. Therefore, if you don't set the width to zero, the size of the content of the buttons will affect how big they are."

Upvotes: 0

Chris Sim
Chris Sim

Reputation: 4132

Thanks for you all I found this answer and it works with me:

    int width;
    int height;
    if (android.os.Build.VERSION.SDK_INT >= 13){
        Display display = getWindowManager().getDefaultDisplay();
        Point size = new Point();
        display.getSize(size);
        width = size.x;
        height = size.y;
    }else {
        Display display = getWindowManager().getDefaultDisplay(); 
        width = display.getWidth();  // deprecated
        height = display.getHeight();  // deprecated
    }
    if(width>0&&height>0){
        LinearLayout layout = (LinearLayout)findViewById(R.id.menu);
        // Gets the layout params that will allow you to resize the layout
        LayoutParams params = layout.getLayoutParams();
        // Changes the height and width to the specified *pixels*
        params.height = height;
        params.width = width*3/4;
    }

Upvotes: 4

Delyan
Delyan

Reputation: 8911

Apart from Selecsosi's answer, which is correct, there is also this view I wrote to always display the second item as a pane (ignoring the default show-side-by-side-if-the-fit behaviour). It can, as the name shows, wrap around the sliding view.

You can implement the behaviour you're after by either using a lot of @dimen resources and switching them based on swXXXdp-(port|land) or just setting the sliding view's width at runtime (something I'm reasonably certain you can do with the default layout as well).

Upvotes: 1

Selecsosi
Selecsosi

Reputation: 1646

Just looking up the doc for sliding pane, looks like it functions like a linear layout, and can use the

layout_weight

parameter to set a percentage based width since the parent viewgroup is match_parent

In the case of 3/4 = 75% you can

android:layout_weight="0.75"

From the android docs http://developer.android.com/reference/android/support/v4/widget/SlidingPaneLayout.html:

Like LinearLayout, SlidingPaneLayout supports the use of the layout parameter layout_weight on child views to determine how to divide leftover space after measurement is complete. It is only relevant for width. When views do not overlap weight behaves as it does in a LinearLayout.

When views do overlap, weight on a slideable pane indicates that the pane should be sized to fill all available space in the closed state. Weight on a pane that becomes covered indicates that the pane should be sized to fill all available space except a small minimum strip that the user may use to grab the slideable view and pull it back over into a closed state.

And from the LinearLayout docs http://developer.android.com/guide/topics/ui/layout/linear.html#Weight

Note: You will end up setting the layout_width parameter to 0dp since the view group will actually use the weight to lay the children out

Upvotes: 3

Related Questions