DrawerLayout End gravity. Drawer not opening with edge swipe when as child - android

I was working on the landscape layout for my activity. In landscape activity I happen to have the layout with xml code below.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="wrap_content"
android:layout_height="match_parent">
<FrameLayout
android:layout_width="200dp"
android:background="#android:color/holo_blue_bright"
android:layout_height="match_parent"/>
<android.support.v4.widget.DrawerLayout
android:layout_width="200dp"
android:layout_height="match_parent">
<FrameLayout
android:layout_width="match_parent"
android:background="#android:color/darker_gray"
android:layout_height="match_parent"/>
<FrameLayout
android:layout_width="match_parent"
android:layout_gravity="end"
android:background="#android:color/holo_red_light"
android:layout_height="match_parent"/>
</android.support.v4.widget.DrawerLayout>
</LinearLayout>
The Problem arising here is that the drawer is not opening by edge swipe. BTW using LTR layout so end is right. By java invocation for opening drawer, the Drawer opens and touch events are also handled. Also all drawers are Unlocked as well. But still Drawer doesn't opens by swipe from right edge when Drawer layout is child of linear layout with horizontal orientation. if the first Frame:ayout in the linearLayout is of zero width then the drawer works.
Whats wrong with the Drawer Layout? How Can I fix this?

From the Android reference:
DrawerLayout acts as a top-level container for window content
The DrawerLayout needs to be the root element of the view. It can't be nested in the LinearLayout

Related

Using NavigationView with SlidingPaneLayout

Per Google's Material Design guideline for Navigation drawer, to achieve Standard drawer for tablet or desktop devices, I would use NavigationView with SlidingPaneLayout for tablet devices instead of DrawerLayout for phone devices, which is to achieve Modal drawer.
I put a NavigationView as the first child view of SlidingPaneLayout. A problem occurred.
As you know SlidingPaneLayout's child views overlap if their combined width exceeds the available width in the SlidingPaneLayout. In this case, the child views expand to fill the available width in the SlidingPaneLayout. The user can slide the topmost view out of the way by dragging it back from the edge of the screen.
But my NavigationView's pane wouldn't slide. It just appears or disappears at maximum width without sliding animation.
How can I solve this?
<androidx.slidingpanelayout.widget.SlidingPaneLayout
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"
tools:context=".MainActivity">
<!-- The first child view becomes the left pane. When the combined
desired width (expressed using android:layout_width) would
not fit on-screen at once, the right pane is permitted to
overlap the left. -->
<com.google.android.material.navigation.NavigationView
android:id="#+id/navigationView"
android:layout_width="280dp"
android:layout_height="match_parent"
android:layout_gravity="start"
app:headerLayout="#layout/navigation_view_header"
app:menu="#menu/navigation_view" />
<!-- The second child becomes the right (content) pane. In this
example, android:layout_weight is used to expand this detail pane
to consume leftover available space when the
the entire window is wide enough to fit both the left and right pane.-->
<fragment
android:id="#+id/navigationHost"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="320dp"
android:layout_weight="1"
android:layout_height="match_parent"
app:navGraph="#navigation/main" />
</androidx.slidingpanelayout.widget.SlidingPaneLayout>
Add app:elevation="0dp" to the NavigationView.
<com.google.android.material.navigation.NavigationView
android:id="#+id/navigationView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="start"
app:elevation="0dp"
app:headerLayout="#layout/navigation_view_header"
app:menu="#menu/navigation_view" />
If you inspect the layout, you can notice the NavigationView has elevation of 16dp by default. This causes the problem. It is probably that SlidingPaneLayout handles the two child views under the condition that they have the same elevations (0dp).
So a solution is to override NavigationView's default elevation (16dp) with 0dp.
Another solution is to wrap NavigationView with a FrameLayout or some ViewGroup.
<FrameLayout
android:layout_width="280dp"
android:layout_height="match_parent">
<com.google.android.material.navigation.NavigationView
android:id="#+id/navigationView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="start"
app:headerLayout="#layout/navigation_view_header"
app:menu="#menu/navigation_view" />
</FrameLayout>
Then, you should specify layout_width with FrameLayout and NavigationView's layout_width to match_parent.
In spite of NavigationView's default elevation of 16dp, for its parent ViewGroup's elevation is 0dp, SlidingPaneLayout can properly handle.

How i can animate navigation drawer resize itself to half screen?

the whole navigation bar comes front and the main activity shifts towards left and gets shrink. Please anyone can tell me the easiest way to do this??
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:layout_width="250dp" <!-- Set the width -->
android:layout_height="match_parent">
#Your view
</FrameLayout>
</android.support.v4.widget.DrawerLayout>

