Material CAB


Source link: https://github.com/afollestad/material-cab

Material Contextual Action Bar

Material CAB allows you to implement a customizable and flexible contextual action bar in your app. The traditional stock CAB on Android is limited to being placed at the top of your Activity, and the navigation drawer cannot go over it. This library lets you choose its exact location, and a toolbar is used allowing views to be be placed over and under it.

Not only that, the stock CAB only allows you to specify theme properties from styles.xml, this library lets you dynamically change theme properties at runtime from code.

Gradle Dependency

The Gradle dependency is available via jCenter. jCenter is the default Maven repository used by Android Studio.

Dependency

Add this to your module's build.gradle file:

dependencies {

  // ... other dependencies
  compile 'com.afollestad:material-cab:0.1.12' 
}

Attacher

This library attaches to your Activity by taking the place of a ViewStub in your Activity layout. For an example, this is the main layout of the sample project:

<?xml version="1.0" encoding="utf-8"?> <LinearLayout 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:orientation="vertical">

<FrameLayout

android:layout_width="match_parent"

android:layout_height="wrap_content">

 <android.support.v7.widget.Toolbar

 android:id="@+id/main_toolbar"

 android:layout_width="match_parent"

 android:layout_height="?actionBarSize"

 android:background="?colorPrimary"

 android:elevation="@dimen/mcab_toolbar_elevation"

 android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"

 app:contentInsetStart="@dimen/mcab_default_content_inset"

 app:popupTheme="@style/ThemeOverlay.AppCompat.Light"

 tools:ignore="UnusedAttribute" />

 <ViewStub

 android:id="@+id/cab_stub"

 android:layout_width="match_parent"

 android:layout_height="?actionBarSize" />

</FrameLayout>

<android.support.v7.widget.RecyclerView

android:id="@+id/list"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:scrollbars="vertical" />  </LinearLayout>

You attach a Material CAB to the Activity like this:

MaterialCab cab = new MaterialCab(this, R.id.cab_stub)
  .start(this);

R.id.cab_stub references the ViewStub, which is replaced with the CAB toolbar when start() is called.

Note that the parameter in start() is a Callback interface implementer which receives CAB events.


Instead of a ViewStub, you can also pass a ViewGroup (such as a FrameLayout). The CAB will get added as a child to that view group.

Callback

Whether it's an Activity that implements the Callback interface, or an inline callback, it implements these methods:

new MaterialCab.Callback() {

 @Override

 public boolean onCabCreated(MaterialCab cab, Menu menu) {

  // The CAB was started, return true to allow creation to continue.

  return true;

  
}

  @Override

 public boolean onCabItemClicked(MenuItem item) {

  // An item in the toolbar or overflow menu was tapped.

  return true;

 
}

  @Override

 public boolean onCabFinished(MaterialCab cab) {

  // The CAB was finished, return true to allow destruction to continue.

  return true;

 
}

}
;

Properties

This code chains calls to properties that would be commonly used:

MaterialCab cab = new MaterialCab(this, R.id.cab_stub)
  .setTitleRes(R.string.cab_title)
  .setMenu(R.menu.cab_menu)
  .setPopupMenuTheme(R.style.ThemeOverlay_AppCompat_Light)
  .setContentInsetStartRes(R.dimen.mcab_default_content_inset)
  .setBackgroundColorRes(R.color.indigo_500)
  .setCloseDrawableRes(R.drawable.mcab_nav_back)
  .start(this);

Note that most of the property setters have different variations for literal values, dimension resources, and attribute IDs.

You can also check whether or not the CAB is currently started:

MaterialCab cab = // ...  if (cab.isActive()) {

  // Do something 
}

Global Theming

<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">

<!-- Sets a default title for all CABs in the Activity -->
  <item name="mcab_title">@string/hello_world</item>

 <!-- Sets a default inflated menu for all CABs in the Activity -->
  <item name="mcab_menu">@menu/menu_cab</item>

 <!--  

  Changes the default content inset for all CABs in the Activity. 

  Defaults to 72dp. 
 -->
  <item name="mcab_contentinset_start">72dp</item>

 <!--  

  Changes the default CAB background color for all CABs in the Activity. 

  Defaults to the default value of ?colorPrimary (the AppCompat theme attribute). 
 -->
  <item name="mcab_background_color">?colorAccent</item>

 <!--  

  Changes the default CAB close drawable for all CABs in the Activity. 

  Defaults to the AppCompat R.drawable.mcab_nav_back back arrow. 
 -->
  <item name="mcab_close_drawable">@drawable/mcab_nav_back</item>

 <!--  

  Changes the default overflow popup theme for all CABs in the Activity. 

  Defaults to @style/ThemeOverlay.AppCompat.Light. 
 -->
  <item name="mcab_popup_theme">@style/ThemeOverlay.AppCompat.Dark</item>  </style>

Saving and Restoring States

In order to keep the CAB active, and maintain all of its current properties, you have to save and restore the CAB state during configuration changes.

