Silly Android


Source link: https://github.com/milosmns/silly-android

Silly Android

What is this?

Silly Android is an Android (Java) library with various plugins for the core Android framework. In general, AppCompat offers lot of help; but often enough, we need more. A lot of quirky Android APIs introduce the need for a bunch of utility classes, other times we just add a considerable amount of boilerplate code. Having said that, Silly Android does not aim to remove all boilerplate code from your apps, or fix all of the silly API design decisions imposed by the default framework; people are tired of copying the same utilities and workarounds over and over, from one app they work on to the next one - and Silly Android's core goal is to help with that.

In shortest terms, Silly Android is a set of most commonly used workarounds, fixes and utilities, all of which should have been included in the core framework by default. When you find yourself asking questions like "Why is this simple task so complicated to do?" or "How was this not done by default?" - Silly Android should be the help you need.

Examples

Note: Not all features are demonstrated in this section. Please check the 'Code organization' section for more info. All samples internally reference the SillyAndroid class which does all of the heavy lifting - and, all APIs are available through that class directly, but are integrated into Easy and Parsable components for ease of use.

Extra dimensions, colors and other values:

 ...
  <!-- SillyAndroid provides '@dimen/spacing_large', '@color/yellow', '@dimen/text_size_small', etc. -->
  <Button

android:id="@+id/button_random_padding"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:paddingLeft="@dimen/spacing_large"

android:paddingRight="@dimen/spacing_large"

android:text="@string/label_random_padding"

android:textColor="@color/yellow"

android:textSize="@dimen/text_size_small" />
  ...

Typed findView() method (available in all Easy components)

 ...
  TextView displayView = findView(R.id.my_text_view);
 // no cast necessary!
  ...

Screen size calculations:

 ...
  Point screenSize = SillyAndroid.UI.getScreenSize(this);

  if (screenSize.x < 1000 && screenSize.y < 1000) {

// do something dynamically for a small screen
  
}

  ...

Software keyboard helpers (available in all Easy components):

 ...
  @Override
  public void onKeyboardShown(int size) {

Log.d(TAG, "Current keyboard size is: " + size);

  
}

 @Override
  public void onKeyboardHidden() {

Log.d(TAG, "Current keyboard is hidden!");

  
}

 @Override
  public void onClick(@NonNull View view) {

// this returns false if it fails to hide the keyboard:

hideKeyboard();

}

  ...

UI configuration checks:

 ...
  @DeviceType int type = SillyAndroid.UI.getDeviceType(this);

  if (type == SillyAndroid.UI.TABLET_PORT || type == SillyAndroid.UI.PHONE_LAND) {

// do something weird
  
}

  ...

EasyActivity permission enhancement (also available in EasyFragments):

 ...
  // obviously you don't need to request this, but it's just a demo
  if (!hasPermission(Manifest.permission.ACCESS_WIFI_STATE)) {

// no typed array, just declare them one after the other

requestPermissions(REQ_CODE, Manifest.permission.ACCESS_WIFI_STATE);

 return;
  
}

  // do your permitted work here
  ...

EasyFragment permission enhancement (also available in EasyActivities):

 @Override
  protected void onPermissionsResult(int reqestCode, @NonNull Set<String> granted, @NonNull Set<String> denied) {

if (granted.contains(Manifest.permission.ACCESS_WIFI_STATE)) {

 // do your permitted work here

}

  
}

Easy Toast displays (available in all Easy components):

 ...
  toastLong(R.string.my_error);

  toastShort("Dynamic text here");

  ...

Easy conversions, checks and setters (available in all Easy components):

 ...
  // dp<->px conversions, network checks, and finally an easy padding setter
  int padding = SillyAndroid.convertDipsToPixels(getContext(), 20);

  boolean hasNetwork = SillyAndroid.isNetworkConnected(getContext());

  setPadding(mYourView, 0);
 // reset padding for some reason
  if (hasNetwork) {

setPaddingVertical(mYourView, padding);

  
}
 else {

setPaddingHorizontal(mYourView, padding);

  
}

  ...

Thread checking:

 ...
  if (SillyAndroid.isThisMainThread()) {

throw new IllegalStateException("Don't run this on the UI thread");

  
}

  ...

