Tuesday, March 14, 2017

Bottom Sheet




 
build.gragle file 
compile 'com.android.support:appcompat-v7:25.2.0' 
compile 'com.android.support:design:25.2.0'
 
MainActivity
 
public class MainActivity extends AppCompatActivity {
    String TAG = "MainActivity";
    @Override 
 protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState); 
 setContentView(R.layout.activity_main); 
 View bottomSheet = findViewById(R.id.design_bottom_sheet); 
 final BottomSheetBehavior behavior = BottomSheetBehavior.from(bottomSheet); 
 // listen view change 
 behavior.setBottomSheetCallback(new BottomSheetBehavior.BottomSheetCallback() {
            @Override 
 public void onStateChanged(@NonNull View bottomSheet, int newState) {
 switch (newState) {
 case BottomSheetBehavior.STATE_DRAGGING:
 Log.d(TAG, "BottomSheetBehavior.STATE_DRAGGING"); 
 break; 
 case BottomSheetBehavior.STATE_SETTLING:
 Log.d(TAG, "BottomSheetBehavior.STATE_SETTLING"); 
 break; 
 case BottomSheetBehavior.STATE_EXPANDED:
 Log.d(TAG, "BottomSheetBehavior.STATE_EXPANDED"); 
 break; 
 case BottomSheetBehavior.STATE_COLLAPSED:
 Log.d(TAG, "BottomSheetBehavior.STATE_COLLAPSED"); 
 break; 
 case BottomSheetBehavior.STATE_HIDDEN:
 Log.d(TAG, "BottomSheetBehavior.STATE_HIDDEN"); 
 break;                }
            }

            @Override 
 public void onSlide(@NonNull View bottomSheet, float slideOffset) {
                Log.d(TAG, "slideOffset: " + slideOffset); 
                     }
        });
        Button button = (Button) findViewById(R.id.button); 
 button.setOnClickListener(new View.OnClickListener() {
            @Override 
 public void onClick(View view) {
                if (behavior.getState() == BottomSheetBehavior.STATE_COLLAPSED) {
                    behavior.setState(BottomSheetBehavior.STATE_EXPANDED); 
                 } else {
                    behavior.setState(BottomSheetBehavior.STATE_COLLAPSED); 
                 }
                      }
             }); 
          }
}
 
 
 activity_main
 
<?xml version="1.0" encoding="utf-8"?> 
<android.support.design.widget.CoordinatorLayout 
 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">
    <android.support.v4.widget.NestedScrollView 
 android:layout_width="match_parent" 
 android:layout_height="match_parent" 
 app:layout_behavior="@string/appbar_scrolling_view_behavior">
        <RelativeLayout 
 android:layout_width="match_parent" 
 android:layout_height="match_parent" 
 android:paddingBottom="@dimen/activity_vertical_margin" 
 android:paddingLeft="@dimen/activity_horizontal_margin" 
 android:paddingRight="@dimen/activity_horizontal_margin" 
 android:paddingTop="@dimen/activity_vertical_margin">
            <ImageView 
 android:id="@+id/image" 
 android:layout_width="wrap_content" 
 android:layout_height="wrap_content" 
 android:layout_centerHorizontal="true" 
 android:src="@mipmap/ic_launcher"/>
            <Button 
 android:id="@+id/button" 
 android:layout_width="wrap_content" 
 android:layout_height="wrap_content" 
 android:layout_below="@+id/image" 
 android:layout_centerHorizontal="true" 
 android:text="Show Bottom Sheet"/> 
 </RelativeLayout> 
 </android.support.v4.widget.NestedScrollView>
    <RelativeLayout 
 android:id="@+id/design_bottom_sheet" 
 android:layout_width="match_parent" 
 android:layout_height="match_parent" 
 android:background="@color/colorAccent" 
 android:paddingBottom="@dimen/activity_vertical_margin" 
 android:paddingLeft="@dimen/activity_horizontal_margin" 
 android:paddingRight="@dimen/activity_horizontal_margin" 
 android:paddingTop="@dimen/activity_vertical_margin" 
 app:behavior_hideable="true" 
 app:behavior_peekHeight="50dp" 
 app:elevation="4dp" 
 app:layout_behavior="@string/bottom_sheet_behavior">
        <TextView 
 android:id="@+id/bottomsheet_text" 
 android:layout_width="wrap_content" 
 android:layout_height="match_parent" 
 android:text="Introducing Bottom Sheets" 
 android:textColor="#FFFFFF"/>
    </RelativeLayout>

</android.support.design.widget.CoordinatorLayout>

No comments:

Post a Comment