Android Material Activity Chooser


Source link: https://github.com/zawadz88/material-activity-chooser

Android Material Activity Chooser

This library allows to use Material activity choosers from Jelly Bean+ to e.g. share a text or a link using a nice Material dialog. Material activity choosers are displayed as bottom sheet dialogs. Bottom sheets are described here: https://material.google.com/components/bottom-sheets.html The actual implementation of activity choosers varies depending on the OS version. This library tries to mimic the activity chooser version you can see on Android Nougat e.g. on Nexus 6P.

Download (from JCenter)

compile 'com.github.zawadz88:material-activity-chooser:0.2.2'

Sample app

Supported features

  • showing the activity chooser as a bottom sheet dialog on all OS versions starting from Jelly Bean (API 16)+
  • a smooth animation when the items appear in the bottom sheet
  • setting custom bottom sheet titles
  • setting different intents per activity, e.g. to have different messages for different applications when sharing
  • use custom styles for the bottom sheet
  • setting a custom message/button text for the empty view when no activities are found
  • setting a custom view when no activities are found
  • adding a button which triggers a PendingIntent when clicked when no activities are found
  • adding custom actions when an activity was clicked
  • going to the chosen activity settings on long-click

Screenshots

Showcase

Sharing with MaterialActivityChooser

Sharing with the system IntentChooser on Nougat for comparison

Sharing with MaterialActivityChooser with custom style

Sharing with MaterialActivityChooser when no activities found

Sharing with MaterialActivityChooser when no activities found with a custom view

Getting started

Add Activity Chooser activity to AndroidManifest.xml

In AndroidManifest.xml add:

 <activity

android:name="com.github.zawadz88.activitychooser.MaterialActivityChooserActivity"

android:theme="@style/MACTheme" />

Showing an activity chooser

To show an activity chooser you need to pass it the intent as follows:

 new MaterialActivityChooserBuilder(context)

.withIntent(someIntent)

.show();

Choosing a custom dialog title

You can set a custom bottom sheet dialog title with either a String or a String resource ID, e.g.

 new MaterialActivityChooserBuilder(context)

.withIntent(someIntent)

.withTitle("Some custom title")

.show();

or

 new MaterialActivityChooserBuilder(context)

.withIntent(someIntent)

.withTitle(R.string.custom_share_title)

.show();

Using alternative intents for selected apps

It is possible to use alternative intents for selected applications instead of the main intent e.g. assuming you wanted to share a message, but you wanted to have a slightly different message for common Mail apps. This can be done by e.g.

 new MaterialActivityChooserBuilder(context)

.withIntent(shareIntent)

.withSecondaryIntent(alternativeShareIntent,

 "com.google.android.gm" /* GMail */,

 "com.google.android.apps.inbox" /* Inbox */,

 "com.microsoft.office.outlook" /* Microsoft Outlook */,

 "com.google.android.email" /* Default mail app */)

.show();

Choosing a custom empty view message

You can set a custom bottom sheet dialog empty view title with either a String or a String resource ID, e.g.

 new MaterialActivityChooserBuilder(context)

.withIntent(someIntent)

.withEmptyViewTitle("Some custom empty view title")

.show();

or

 new MaterialActivityChooserBuilder(context)

.withIntent(someIntent)

.withEmptyViewTitle(R.string.custom_empty_view_title)

.show();

Showing a button on empty view

You can show a button on the empty view which will trigger a PendingIntent once clicked. To do so you need to provide the PendingIntent with/without a button title, e.g.

 new MaterialActivityChooserBuilder(context)

.withIntent(someIntent)

.withEmptyViewAction(pendingIntent)

.show();

or

 new MaterialActivityChooserBuilder(context)

.withIntent(someIntent)

.withEmptyViewAction("Custom button title", pendingIntent)

.show();

or

 new MaterialActivityChooserBuilder(context)

.withIntent(someIntent)

.withEmptyViewAction(R.string.custom_button_title, pendingIntent)

.show();

Setting a custom empty view

You can inflate an entirely custom empty view as well, e.g.

 new MaterialActivityChooserBuilder(context)

.withIntent(someIntent)