Android BottomSheet like Share Sheet with anchored view at bottom

I'm trying to use the support library's BottomSheetDialogFragment to replicate the standard sheet that shows when you tap a Share button (see below). How would I achieve a similar layout, where there's a title at the top, independently scrollable content in the center, but an bottom anchored view with buttons that always stay on top.
You need to build a custom layout for the bottomSheet, for example share_bottom.xml. Within that layout you could
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/bottomSheet"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
app:layout_behavior="#string/bottom_sheet_behavior"
app:behavior_hideable="true"
app:behavior_peekHeight="200dp">
<TextView
android:layout_width="match_parent"
android:text="header"
android:layout_height="wrap_content"/>
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"/>
<LinearLayout
android:layout_weight="0"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
</LinearLayout>
And then include it at he bottom of your fragment's layout:
After that you can control the visibility of this sheet
//retrieve the bottomsheet
bottomSheet = (LinearLayout) findViewById(R.id.bottomSheet);
//get the behaviour controller
bsb = BottomSheetBehavior.from(bottomSheet);
//hide the sheet
bsb.setState(BottomSheetBehavior.STATE_HIDDEN);
//showthe sheet
bsb.setState(BottomSheetBehavior.STATE_EXPANDED);
I was also having the same problem. I solved using these steps.
Make FrameLayout as your root layout.
Include the view to be anchored as second child and first child should contain all the content of activity.
Animate the second child to be visible when bottom sheet is expanded and make it invisible when bottom sheet is collapsed.
The independently scrolling content inside bottom sheet can be acheived by using a scroll view or nested scroll view inside bottom sheet.
To dim background refer this link
This is just a workaround. Basically what I did is replicate the persistent bottom sheet to behave like modal sheet.

Android UI: align a fragment above another fragment that is aligned bottom

I haven't developed for Android in more than a year and I'm a bit rusty with it. I'm trying to setup a kinda simple UI: a bottom bar at the bottom of the screen and a fragment above it (but without filling the whole height). Something like this:
I thought this would be quite simple, but after a while struggling with it I can't manage to make it work without some "hacks".
The bottom bar is also implemented as a Fragment. This is my XML:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<fragment
android:id="#+id/bottomBar"
android:name="com.mytestpackage.BottomBarFragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true" />
<FrameLayout
android:id="#+id/fragmentContainer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="#id/bottomBar"
android:layout_alignParentBottom="true" />
</RelativeLayout>
The Fragment in fragmentContainer is dynamically loaded from code. With the code paste above, fragmentContainer is aligned bottom but it's not above bottom bar, they're overlapped. If I remove the alignParentBottom from fragmentContainer, then it's placed in top of the screen.
Like I said, I found two "hacks" to solve it but I don't like them much:
1- Set a padding/margin bottom to fragmentContainer.
2- Use a filler empty layout on top of the screen and set fragmentContainer to be below that one.
Is there any way to achieve the layout I want without having to use some tricks like the ones I said?
Thanks!
Add to the relative layout:
android:gravity="bottom"
Ah, and android:orientation="vertical" is meaningless for RelativeLayout
A simpler solution would be to use a LinearLayout with vertical orientation and gravity bottom instead of the RelativeLayout.

Slide Up Panel with View Pager Title Strip

I am using this LIBRARY for my slide up panel. every thing is working fine with a simple layout.
Now my question is , how can I insert a pager title strip in that panel so that I can make it my View pager working on it with multiple fragments.
First image is title strip at bottom
second is slide up and sliding fragments
From your previous post, I'm assuming you're using this library (https://github.com/umano/AndroidSlidingUpPanel).
This library requires that you have 2 children views. The first one being the main layout and the second one the actual sliding view. Now, your sliding view is just a placeholder, so you can place anything you want in there. If you want to add a ViewPager, this is how you can do it.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<SlidingUpPanelLayout
android:layout_width="match_parent"
android:layout_height="match_parent" >
<!-- Top Panel -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent" >
</LinearLayout>
<!-- Sliding Panel -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<android.support.v4.view.ViewPager
android:layout_width="match_parent"
android:layout_height="match_parent" >
</android.support.v4.view.ViewPager>
</LinearLayout>
</SlidingUpPanelLayout>
</RelativeLayout>
What we have here is the main layout of our Activity (RelativeLayout) and we're adding the SlidingPanelLayout to it. Inside this Layout, we've defined our main layout to be a LinearLayout (Top Panel) and a second LinearLayout (Sliding Panel) which is the actual sliding view. Now, all we need to do is add a ViewPager to this sliding panel.

Categories

Resources