Chuyển đến nội dung chính

Using BottomSheetDialogFragment with Material Design Guideline


As per the material design guideline for BottomSheet, there are two types of BottomSheets:
  1. Modal bottom sheets: It Can be implemented using BottomSheetDialog or BottomSheetDialogFragment. Elevation is higher than the app. It is fundamentally acting as the dialog.
  2. Persistent bottom sheets: It can be implemented using BottomSheetBehavior in conjunction with a CoordinatorLayout.
In this article, we are going to learn how to implement the BottomSheetDialogFragment to use the bottom sheet as a modal dialog.
Final Outcome:




So lets get started…
Step 1: Create the layout file for bottom sheet (consider the material design guideline while building the guideline):
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginBottom="@dimen/dp_8"
    android:layout_marginTop="@dimen/dp_8"
    android:orientation="vertical">
    <
TextView
        android:id="@+id/tv_bottom_sheet_heading"
        android:layout_width="wrap_content"
        android:layout_height="@dimen/dp_56"
        android:layout_marginEnd="@dimen/dp_16"
        android:layout_marginStart="@dimen/dp_16"
        android:gravity="center"
        android:text="@string/bottom_sheet_option_heading"
        android:textColor="@color/md_bottom_sheet_title_color"
        android:textSize="16sp" />
    <
TextView
        android:id="@+id/tv_btn_add_photo_camera"
        android:layout_width="match_parent"
        android:layout_height="@dimen/dp_48"
        android:layout_marginEnd="@dimen/dp_16"
        android:layout_marginStart="@dimen/dp_16"
        android:backgroundTint="@android:color/white"
        android:drawablePadding="@dimen/dp_32"
        android:drawableStart="@drawable/ic_add_a_photo"
        android:drawableTint="@color/md_bottom_sheet_text_color"
        android:gravity="start
center_vertical"
        android:text="@string/bottom_sheet_option_camera"
        android:textColor="@color/md_bottom_sheet_text_color"
        android:textSize="16sp" />
    <
TextView
        android:id="@+id/tv_btn_add_photo_gallery"
        android:layout_width="match_parent"
        android:layout_height="48dp"
        android:layout_marginEnd="@dimen/dp_16"
        android:layout_marginStart="@dimen/dp_16"
        android:backgroundTint="@android:color/white"
        android:drawablePadding="@dimen/dp_32"
        android:drawableStart="@drawable/ic_gallery_photo"
        android:drawableTint="@color/md_bottom_sheet_text_color"
        android:gravity="start
center_vertical"
        android:text="@string/bottom_sheet_option_gallery"
        android:textColor="@color/md_bottom_sheet_text_color"
        android:textSize="16sp" />
    <
View
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:layout_marginBottom="@dimen/dp_8"
   
android:layout_marginTop="@dimen/md_bottom_sheet_separator_top_margin"
        android:background="@color/grayTextColor" />
    <
TextView
        android:id="@+id/tv_btn_remove_photo"
        android:layout_width="match_parent"
        android:layout_height="@dimen/dp_48"
        android:layout_marginEnd="@dimen/dp_16"
        android:layout_marginStart="@dimen/dp_16"
        android:backgroundTint="@android:color/white"
        android:drawablePadding="@dimen/dp_32"
        android:drawableStart="@drawable/ic_delete_photo"
        android:drawableTint="@color/md_bottom_sheet_text_color"
        android:gravity="start
center_vertical"
        android:text="@string/bottom_sheet_option_remove_photo"
        android:textColor="@color/md_bottom_sheet_text_color"
        android:textSize="16sp" />
</
LinearLayout>
Step 2: Create the dimen.xml file:
<?xml version="1.0" encoding="utf-8"?><resources>
    <
dimen name="dp_8">8dp</dimen>
    <
dimen name="dp_16">16dp</dimen>
    <
dimen name="dp_24">24dp</dimen>
    <
dimen name="dp_32">32dp</dimen>
    <
dimen name="dp_40">40dp</dimen>
    <
dimen name="dp_48">48dp</dimen>
    <
dimen name="dp_56">56dp</dimen>
    <
dimen name="dp_64">64dp</dimen>
    <
dimen name="dp_72">72dp</dimen>
    <
dimen name="dp_80">80dp</dimen>
    <
dimen name="dp_160">160dp</dimen>
    <
