11

Hi I am a beginner for android and in my app I have to show SlidingPaneLayout at the right side but using my below code it's coming from left side.

Please help me.

How can I make it be at right side?

And second my requirement is my SlidingPaneLayout must be overlapped on Action bar but using my below xml code SlidingPaneLayout showing like my below image

please suggest me how can resolve this two problem

toolbar_layout:-

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/ColorPrimary"
    android:elevation="4dp"
    >

</android.support.v7.widget.Toolbar>

activity_sliding:-

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

    <LinearLayout
        android:layout_width="240dp"
        android:layout_height="match_parent"
        android:layout_gravity="right">

        <ListView
            android:id="@+id/MenuList"
            android:layout_width="match_parent"
            android:layout_height="match_parent" >
        </ListView>

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="right"
        android:background="#101010"
        android:orientation="vertical" >

        <ImageView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@drawable/android_robot" />
    </LinearLayout>

</android.support.v4.widget.SlidingPaneLayout>

main_layout:-

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

    <include
        android:id="@+id/tool_bar"
        layout="@layout/toolbar_layout">
    </include>

    <include
        android:id="@+id/SlidingPanel"
        layout="@layout/activity_sliding">
    </include>

</LinearLayout>

enter image description here

Elydasian
  • 2,016
  • 5
  • 23
  • 41
Krish
  • 4,166
  • 11
  • 58
  • 110

1 Answers1

6

In your manifest, add the following attribute to the opening <application> tag.

android:supportsRtl="true"

Then add this attribute to the opening SlidingPaneLayout tag in your layout.

android:layoutDirection="rtl"

And finally, move the tool_bar <include> element into the main content LinearLayout within the SlidingPaneLayout, and adjust the ImageView's height and weight.

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#101010"
    android:orientation="vertical">

    <include
        android:id="@+id/tool_bar"
        layout="@layout/toolbar_layout">
    </include>

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:background="@drawable/android_robot" />

</LinearLayout>

Please note that the child Views of the SlidingPaneLayout will inherit the rtl layoutDirection. This may cause problems in child Views if their layout behavior is affected by the direction. For example, a horizontally-oriented LinearLayout will lay out its children starting from the right. This is easily remedied by setting android:layoutDirection="ltr" on the affected Views.

Also note that this example hard codes the direction in the layout. If you need to support both LTR and RTL layouts application-wide, you'll need to do this programmatically, accounting for the device's default direction.

Mike M.
  • 38,532
  • 8
  • 99
  • 95
  • i am not sure if it helps. but check this http://stackoverflow.com/questions/23783496/how-to-slide-the-actionbar-along-with-the-navigationdrawer. – Raghunandan Apr 16 '16 at 06:58
  • but major problem is i want to show SliderLayoutPanel at right side – Krish Apr 16 '16 at 06:58
  • @Krish The RTL layout worked for me. You sure you've got the attributes in the right places? – Mike M. Apr 16 '16 at 07:00
  • @MikeM. he has android:layout_gravity="right for both linearlayouts – Raghunandan Apr 16 '16 at 07:02
  • @Krish If this doesn't work for you, you might be stuck with using a `DrawerLayout` with a right drawer `View`. I have a solution for that which is very similar to the `SlidingPaneLayout` behavior. – Mike M. Apr 16 '16 at 07:02
  • 1
    @Raghunandan From what I can tell, `SlidingPaneLayout` ignores the `layout_gravity` attributes. It just goes by the order of the child `View`s. But they should probably get rid of them anyway. – Mike M. Apr 16 '16 at 07:04
  • @MikeM. agreed its after all a viewgroup – Raghunandan Apr 16 '16 at 07:07
  • @Krish I added some notes to the answer that you might need to be aware of. – Mike M. Apr 16 '16 at 07:35
  • One thing is that, using android:layoutDirection="rtl" needs minimum sdk version 17 while I am using 13. Is this the only way for solving problem? – Davoud Mar 08 '17 at 08:08
  • @Patzu Almost certainly not, but this was a one-off solution for a `minSdkVersion` of 23. For anything below 17, apart from just writing your own class from scratch, you'd probably need to subclass `SlidingPaneLayout`, which might get a little hairy. – Mike M. Mar 08 '17 at 08:15