Dynamic, responsive, and contrasted View coloring:

 /* 
  * Prepare the special button coloring to demonstrate the Coloring class (you would have something like this happen generically in real apps): 
  * 
  * - IDLE state colors - 
  *
  Background: GRAY 
  *
  Text: Contrast to [IDLE.Background] 
  *
  Icon: Contrast to [IDLE.Background] 
  * 
  * - PRESSED state colors - 
  *
  Background: #FFCC00 (bright yellow) 
  *
  Text: Contrast to [PRESSED.Background] 
  *
  Icon: Contrast to [PRESSED.Background] 
  * 
  * Android does not recolor to contrast colors when pressed, so we're doing that manually below. 
  */
  int idleBackgroundColor = Color.GRAY; // button background when not pressed
  int idleContentColor = Coloring.contrastColor(idleBackgroundColor);
 // text and icon color when not pressed (need them to be always legible)
  int pressedBackgroundColor = 0xFFFFCC00; // button background highlight color when pressed
  Drawable originalDrawable = ContextCompat.getDrawable(this, android.R.drawable.star_big_on);
 // load a random icon from android
  StateListDrawable statefulDrawable = Coloring.createContrastStateDrawable(this, idleContentColor, pressedBackgroundColor, true, originalDrawable);

  ColorStateList statefulTextColors = Coloring.createContrastTextColors(idleContentColor, pressedBackgroundColor);

  Rect originalBounds = mPaddingButton.getBackground().copyBounds();
 // copy original drawable's bounds so that the ripple is bordered
  int cornerRoundness = SillyAndroid.convertDipsToPixels(this, 4);

  Drawable backgroundDrawable = Coloring.createResponsiveDrawable(this, idleBackgroundColor, pressedBackgroundColor, idleBackgroundColor, true, cornerRoundness, originalBounds);

  // set the background color, pre-colored compound icon and text colors
  setBackgroundCompat(mPaddingButton, backgroundDrawable);

  mPaddingButton.setCompoundDrawablesWithIntrinsicBounds(statefulDrawable, null, null, null);

  mPaddingButton.setTextColor(statefulTextColors);

Automatic layout and menu setup for Parsable components:

 @Menu(R.menu.my_menu)
  @Layout(R.layout.my_layout)
  class MainActivity extends ParsableActivity {

// your activity code here
  
}

Automatic View finding and injections for Parsable components:

 @Layout(R.layout.my_layout)
  class MainActivity extends ParsableActivity {

@Clickable // makes the View respond to clicks via this#onClick()

@FindView(R.id.my_clickable_button)

private Button mClickableButton;

  @LongClickable // makes the View respond to clicks via this#onLongClick()

@FindView(R.id.my_long_clickable_button)

private Button mLongClickableButton;
  
}

Setup

  • Gradle build - jCenter and mavenCentral are both supported
  • AppCompat, minimum version 25.0.2 - Silly Android heavily relies on the features provided by this library. Note that including other versions might work too, but don't report issues if it does not. You've been warned!

To include Silly Android in your app, add the following line to your app's build.gradle's dependecies block.

 // look for the latest version on top of this file
  compile "me.angrybyte.sillyandroid:sillyandroid:VERSION_NAME" 

Code organization

  • The backbone of the library is here: me.angrybyte.sillyandroid.SillyAndroid. It contains various utilities to help around with accessing APIs more easily.
  • Check out the me.angrybyte.sillyandroid.extras.Coloring class for various color and drawable utilities.
  • Android components such as Activity, Fragment, Dialog, View and ViewGroup have been enhanced to include features from the Silly Android internally, you can check that out in the me.angrybyte.sillyandroid.components package. They are now called EasyActivity, EasyFragment and so on.
  • Even more enhancements are added to the Easy component set using Silly Android's annotations, such as View injections, typed T findView(int id) methods, automated click and long-click handling, and more. For information about that, see package me.angrybyte.sillyandroid.parsable.components.
  • For a coded demo of the fully enhanced Activity class, go to me.angrybyte.sillyandroid.demo.MainActivity.
  • Check out the colors, UI sizes and text sizes added in sillyandroid/src/main/res.

Contributions and how we determine what to include

All interested parties need to create a new Feature request so that everyone involved in active development can discuss the feature or the workaround described. Any pull request not referencing a Feature request will be automatically denied. You need to have actual reasons backed up by real-world facts in order to include stuff in the library - otherwise, we would have a huge library with lots of useless components that would need to be removed with ProGuard (and we don't want to be another AppCompat).

Furthermore, we are trying to test everything that's not trivial and keep the code as clean as humanly possible; thus, any pull requests that fail the CI code quality check or fail to properly pass the tests will also be denied. If pull requests pass every check (and don't worry, it's not impossible to pass), one of the admins could then merge the changes to the release branch - this triggers a CI build with device/emulator tests. If all goes ok, the library is automatically deployed to jCenter and MavenCentral.

Further support

In case of emergency errors, please create an issue. We missed something really useful? Have an idea for a cleaner API? Fork this project and submit a pull request through GitHub. Keep in mind that you need a Feature request first with a finalized discussion (see the Contributions section). Some more help may be found here:

Resources

Slanted TextView for Android.

Another NumberPicker view with flexible configuration.

Compressor is a lightweight and powerful android image compression library. Compressor will allow you to compress large photos into smaller sized photos with very less or negligible loss in quality of the image.

The Asciidoctor Gradle Plugin is the official means of using Asciidoctor to convert all your AsciiDoc documentation using Gradle.

An Espresso IdlingResource for OkHttp.

Android view for displaying PDFs rendered with PdfiumAndroid.

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