.withEmptyViewCustomView(R.layout.layout_custom_empty_view)

.show();
 

Styling the dialog to your own needs

You can override the default styling of the Activity Chooser to your own needs. The easiest way is to override the MACTheme and replace the styles in the custom attributes. For more info see the sample app.

Disabling the item animation

By default there is a layout animation on the RecyclerView which animates the items as they show up. It can be however disabled by extending the MACTheme and overriding mac_bottomSheetRecyclerViewStyle with a style with this animation disabled, e.g.

 <style name="NoAnimationRecyclerViewStyle" parent="MACBottomSheetRecyclerView">

<item name="android:layoutAnimation">@null</item>
  </style>

Tracking when an activity was clicked

You might want to track/log when an activity has been selected to handle your intent. To do so you can extend the default MaterialActivityChooserActivity.

public class TrackingActivityChooserActivity extends MaterialActivityChooserActivity {

...

@Override
  public void onActivityClicked(ResolveInfo activity) {

Toast.makeText(this, "Application clicked: " + activity.activityInfo.packageName, Toast.LENGTH_SHORT).show();

super.onActivityClicked(activity);

  
}
 
}

which needs to be declared in the AndroidManifest.xml

 <activity

android:name=".TrackingActivityChooserActivity"

android:theme="@style/MACTheme" />

and also declared when building the activity chooser:

 new MaterialActivityChooserBuilder(this)

.withIntent(getDefaultShareIntent())

.withActivity(TrackingActivityChooserActivity.class)

.show();

Testing

The library is tested mainly with Espresso + Cucumber as well as a few unit tests.

Instrumentation tests

To run instrumentation tests you need to execute:

./gradlew connectedDebugAndroidTest 

This additionally fetches the Cucumber reports from the device and saves them to sample/build/reports/cucumber.

It is also recommended to install Cucumber for Java and Gherkin plugins in Android Studio for better Cucumber integration inside of the IDE.

Unit tests

To run unit tests you need to execute:

./gradlew testDebugUnitTest 

Code coverage

Tests can be also executed with code coverage. To do so execute:

./gradlew jacocoTestReport 

The coverage report can be then found at sample/build/reports/jacoco. This task cleans the project, runs the unit tests & instrumentation tests and at the end it creates the report.

Running selected scenarios only for instrumentation tests

All the tested scenarios have Cucumber tags. You can run a specific test by using this tag. Assuming you wanted to run only the scenario with tag @sample-scenario-22 you would do the following:

  • From command line:
./gradlew connectedDebugAndroidTest -Ptags="@sample-scenario-22"
  • In Android Studio you need to edit the current Android Tests run configuration and under Extra options enter:
-e tags @sample-scenario-22 

Notes

This library comes with a number of dependencies.
It relies on the BottomSheetBehavior from Android Support Design Library and it uses a RecyclerView for displaying a list of items.

License

Copyright 2016 Piotr Zawadzki

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

     http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Resources

Truth is a testing framework designed to make your tests and their error messages more readable and discoverable, while being extensible to new types of objects.

Truth adopts a fluent style for test propositions, is extensible in several ways, supports IDE completion/discovery of available propositions, and supports different responses to un-true propositions. Truth can be used to declare JUnit-style assumptions (which skip the test on failure), assertions (interrupt the test on failure), and expectations (continue the test, but collect errors and report failure at the end).

Use gradle tasks to run specific adb commands. You can use this plugin to do things such as:

  • Find all devices attached and get basic info about them
  • Select the first one that complies with a custom rule
  • Install a specific APK from the available build types + flavours
  • Clear preferences or do something related to the APK to prepare for tests
  • Run monkey for that specific APK on that specific device
  • Uninstall the APK

Bash script that exports content provider's data to raw and CSV format. It can be your own content provider or any other provider that android decided not to block with permissions.

Android Studio Support for Additional File Types (CSS & JavaScript).

Current File Types Added:

  • CSS ( Cascading Style Sheet )
  • JavaScript

This plugin help you to set material design icons to your Android project.

Small Android library for use the Interactor (use case) and Executor for running code on a ThreadPool Thread or UI Thread. Normally this library is used for implement the Clean Architecture.

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