Android Open Source - Material Snackbar Fragment






From Project

Back to project page Material.

License

The source code is released under:

Apache License

If you think the Android project Material listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.rey.material.demo;
/*from  w  ww . j  av a2  s .  co  m*/
import android.annotation.TargetApi;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.rey.material.util.ThemeUtil;
import com.rey.material.view.Button;
import com.rey.material.view.SnackBar;

public class SnackbarFragment extends Fragment{
  
  SnackBar mSnackBar;
    
  public static SnackbarFragment newInstance(){
    SnackbarFragment fragment = new SnackbarFragment();
    
    return fragment;
  }

  @TargetApi(Build.VERSION_CODES.HONEYCOMB)
  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.fragment_snackbar, container, false);
    
    Button bt_mobile_single = (Button)v.findViewById(R.id.snackbar_bt_mobile_single);
    Button bt_mobile_multi = (Button)v.findViewById(R.id.snackbar_bt_mobile_multi);
    Button bt_tablet_single = (Button)v.findViewById(R.id.snackbar_bt_tablet_single);
    Button bt_tablet_multi = (Button)v.findViewById(R.id.snackbar_bt_tablet_multi);
    
    View.OnClickListener listener = new View.OnClickListener() {
      
      @Override
      public void onClick(View v) {
        if(mSnackBar.getState() == SnackBar.STATE_SHOWED)
          mSnackBar.dismiss();
        else{
          switch (v.getId()) {
            case R.id.snackbar_bt_mobile_single:
              mSnackBar.text("This is single-line snackbar.")
                  .actionText("CLOSE")
                  .singleLine(true)
                  .width(SnackBar.MATCH_PARENT)
                  .minWidth(0)
                  .maxWidth(0)
                  .height(ThemeUtil.dpToPx(getActivity(), 48))
                  .marginLeft(0)
                  .marginBottom(0)
                  .verticalPadding(0)
                  .show(getActivity());
              break;
            case R.id.snackbar_bt_mobile_multi:
              mSnackBar.text("This is multi-line snackbar.\nIt will auto-close after 5s.")
                  .actionText(null)
                  .singleLine(false)
                  .maxLines(2)
                  .width(SnackBar.MATCH_PARENT)
                  .minWidth(0)
                  .maxWidth(0)
                  .height(SnackBar.WRAP_CONTENT)
                  .marginLeft(0)
                  .marginBottom(0)
                  .verticalPadding(ThemeUtil.dpToPx(getActivity(), 12))
                  .duration(5000)
                  .show(getActivity());
              break;
            case R.id.snackbar_bt_tablet_single:
              mSnackBar.text("This is single-line snackbar.")
                  .actionText("CLOSE")
                  .singleLine(true)
                  .width(SnackBar.WRAP_CONTENT)
                  .minWidth(ThemeUtil.dpToPx(getActivity(), 288))
                  .maxWidth(ThemeUtil.dpToPx(getActivity(), 568))
                  .height(ThemeUtil.dpToPx(getActivity(), 48))
                  .marginLeft(ThemeUtil.dpToPx(getActivity(), 16))
                  .marginBottom(ThemeUtil.dpToPx(getActivity(), 16))
                  .verticalPadding(0)
                  .show(getActivity());
              break;
            case R.id.snackbar_bt_tablet_multi:
              mSnackBar.text("This is multi-line snackbar.\nIt will auto-close after 5s.")
                  .actionText(null)
                  .singleLine(false)
                  .maxLines(2)
                  .width(SnackBar.WRAP_CONTENT)
                  .minWidth(ThemeUtil.dpToPx(getActivity(), 288))
                  .maxWidth(ThemeUtil.dpToPx(getActivity(), 568))
                  .height(SnackBar.WRAP_CONTENT)
                  .marginLeft(ThemeUtil.dpToPx(getActivity(), 16))
                  .marginBottom(ThemeUtil.dpToPx(getActivity(), 16))
                  .verticalPadding(ThemeUtil.dpToPx(getActivity(), 12))
                  .duration(5000)
                  .show(getActivity());
              break;
          }
        }        
      }
    };
    
    
    bt_mobile_single.setOnClickListener(listener);
    bt_mobile_multi.setOnClickListener(listener);
    bt_tablet_single.setOnClickListener(listener);
    bt_tablet_multi.setOnClickListener(listener);
    
    mSnackBar = SnackBar.make(getActivity()).applyStyle(R.style.SnackBar);
        
    return v;
  }

  @Override
  public void onPause() {
    super.onPause();
  }

  @Override
  public void onResume() {
    super.onResume();
  }
  
}




Java Source Code List

com.rey.material.ApplicationTest.java
com.rey.material.demo.ButtonFragment.java
com.rey.material.demo.MainActivity.java
com.rey.material.demo.ProgressFragment.java
com.rey.material.demo.SnackbarFragment.java
com.rey.material.demo.SwitchesFragment.java
com.rey.material.demo.TextfieldFragment.java
com.rey.material.drawable.ArrowDrawable.java
com.rey.material.drawable.BlankDrawable.java
com.rey.material.drawable.CheckBoxDrawable.java
com.rey.material.drawable.CircularProgressDrawable.java
com.rey.material.drawable.DividerDrawable.java
com.rey.material.drawable.LineMorphingDrawable.java
com.rey.material.drawable.LinearProgressDrawable.java
com.rey.material.drawable.NavigationDrawerDrawable.java
com.rey.material.drawable.RadioButtonDrawable.java
com.rey.material.drawable.RevealDrawable.java
com.rey.material.drawable.RippleDrawable.java
com.rey.material.drawable.ToolbarRippleDrawable.java
com.rey.material.util.ColorUtil.java
com.rey.material.util.ThemeUtil.java
com.rey.material.util.ViewUtil.java
com.rey.material.view.Button.java
com.rey.material.view.CheckBox.java
com.rey.material.view.CheckedTextView.java
com.rey.material.view.CompoundButton.java
com.rey.material.view.EditText.java
com.rey.material.view.FloatingActionButton.java
com.rey.material.view.ListPopupWindow.java
com.rey.material.view.ListView.java
com.rey.material.view.PopupWindow.java
com.rey.material.view.ProgressView.java
com.rey.material.view.RadioButton.java
com.rey.material.view.RippleManager.java
com.rey.material.view.SnackBar.java
com.rey.material.view.Spinner.java
com.rey.material.view.Switch.java
com.rey.material.view.TabPageIndicator.java
com.rey.material.view.TextView.java