Chris J Davis
Chris J Davis

Reputation: 111

Android - how to set SlidingUpPanelLayout to half of the screen height?

I am using Umano's SlidingUpPanelLayout to provide a sliding panel that you can drag up from the bottom of the screen. You can find the documentation here:

https://github.com/umano/AndroidSlidingUpPanel

I want it so that the panel only fills half of the screen when it is extended. The documentation states this:

  • The main layout should have the width and the height set to match_parent.
  • The sliding layout should have the width set to match_parent and the height set to either match_parent, wrap_content or the max desireable height. If you would like to define the height as the percetange of the screen, set it to match_parent and also define a layout_weight attribute for the sliding view.

This is my XML layout containing the sliding panel:

    <?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context="com.example.bleachedlizard.policeapp.mainactivity.MainActivity">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:popupTheme="@style/AppTheme.PopupOverlay" />

    </android.support.design.widget.AppBarLayout>

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <com.sothree.slidinguppanel.SlidingUpPanelLayout
            android:id="@+id/sliding_up_panel"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="bottom">

            <include layout="@layout/main_content" />

            <FrameLayout
                android:id="@+id/contact_details_frame"
                android:layout_width="match_parent"
                android:layout_height="match_parent">

            </FrameLayout>


        </com.sothree.slidinguppanel.SlidingUpPanelLayout>

    </FrameLayout>


</android.support.design.widget.CoordinatorLayout>

I've tried adding a layout_weight attribute to contact_details_frame but it doesn't work. In fact, Android Studio won't auto-complete when I try to add a layout_weight attribute (I'm guessing because it needs to be inside a LinearLayout in order for that attribute to be valid there). I've tried adding a LinearLayout, both on its own and also with an empty View to try to take up the top half of the screen, but still nothing.

Can someone tell me how to get the SlidingUpPanelLayout to only take up half of the screen?

Upvotes: 4

Views: 2761

Answers (1)

hassan moradnezhad
hassan moradnezhad

Reputation: 577

I test a lot of ways , but non of them works, But here is a another way. hope that it helps :)

First: just change sothree:umanoPanelHeight value to 0dp that it'll hide bottom bar of SlidingUpPanelLayout

<com.sothree.slidinguppanel.SlidingUpPanelLayout
        // your code
        sothree:umanoPanelHeight="0dp">

Second: make a barlayout or button [Not in dragView]
(to open SlidingUpPanelLayout, instead of using the bar that SlidingUpPanelLayout shows itself)

<Button
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/btn_open_slide_panel"
    android:text="open SlidingUpPanelLayout "/>

Third: in SetOnClickListener method of your view(that we made in second part)

  1. Call method setAnchorPoint() and pass 0.5f as argument
  2. Use ANCHORED as PanelState
slidingUpPanelLayout.setAnchorPoint(0.5f);
slidingUpPanelLayout.setPanelState(SlidingUpPanelLayout.PanelState.ANCHORED);

hope it helps :)

Upvotes: 4

Related Questions