- Android: выдвигающийся экран снизу
- Зависимости
- Создание макетов
- Container view
- Динамическое управление
- Прикрепление элементов к нижнему экрану
- Скрытие плавающей кнопки при скроле
- Android Bottom Sheet / Bottom Sheet Behavior
- Bottom Sheet Android Tutorial – Working with BottomSheet
- Types of Bottom Sheet
- #1 Persistent Bottom Sheet
- #2 Modal Bottom Sheets
- Full Screen Bottom Sheet
- Want to make a bottom sheet that can act as a full screen? Here you go!
- Android: Bottom sheet
- Adding resources
- Creating layouts
- Dynamic control
Android: выдвигающийся экран снизу
Данная статья является переводом статьи Emrullah Luleci, а также её продолжения.
Нижний экран (Здесь и далее под «нижним экраном/слоем» будет подразумеваться элемент bottom sheet — прим. пер.) — компонент, выезжающий снизу экрана, который используется для отображения дополнительного контента. Подробнее об этом элементе можно узнать на официальной сайте посвященном материальному дизайну.
Зависимости
Для использования этого элемента, добавьте последние версии библиотек поддержки в свой проект:
Создайте класс наследник от AppCompatActivity:
Создание макетов
Содержимое нижнего экрана
Для удобства воспользуемся макетами. Назовем файл с нижним слоем bottom_sheet.xml.
behavior_peekHeight: Определяет высоту видимой части.
behavior_hideable: Определяет, может ли нижний экран скрываться свайпом вниз.
Container view
Создайте CoordinatorLayout в качестве корневого вью. Добавьте в него прямым наследником bottom_sheet.xml. Элементы app_bar и activity_bottom_sheet_content не имеют прямого отношения к нижнему экрану, поэтому их можно заменить или удалить.
На данном этапе нижний экран должен работать примерно так:
Динамическое управление
Поведением и свойствами нижнего экрана можно также управлять динамически с помощью Java.
Прикрепление элементов к нижнему экрану
Также можно прикрепить вью к нижнему экрану, чтобы прикрепленный элемент перемещался одновременно с нижним слоем.
Добавим Floating Action Button в макет созданный выше. Новый компонент должен являться непосредственным наследником CoordinatorLayout также как и bottom_sheet. Для прикрепления элемента к нижнему экрану необходимо добавить app:layout_anchor с id вью нижнего экрана, а также app:layout_anchorGravity со значением top|end.
Теперь плавающая кнопка закреплена в верхнем углу нашего нижнего экрана и перемещается вместе с ним.
Скрытие плавающей кнопки при скроле
Для скрытия кнопки при скроле необходимо добавить слушатель к нижнему экрану и отображать/скрывать кнопку. Для начала найдем необходимые вью:
Для скрытия кнопки в момент начала скрола и отображения после полного сворачивания нижнего экрана, используйте следующее:
Результат обоих вариантов можно увидеть ниже:
Источник
Android Bottom Sheet / Bottom Sheet Behavior
Jan 2, 2019 · 4 min read
Android Bottom Sheet is a component that slides up from the bottom of the screen having multiple options. Here are the examples of the Bottom sheet from apps.
There are two types of bottom sheets, Persistent Bottom Sheet and Modal Bottom Sheet.
Persistent Bottom Sheet: This bottom sheet shows in-application content. It will be shown at the base of the screen with a portion of its part noticeable (we can characterize measurements in dp), it shows full substance in the wake of growing it. It has a littler height.
Modal Bottom Sheet: This botom sheet functions as a menu or discourse with choices, it implies this replaces menu or exchange. It has a higher height than the determined base sheet. For the most part, they are utilized for incorporating profound connecting picker activities.
Let’s start to implement the first persistent bottom sheet.
After creating a new project, open build.gradle file in app level, add support design dependency because the bottom sheet is the component of the android design support library.
Create bottom_sheet.xml layout:
Create conent_main.xml layout, that displays on-screen without the bottom sheet.
Now crea t e activity_main.xml including content and bottom sheet layout.
Now code in MainActivity.java
Now lets a look at Modal bottom sheet, the easiest way for the bottom sheet. Modal bottom sheet will be shown as an external dialog using BottomSheetDialog or BottomSheetDialogFragment.
As component we are using (BottomSheetDialog or BottomSheetDialogFragment)is a dialog itself. So its behavior is same as normal dialog, that you can touch outside to dismiss bottom sheet using this method.
We will show the bottom sheet dialog with the same UI that we used before.
That’s it. Now you have learnt the implementation of the bottom sheet. Find code her
Источник
Bottom Sheet Android Tutorial – Working with BottomSheet
Have you seen the Bottom Sheet Android Component? It slides up from the Bottom to display some more options to the user. I am pretty sure that you have already seen Bottom Sheet in Google Maps application.
Bottom Sheet Android
You can also use the Bottom Sheet in your application. And if you want to use it in your app, in this post, I will guide you about integrating the android Bottom sheet in your project.
You can also go through the official guide about the Bottom Sheet .
Types of Bottom Sheet
#1 Persistent Bottom Sheet
As clear from the name, it is persistent at the bottom of the screen. A user can view the full Bottom Sheet by dragging the sheet up vertically. The Bottom Sheet is slightly elevated, and it can display more options or app content to the user. For example, the picture that we saw above is an example of a persistent bottom sheet.
#2 Modal Bottom Sheets
Again as the name suggests, these sheets behave like Modals or dialogues. It shadows the activity or fragment when activated. And if we tap outside the Bottom Sheet, it is dismissed just like a modal. A user can also slide up and slide down to activate and deactivate the Bottom Sheet, respectively.
Modal Bottom Sheet
I hope you got that what exactly is the android bottom sheet and when we can use it in our application. Now let’s learn how we can implement it in an Android Studio Project.
Источник
Full Screen Bottom Sheet
Want to make a bottom sheet that can act as a full screen? Here you go!
Sep 7, 2019 · 3 min read
A bottom sheet is a good way of representing any kind of user-related info or to place options to get rapid actions from the user, like opening the camera, share, and gallery, etc.
But today we’ll learn how to make a beautiful bottom sheet that can be opened like a full screen by tapping a button. Excited?
Let’s get started!
What we are going to build is:
Let’s make a new project and add the following dependencies in your gradle.build .
I am using data bind i ng in this tutorial, so make sure you have added the data binding dependency in the build.gradle .
To make this bottom sheet, we first need to have a layout that will pop up with the bottom sheet.
Create a layout resource file in the layout directory and paste the following code:
Now you have a layout, it’s time to make a bottom sheet adapter.
BottomSheet.java extends from BottomSheetDialogFragment , so it has some override function, like onCreateDialog() .
- onCreateDialog(Bundle savedInstanceState) : Override to build your own custom dialog container.
We need to add bottom sheet behavior so we can hide and show the app bar according to the behavior of bottom sheet. We have a BottomSheetBehavior class for this.
According to the documentation, a bottom sheet may be defined as:
An interaction behavior plugin for a child view of CoordinatorLayout to make it work as a bottom sheet.
Bottom sheets have some constants.
- PEEK_HEIGHT_AUTO: Peek at the 16:9 ratio keyline of its parent.
- STATE_COLLAPSED: This indicates the bottom sheet is collapsed.
- STATE_EXPANDED: This indicates the bottom sheet is expanded.
BottomSheetBehavior has a nested class, BottomSheetCallback , which will help us achieve our required result, i.e., hiding or showing the app bar according to the bottom sheet’s state.
In this nested class, there are two abstract methods.
- onSlide(View bottomSheet, float slideOffset) , called when the bottom sheet is being dragged.
- onStateChanged(View bottomSheet, int newState) , called when the bottom sheet’s state changed.
So these are somehow relative methods that will help us to achieve our goal.
Источник
Android: Bottom sheet
Bottom sheet is a component that slides up from bottom of the screen to reveal more content. You can find more detailed information of Bottom Sheet on Google Material Design guidelines.
Adding resources
Add the latest appcompat and design support library to your project.
Make your activity extend AppCompatActivity.
Creating layouts
Bottom sheet content
We’ll include the layouts for the sake of simplicity. This is the layout that will contain the content of the bottom sheet. File is bottom_sheet.xml.
behavior_peekHeight: Defines the height of the visible part.
behavior_hideable: Defines if the bottom sheet can be hidden by swiping it down.
Add the CoordinatorLayout as the root view. Then include the bottom sheet view as the direct child of the CoordinatorLayout. The layouts app_bar and activity_bottom_sheet_content are some view references that are not related to the bottom sheet. So you can replace or remove them.
At this point the bottom sheet dialog should work like this.
Dynamic control
Behaviors and attributes of the bottom sheet dialog can be controlled dynamically in Java as well.
Источник