It works like this in an Activity:

private MaterialCab mCab;  @Override protected void onCreate(Bundle savedInstanceState) {

  super.onCreate(savedInstanceState);

// ... other initialization for an Activity

if (savedInstanceState != null) {

// Restore the CAB state, save a reference to mCab.

mCab = MaterialCab.restoreState(savedInstanceState, this, this);

  
}
 else {

// No previous state, first creation.
  
}
 
}
  @Override protected void onSaveInstanceState(Bundle outState) {

  super.onSaveInstanceState(outState);

  if (mCab != null) {

// If the CAB isn't null, save it's state for restoration in onCreate()

mCab.saveState(outState);

  
}
 
}

Finishing the CAB

The icon on the left of the CAB toolbar (the close drawable) will cause the CAB to be finished, but you can also manually finish the CAB:

MaterialCab cab = // ... initialize cab.finish();

Resources

This project aims to provide a working page flip implementation for usage in ListView.

An Android Wear Menu implementation.

It's a very simple custom views library according to UIButton of iOS, all of the views can be automatically set a pressed effect to a button with a simple background image without writing a selector.xml

Dagger2Scopes is a sample Android app to implement usual Dagger 1 multiple scoping logic with the brand new Dagger 2 Google lib. In this sample you will be able to find the following features:

  • Multiple scopes with Dagger 2 (Components and subcomponents).
  • An approach to Clean Architecture.
  • An approach to MVP pattern.

RxCupboard brings the excellent Android Cupboard library into the world of RxJava. Using a small set of convenience Observables, you can fluently store and retrieve streams of POJOs from your database.

An efficient sparse bit set implementation for Java.

Topics


2D Engines   3D Engines   9-Patch   Action Bars   Activities   ADB   Advertisements   Analytics   Animations   ANR   AOP   API   APK   APT   Architecture   Audio   Autocomplete   Background Processing   Backward Compatibility   Badges   Bar Codes   Benchmarking   Bitmaps   Bluetooth   Blur Effects   Bread Crumbs   BRMS   Browser Extensions   Build Systems   Bundles   Buttons   Caching   Camera   Canvas   Cards   Carousels   Changelog   Checkboxes   Cloud Storages   Color Analysis   Color Pickers   Colors   Comet/Push   Compass Sensors   Conferences   Content Providers   Continuous Integration   Crash Reports   Credit Cards   Credits   CSV   Curl/Flip   Data Binding   Data Generators   Data Structures   Database   Database Browsers   Date &   Debugging   Decompilers   Deep Links   Dependency Injections   Design   Design Patterns   Dex   Dialogs   Distributed Computing   Distribution Platforms   Download Managers   Drawables   Emoji   Emulators   EPUB   Equalizers &   Event Buses   Exception Handling   Face Recognition   Feedback &   File System   File/Directory   Fingerprint   Floating Action   Fonts   Forms   Fragments   FRP   FSM   Functional Programming   Gamepads   Games   Geocaching   Gestures   GIF   Glow Pad   Gradle Plugins   Graphics   Grid Views   Highlighting   HTML   HTTP Mocking   Icons   IDE   IDE Plugins   Image Croppers   Image Loaders   Image Pickers   Image Processing   Image Views   Instrumentation   Intents   Job Schedulers   JSON   Keyboard   Kotlin   Layouts   Library Demos   List View   List Views   Localization   Location   Lock Patterns   Logcat   Logging   Mails   Maps   Markdown   Mathematics   Maven Plugins   MBaaS   Media   Menus   Messaging   MIME   Mobile Web   Native Image   Navigation   NDK   Networking   NFC   NoSQL   Number Pickers   OAuth   Object Mocking   OCR Engines   OpenGL   ORM   Other Pickers   Parallax List   Parcelables   Particle Systems   Password Inputs   PDF   Permissions   Physics Engines   Platforms   Plugin Frameworks   Preferences   Progress Indicators   ProGuard   Properties   Protocol Buffer   Pull To   Purchases   Push/Pull   QR Codes   Quick Return   Radio Buttons   Range Bars   Ratings   Recycler Views   Resources   REST   Ripple Effects   RSS   Screenshots   Scripting   Scroll Views   SDK   Search Inputs   Security   Sensors   Services   Showcase Views   Signatures   Sliding Panels   Snackbars   SOAP   Social Networks   Spannable   Spinners   Splash Screens   SSH   Static Analysis   Status Bars   Styling   SVG   System   Tags   Task Managers   TDD &   Template Engines   Testing   Testing Tools   Text Formatting   Text Views   Text Watchers   Text-to   Toasts   Toolkits For   Tools   Tooltips   Trainings   TV   Twitter   Updaters   USB   User Stories   Utils   Validation   Video   View Adapters   View Pagers   Views   Watch Face   Wearable Data   Wearables   Weather   Web Tools   Web Views   WebRTC   WebSockets   Wheel Widgets   Wi-Fi   Widgets   Windows   Wizards   XML   XMPP   YAML   ZIP Codes