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

NHỮNG WIDGET THƯỜNG DÙNG TRONG FLUTTER

 https://baoflutter.com/nhung-widget-thuong-dung-trong-flutter/ Trong bài viết trước về  cách xây dựng màn hình ứng dụng Flutter , các bạn biết rằng các màn hình ứng dụng được tạo ra bởi các widget ghép lại với nhau. Vì vậy việc hiểu và sử dụng các Widget là rất quan trọng. Vì vậy trong bài viết này, tôi sẽ giới thiệu cho các bạn về những widget quan trọng trong Flutter. Hầu hết các Widget đều có các phần sau đây: + Đặc tính của Widget như : color, theme, height, weight, decoration, onTap, onPressed + Liên kết với các Widget khác với từ khoá: child, children, home hoặc body Ví dụ : 1 2 3 4 5 6 Container ( color : Colors . blue , height : 300 , weight : 300 , child : Text ( "Widget con" ) , ) Khi làm một số App cơ bản, bạn sẽ nắm chắc được cách sử dụng các Widget hay dùng. MaterialApp – Là widget rất liện lợi, cung cấp các widget cho việc xây dựng ứng dụng sử dụng thư viện Material Design UI của google. – Widget này được sử dụng trong hàm build đầu tiên của hầu hết các ứn...

Get Current location using FusedLocationProviderClient in Android

Hello to coders, Previously we have taught you  how you get current location using GPS/Network Provider . Then android has revealed  FusedLocationProviderClient  under  GoogleApi . FusedLocationProviderClient is for interacting with the location using fused location provider. ( NOTE :   To use this feature, GPS must be turned on your device. For manually ask the user to turn on GPS, please check  next article ) So let’s get started for the tutorial for getting the current location. First, add a dependency for location by play services: implementation 'com.google.android.gms:play-services-location:15.0.1' Then define FusedLocationProviderClient: private FusedLocationProviderClient mFusedLocationClient; private TextView txtLocation; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout. activity_main ); this.txtLocation = (TextView) findViewById(R.id. txtLocat...

Get You Last known Location & Current Location using FusedLocationProviderClient

NOVEMBER 30, 2017 Get You Last known Location & Current Location using FusedLocationProviderClient        I would like to cover very basic and simple example of retreiving last known location & Current Location  using Fused location API.  I have written many example for fused location API in my previous posts but in this post i will show how to get retrieve the location  without implementing Google Api Client . All these magics happen after the release of version  11.0.0 of Google Play services SDK. FusedLocationProviderApi is Deprecated We have previously used following way to retrieve the last known location. mLastLocation  =  LocationServices. FusedLocationApi . getLastLocation ( mGoogleApiClient ) ; We no need to define LocationServices. FusedLocationApi  anymore. it is deprecated. It will be removed in the future release. Instead You can use LocationServi...