dimen name="md_bottom_sheet_separator_top_margin">7dp</dimen>
</
resources>
The dp used while creating the bottom sheet layout are adhere to theMaterial Design Guidelines.
Step 3: The string.xml file:
<string name="bottom_sheet_option_camera">Use Camera</string>
<
string name="bottom_sheet_option_gallery">Upload from Gallery</string>
<
string name="bottom_sheet_option_heading">Add Photo</string>
<
string name="bottom_sheet_option_remove_photo">Remove Photo</string>
Step 4: Create the custom BottomSheetDialogFragment
public class AddPhotoBottomDialogFragment extends BottomSheetDialogFragment{
    
public static AddPhotoBottomDialogFragment newInstance() {
        
return new AddPhotoBottomDialogFragment();
    }
    @Nullable
    @Override
    
public View onCreateView(LayoutInflater inflater,
                             @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.
layout_photo_bottom_sheet, container,
                
false);
        
// get the views and attach the listener
        return view;
    }
}
Step 5: To show the dialog fragment in the activity:
AddPhotoBottomDialogFragment addPhotoBottomDialogFragment =
        AddPhotoBottomDialogFragment.
newInstance();
addPhotoBottomDialogFragment.show(getSupportFragmentManager(),
        
"add_photo_dialog_fragment");
That’s it... Now you can run the application and check the BottomSheet in your application.
You can create the custom callback (interfaces) in the custom bottom sheet dialog fragment to notify the activity when one of the button is tapped on the Bottom Sheet (dialog fragment).
Thank you for reading the article... :)

Nhận xét

Bài đăng phổ biến từ blog này

Camera2 - Android

Ở bài viết này mình xin giới thiệu về cách sử dụng Camera2 trong android SDK 21. Với các lập trình viên android việc sử dụng Camera có rất nhiều trong ứng dụng: Camera Capture Images, Barcode - QR Code Reader, AR, Video Record,.... Nhiều ứng dụng chỉ ở tầng ứng dụng sử dụng thông qua  Intent  như vậy hệ thống tự động được gọi ở mức tối ưu nhất, nhưng cũng không ít ứng dụng cần can thiệp vào tầng  native  để xử lý Với  Camera   developer.android.com  đã  deprecate  nó đã không còn được sử dụng cơ bản trong các ứng dụng nữa vì rất nhiều nguyên nhân trong đó phải kể tới: tốn tài nguyên, thời gian capture khá chậm và đặc biệt phục vụ nhu cầu ngày càng cao của người dùng như 'Chụp ảnh liên tục, chụp nhiều ảnh và tự động lấy nét' thì  Camera không đáp ứng được Với  Camera2  đã đáp ứng được những thiếu xót trên ngoài ra việc customize cho ảnh là rất dễ dàng mang lại chất lượng cao ngoài ra việc sử dụng cũng không có nhiều thay ...

Sự khác nhau giữa let, apply, with, run và also trong Kotlin

Với những ai đã sử dụng Kotlin để phát triển ứng dụng, chắc hẳn đã không ít lần sử dụng các standard functions run, with, let, also và apply. Để hiểu và sử dụng thành thục chúng không phải là dễ. Và dưới đây là những điều đúc kết lại được. https://viblo.asia/p/su-khac-nhau-giua-let-apply-with-run-va-also-trong-kotlin-RQqKLNdml7z Scoping functions Có thể hiểu đơn giản, scoping function là phạm vi ảnh hưởng nhất định của một hàm. Nó là điều cốt lõi để phân biệt giữa các scoping functions: run, with, T.run, T.let, T.also và T.apply. Dưới đây là minh hoạ phạm vi của hàm run: fun test ( ) { var mood = "I am sad" run { val mood = "I am happy" println ( mood ) // I am happy } println ( mood ) // I am sad } Ở trên, ta có thể thấy rõ ràng trong phạm vi của hàm run, biến mood đã được định nghĩa lại trước khi in ra mà không làm ảnh hưởng tới phần khác của chương trình 3 attributes of scoping functions 1.Normal vs. extension fun...

TỰ ĐỘNG HUỶ ACTIVITY SAU KHI STARTACTIVITY

Trước giờ để huỷ một Activity khi bạn thường dùng hàm  finish()  đúng không nào? Không đi đâu xa là khi bạn Intent từ một Activity này sang Activity khác mà muốn huỷ luôn Activity đầu tiên luôn thì bạn sẽ dùng đoạn code y chang bên dưới chứ? Cơ chế của Activity là khi bạn chuyển từ một Activity này sáng Activity khác thì nó sẽ Activity đó vào stack, và khi back về thì Activity sẽ được hiện lên lại và chạy vào onResume(), nếu bạn chưa hiểu về  vòng đời của Activity  thì xem lại bài viết kèm video tại blog mình nhé, mình ví dụ khá chi tiết. Intent intent = new Intent(MainActivity.this, LoginActivity.class); startActivity(intent); finish(); Đoạn code trên nghĩa là chuyển từ MainActivity sang LoginActivity và sau đó huỷ luôn MainActivity đúng không nào? Tuy nhiên đó không phải là cách duy nhất mà chúng ta làm đâu bởi Android hỗ trợ chúng ta một số thuộc tính mà bạn không phải dùng code Java để làm. Không lưu Activity vào stack Cũng logic như bài toán phí...