1/* 2 * Copyright (C) 2016 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17package com.example.android.support.design.widget; 18 19import android.os.Bundle; 20import android.view.LayoutInflater; 21import android.view.View; 22 23import androidx.appcompat.app.AppCompatActivity; 24import androidx.coordinatorlayout.widget.CoordinatorLayout; 25 26import com.example.android.support.design.R; 27import com.google.android.material.snackbar.BaseTransientBottomBar; 28 29/** 30 * This demonstrates custom usage of the snackbar 31 */ 32public class CustomSnackbarUsage extends AppCompatActivity { 33 34 private CoordinatorLayout mContentView; 35 36 @Override 37 protected void onCreate(Bundle savedInstanceState) { 38 super.onCreate(savedInstanceState); 39 setContentView(R.layout.custom_snackbar_with_fab); 40 41 mContentView = findViewById(R.id.content_view); 42 } 43 44 /** Shows a custom snackbar with no action. */ 45 public void show(View view) { 46 final LayoutInflater inflater = LayoutInflater.from(mContentView.getContext()); 47 final CustomSnackbarMainContent content = 48 (CustomSnackbarMainContent) inflater.inflate( 49 R.layout.custom_snackbar_include, mContentView, false); 50 final BaseTransientBottomBar.ContentViewCallback contentViewCallback = 51 new BaseTransientBottomBar.ContentViewCallback() { 52 @Override 53 public void animateContentIn(int delay, int duration) { 54 content.setAlpha(0f); 55 content.animate().alpha(1f).setDuration(duration) 56 .setStartDelay(delay).start(); 57 } 58 59 @Override 60 public void animateContentOut(int delay, int duration) { 61 content.setAlpha(1f); 62 content.animate().alpha(0f).setDuration(duration) 63 .setStartDelay(delay).start(); 64 } 65 }; 66 new CustomSnackbar(mContentView, content, contentViewCallback).setTitle("Custom title") 67 .setSubtitle("Custom subtitle").show(); 68 } 69} 70