Pretty


Source link: https://github.com/madisp/pretty

pretty

Pretty is a library that enables one to enhance Android layout inflation without boilerplate and knowledge about LayoutInflater internals. Pretty hooks into the Android layout inflation so you could add new attributes to existing View subtypes.

Example

Lets take a standard example - using a custom font with TextViews. If you have used a custom font chances are that you have something like this in your codebase:

TextView text1 = (TextView) findViewById(R.id.text1);
 Typeface tf = Typeface.createFromAsset(getResources().getAssets(), "my-font.ttf");
 text1.setTypeface(tf);

This approach doesn't really scale that well once you have a bunch of complex layouts. This usually results in either creating a TextView subclass or some trickery with walking the view tree and replacing the typeface for all TextView instances.

Also one shouldn't mix presentation definition (the font) with logic (the Java code) if at all possible. Wouldn't it be nice if you could just point to your custom font right there in the layout? E.g.:

<TextView android:id="@+id/text1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:typeface_asset="my-font.ttf" />

With some LayoutInflater trickery it is actually possible. Pretty does all the trickery for you so you can focus on the code that matters. Lets write a pretty decorator that enables us to use a custom attribute to load the typeface.

First off we need to define our custom attribute in res/values/attrs.xml:

<?xml version="1.0" encoding="utf-8"?> <resources>
<attr name="typeface_asset" format="string" /> </resources>

After having a custom attribute we can reference it in java code through the R.attr class and in our layouts using the res-auto package namespace. Now we can write a decorator that uses the attribute to change the typeface for any TextView instances.

We could write a general Decor implementation but there is a base class for writing decorators that work explicitly with attributes - AttrsDecor<T extends View>. Lets use it to implement our typeface_asset attribute:

public class FontDecor extends AttrsDecor<TextView> {
  @Override  protected int[] attrs() {

  // return the attrs this decorator applies to
return new int[] {
 R.attr.typeface_asset 
}
;  
}

@Override  protected Class<TextView> clazz() {

  // we want to modify all the TextView instances. This includes descendants,
  // like Button or EditText.
return TextView.class;  
}

@Override  protected void apply(TextView view, int attr, TypedValue value) {

  // this method is called if we have a TextView with any of the attributes
  // we previously declared. Usually one would have a switch here but as this
  // decorator only supports a single attr we don't need to do that as we know
  // it can only be R.attr.typeface_asset.
  // So lets just replace the typeface here.
view.setTypeface(Typeface.createFromAsset(view.getResources().getAssets(), value.string.toString()));
  
}
 
}

Now we can use our attribute in a layout:

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:auto="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="16dp">
<TextView
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:text="Woop, custom fonts!"
  auto:typeface_asset="my-font.ttf" /> </LinearLayout>

Pretty straightforward, right? Now all we need to do is to attach pretty to an activity so it can take care of all the LayoutInflater magic. We also need to register our new shiny decorator.

public class SampleActivity extends Activity {
  @Override  protected void onCreate(Bundle savedInstanceState) {

Pretty.wrap(this).with(new FontDecor());

super.onCreate(savedInstanceState);

setContentView(R.layout.main);
  
}
 
}

You can try out this code by checking out the repository and looking at the samples project.

How pretty works

Pretty does its magic by replacing the view Factory in the LayoutInflater. This idea was borrowed from the android support library - thats how the support library enables one to use the <fragment> tags in OS versions that do not know about the tag.

Limitations

Currently using attributes with the auto prefix for system widgets raises a lint error Unexpected namespace prefix "auto" found for tag T. Fear not, this is a red herring. The solution is to suppress that check in your gradle buildfile:

android {

lintOptions {

  disable 'MissingPrefix'

}
 
}

License

Standard MIT. See the LICENSE file.

Resources

A customized video view that will automatically pause video if user is not looking at device screen.

Material Deisgn RatingBar with Google app's style while fixinig a bunch of framework limitation/bugs. It extends framework RatingBar and can be used as a drop-in replacement.

Android File Chooser is a simple and customizable file/directory chooser dialog which you can use in your apps to let your users select a file or directory based on your needs.

This is a library that contains practical animations: Rotation, Flip, Horizontal and Vertical Shake, Pulse.

Designed simply and elegant these components are easy to integrate during development process.

This project makes a request to Play Store to check if there is a new version of your published app. Then, you can notify your users to do the update.

This Android project allows to send Intent to destination Activity when app was closed (in the simplest way).

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