Home | History | Annotate | Download | only in widget
      1 /*
      2  * Copyright (C) 2015 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 
     17 package com.example.android.support.design.widget;
     18 
     19 import android.os.Bundle;
     20 import android.view.View;
     21 import android.view.ViewGroup;
     22 import android.widget.Toast;
     23 
     24 import androidx.appcompat.app.AppCompatActivity;
     25 
     26 import com.example.android.support.design.R;
     27 import com.google.android.material.snackbar.Snackbar;
     28 
     29 /**
     30  * This demonstrates idiomatic usage of the snackbar
     31  */
     32 public class SnackbarUsage extends AppCompatActivity {
     33 
     34     private ViewGroup mContentView;
     35 
     36     @Override
     37     protected void onCreate(Bundle savedInstanceState) {
     38         super.onCreate(savedInstanceState);
     39         setContentView(getLayoutId());
     40 
     41         mContentView = findViewById(R.id.content_view);
     42     }
     43 
     44     protected int getLayoutId() {
     45         return R.layout.design_snackbar;
     46     }
     47 
     48     public void showShort(View view) {
     49         Snackbar.make(mContentView, "Short snackbar message", Snackbar.LENGTH_SHORT).show();
     50     }
     51 
     52     public void showAction(View view) {
     53         Snackbar.make(mContentView, "Short snackbar message", Snackbar.LENGTH_SHORT)
     54                 .setAction("Action", new View.OnClickListener() {
     55                     @Override
     56                     public void onClick(View view) {
     57                         Toast.makeText(SnackbarUsage.this, "Snackbar Action pressed",
     58                                 Toast.LENGTH_SHORT).show();
     59                     }
     60                 }).show();
     61     }
     62 
     63     public void showLong(View view) {
     64         Snackbar.make(mContentView, "Long snackbar message which wraps onto another line and"
     65                 + "makes the Snackbar taller", Snackbar.LENGTH_SHORT).show();
     66     }
     67 
     68     public void showLongAction(View view) {
     69         Snackbar.make(mContentView, "Long snackbar message which wraps onto another line and"
     70                 + "makes the Snackbar taller", Snackbar.LENGTH_SHORT)
     71                 .setAction("Action", new View.OnClickListener() {
     72                     @Override
     73                     public void onClick(View view) {
     74                         Toast.makeText(SnackbarUsage.this, "Snackbar Action pressed",
     75                                 Toast.LENGTH_SHORT).show();
     76                     }
     77                 }).show();
     78     }
     79 
     80     public void showLongLongAction(View view) {
     81         Snackbar.make(mContentView, "Long snackbar message which wraps onto another line and"
     82                 + "makes the Snackbar taller", Snackbar.LENGTH_SHORT)
     83                 .setAction("Action which wraps", new View.OnClickListener() {
     84                     @Override
     85                     public void onClick(View view) {
     86                         Toast.makeText(SnackbarUsage.this, "Snackbar Action pressed",
     87                                 Toast.LENGTH_SHORT).show();
     88                     }
     89                 }).show();
     90     }
     91 
     92 }